Sin descripción

upload.vue 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <script setup lang="ts">
  2. import { onMounted, ref } from 'vue'
  3. import { useToast } from 'wot-design-uni'
  4. import { dealUploadTask } from '@/api/schedule/task'
  5. import useUpload from '@/hooks/useUpload'
  6. const toast = useToast()
  7. const taskId = ref('')
  8. const checkId = ref('')
  9. // 表单数据
  10. const formData = ref({
  11. attachments: '',
  12. remark: '',
  13. })
  14. // 上传文件列表
  15. const uploadFiles = ref<any[]>([])
  16. // 初始化useUpload
  17. const { run: uploadRun, loading: uploadLoading } = useUpload({
  18. fileType: 'file',
  19. success: (data) => {
  20. console.log('上传成功:', data)
  21. // 构建文件对象
  22. const fileObj = {
  23. url: data.url || '',
  24. originalFilename: data.originalFilename || data.name || '',
  25. name: data.fileName || data.name || '',
  26. }
  27. // 添加到上传文件列表
  28. uploadFiles.value.push(fileObj)
  29. // 更新附件字段
  30. const fileNames = uploadFiles.value.map((f) => {
  31. try {
  32. const response = JSON.parse(f.response)
  33. return response.data.fileName
  34. }
  35. catch (error) {
  36. console.error('解析文件响应失败:', error)
  37. return ''
  38. }
  39. }).filter(Boolean)
  40. formData.value.attachments = fileNames.join(',')
  41. console.log('文件名称:', formData.value.attachments)
  42. },
  43. error: (err) => {
  44. console.error('上传失败:', err)
  45. toast.error('上传失败,请重试')
  46. },
  47. })
  48. // 处理删除文件
  49. function handleRemoveFile(file: any) {
  50. const index = uploadFiles.value.findIndex(f => f.url === file.url)
  51. if (index !== -1) {
  52. uploadFiles.value.splice(index, 1)
  53. // 更新附件字段
  54. const fileNames = uploadFiles.value.map((f) => {
  55. try {
  56. const response = JSON.parse(f.response)
  57. return response.data.fileName
  58. }
  59. catch (error) {
  60. console.error('解析文件响应失败:', error)
  61. return ''
  62. }
  63. }).filter(Boolean)
  64. formData.value.attachments = fileNames.join(',')
  65. }
  66. }
  67. // 提交表单
  68. async function submitForm() {
  69. // 这里可以添加表单验证逻辑
  70. // 检查是否有文件上传
  71. if (uploadFiles.value.length === 0) {
  72. toast.info('请上传附件')
  73. return
  74. }
  75. // 构建请求参数
  76. const params = {
  77. taskId: Number(taskId.value),
  78. checkId: Number(checkId.value),
  79. ...formData.value,
  80. id: taskId.value,
  81. attachment: uploadFiles.value.map(f => f.name).join(','),
  82. attachmentName: uploadFiles.value.map(f => f.originalFilename).join(','),
  83. remark: formData.value.remark,
  84. }
  85. // 调用处理任务附件接口
  86. try {
  87. const res: any = await dealUploadTask(params)
  88. if (res.code === 200) {
  89. toast.success('提交成功')
  90. // 跳转回上一页
  91. setTimeout(() => {
  92. uni.navigateBack()
  93. }, 1500)
  94. }
  95. else {
  96. toast.error(res.msg || '提交失败,请重试')
  97. return
  98. }
  99. }
  100. catch (error) {
  101. toast.error('提交失败,请重试')
  102. return
  103. }
  104. }
  105. // 页面加载时获取参数
  106. onMounted(() => {
  107. // 获取页面参数
  108. const pages = getCurrentPages()
  109. const currentPage = pages[pages.length - 1]
  110. // 使用类型断言获取options
  111. const options = (currentPage as any).options || {}
  112. taskId.value = options.id || ''
  113. checkId.value = options.checkId || ''
  114. })
  115. </script>
  116. <template>
  117. <view class="page min-h-screen bg-gray-100 p-4">
  118. <view class="rounded-lg bg-white p-4 shadow-sm">
  119. <!-- 上传附件 -->
  120. <view class="mb-6">
  121. <view class="flex items-center justify-between">
  122. <text class="font-medium">上传附件</text>
  123. <wd-button type="text" @click="uploadRun">
  124. 点击上传附件
  125. </wd-button>
  126. </view>
  127. <view v-for="(file, index) in uploadFiles" :key="index" class="mt-2 flex items-center justify-between">
  128. <wd-icon name="close-circle" class="mr-2 text-red-500" @click="handleRemoveFile(file)" />
  129. <wd-text :text="file.originalFilename" class="flex-1" />
  130. </view>
  131. </view>
  132. <!-- 备注 -->
  133. <view class="mb-6">
  134. <text class="mb-2 block font-medium">备注</text>
  135. <wd-textarea v-model="formData.remark" placeholder="请填写备注" auto-height :maxlength="120" clearable show-word-limit />
  136. </view>
  137. <!-- 提交按钮 -->
  138. <view class="mt-8 flex justify-center">
  139. <wd-button
  140. type="primary"
  141. size="large"
  142. @click="submitForm"
  143. >
  144. 提交
  145. </wd-button>
  146. </view>
  147. <wd-toast />
  148. </view>
  149. </view>
  150. </template>