form.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <script lang="ts" setup>
  2. import { reactive, ref } from "vue";
  3. import { formUpload } from "@/api/mock";
  4. import { message } from "@/utils/message";
  5. import { createFormData } from "@pureadmin/utils";
  6. import UploadIcon from "@iconify-icons/ri/upload-2-line";
  7. const formRef = ref();
  8. const uploadRef = ref();
  9. const validateForm = reactive({
  10. fileList: [],
  11. date: ""
  12. });
  13. const submitForm = formEl => {
  14. if (!formEl) return;
  15. formEl.validate(valid => {
  16. if (valid) {
  17. // 多个 file 在一个接口同时上传
  18. const formData = createFormData({
  19. files: validateForm.fileList.map(file => ({ raw: file.raw })), // file 文件
  20. date: validateForm.date // 别的字段
  21. });
  22. formUpload(formData)
  23. .then(({ success }) => {
  24. if (success) {
  25. message("提交成功", { type: "success" });
  26. } else {
  27. message("提交失败");
  28. }
  29. })
  30. .catch(error => {
  31. message(`提交异常 ${error}`, { type: "error" });
  32. });
  33. } else {
  34. return false;
  35. }
  36. });
  37. };
  38. const resetForm = formEl => {
  39. if (!formEl) return;
  40. formEl.resetFields();
  41. };
  42. </script>
  43. <template>
  44. <el-form ref="formRef" :model="validateForm" label-width="82px">
  45. <el-form-item
  46. label="附件"
  47. prop="fileList"
  48. :rules="[{ required: true, message: '附件不能为空' }]"
  49. >
  50. <el-upload
  51. ref="uploadRef"
  52. v-model:file-list="validateForm.fileList"
  53. drag
  54. multiple
  55. action="#"
  56. class="!w-[200px]"
  57. :auto-upload="false"
  58. >
  59. <div class="el-upload__text">
  60. <IconifyIconOffline
  61. :icon="UploadIcon"
  62. width="26"
  63. class="m-auto mb-2"
  64. />
  65. 可点击或拖拽上传
  66. </div>
  67. </el-upload>
  68. </el-form-item>
  69. <el-form-item
  70. label="日期"
  71. prop="date"
  72. :rules="[{ required: true, message: '日期不能为空' }]"
  73. >
  74. <el-date-picker
  75. v-model="validateForm.date"
  76. type="datetime"
  77. class="!w-[200px]"
  78. placeholder="请选择日期时间"
  79. value-format="YYYY-MM-DD HH:mm:ss"
  80. />
  81. </el-form-item>
  82. <el-form-item>
  83. <el-button type="primary" text bg @click="submitForm(formRef)">
  84. 提交
  85. </el-button>
  86. <el-button text bg @click="resetForm(formRef)">重置</el-button>
  87. </el-form-item>
  88. </el-form>
  89. </template>