SpecimenImportForm.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <Dialog loading="loading" v-model="dialogVisible" title="标本信息导入" width="600">
  3. <el-alert title="需确保图片命名规范,不与系统中旧图片命名重复,否则将会覆盖原图片。" type="warning" style="margin-bottom: 10px"/>
  4. <el-upload
  5. ref="uploadRef"
  6. v-model:file-list="fileList"
  7. :action=" importUrl + '?updateSupport=' + updateSupport"
  8. :auto-upload="false"
  9. :disabled="formLoading"
  10. :headers="uploadHeaders"
  11. :limit="1"
  12. :on-error="submitFormError"
  13. :on-exceed="handleExceed"
  14. accept=".xlsx, .xls"
  15. drag
  16. >
  17. <Icon icon="ep:upload" />
  18. <div class="el-upload__text">将xlsx表拖到此处,或<em>点击上传</em></div>
  19. <template #tip>
  20. <div class="el-upload__tip text-center">
  21. <span>仅允许导入 xls、xlsx 格式文件。</span>
  22. <el-link
  23. :underline="false"
  24. style="font-size: 12px; vertical-align: baseline"
  25. type="primary"
  26. @click="importTemplate"
  27. >
  28. 下载模板
  29. </el-link>
  30. <div class="el-upload__tip" style="color:#ff0000;">
  31. <el-checkbox v-model="updateSupport"/>
  32. 是否更新已经存在的标本数据
  33. </div>
  34. </div>
  35. </template>
  36. </el-upload>
  37. <el-upload
  38. ref="uploadimageRef"
  39. v-model:file-list="imageList"
  40. drag
  41. :action="importUrl + '?updateSupport=' + updateSupport + '&updateType=' + updateType"
  42. :on-success="submitImageSuccess"
  43. :auto-upload="false"
  44. :disabled="formLoading"
  45. :limit="1"
  46. :on-error="submitFormError"
  47. :headers="uploadHeaders"
  48. accept=".zip"
  49. >
  50. <el-icon class="el-icon--upload">
  51. <upload-filled/>
  52. </el-icon>
  53. <div class="el-upload__text">
  54. <div class="el-upload__text">将图片压缩包拖到此处,或<em>点击上传</em></div>
  55. </div>
  56. <template #tip>
  57. <div class="el-upload__tip text-center">
  58. <span>仅允许导入 .zip 格式文件。</span>
  59. <!-- <div class="el-upload__tip">-->
  60. <!-- <el-checkbox v-model="updateSupport"/>-->
  61. <!-- 是否更新已经存在的标本数据-->
  62. <!-- </div>-->
  63. </div>
  64. </template>
  65. </el-upload>
  66. <template #footer>
  67. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  68. <el-button @click="dialogVisible = false">取 消</el-button>
  69. </template>
  70. </Dialog>
  71. </template>
  72. <script lang="ts" setup>
  73. import download from '@/utils/download'
  74. import {getAccessToken, getTenantId} from '@/utils/auth'
  75. import {SpecimenInfoApi} from "@/api/museums/specimeninfo";
  76. import {ref, nextTick} from 'vue';
  77. import {UploadFilled} from "@element-plus/icons-vue";
  78. import {useUpload} from "@/components/UploadFile/src/useUpload";
  79. defineOptions({name: 'SpecimenImportForm'})
  80. const loading = ref(true) // 列表的加载中
  81. const dialogVisible = ref(false) // 弹窗的是否展示
  82. const formLoading = ref(false) // 表单的加载中
  83. const uploadRef = ref()
  84. const uploadimageRef = ref()
  85. const message = useMessage() // 消息弹窗
  86. import axios from 'axios';
  87. const updateType = ref(-1)
  88. const updateSupport = ref(true) // 是否更新已经存在的用户数据
  89. const fileList = ref([]) // 文件列表
  90. const imageList = ref([])
  91. const uploadHeaders = ref() // 上传 Header 头
  92. const importUrl =
  93. import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/museums/specimen-info/import-specimen-with-images'
  94. /** 打开弹窗 */
  95. const open = (typeNum : any) => {
  96. loading.value = true
  97. dialogVisible.value = true
  98. updateSupport.value = true
  99. if(typeNum != null && typeNum != '' && typeNum != undefined){
  100. updateType.value = typeNum
  101. }else {
  102. updateType.value = -1
  103. }
  104. fileList.value = []
  105. imageList.value = []
  106. resetForm()
  107. }
  108. defineExpose({open}) // 提供 open 方法,用于打开弹窗
  109. const { httpRequest } = useUpload()
  110. /** 提交表单 */
  111. const submitForm = async () => {
  112. if (fileList.value.length == 0 ) {
  113. message.error('必须上传EXCEL文件')
  114. return
  115. }
  116. // 提交请求
  117. uploadHeaders.value = {
  118. Authorization: 'Bearer ' + getAccessToken(),
  119. 'tenant-id': getTenantId()
  120. }
  121. formLoading.value = true
  122. const formData = new FormData()
  123. if (fileList.value.length > 0) {
  124. formData.append('file', fileList.value[0].raw)
  125. }
  126. if (imageList.value.length > 0) {
  127. formData.append('imageFile', imageList.value[0].raw)
  128. }
  129. try {
  130. const response = await axios.post(importUrl + '?updateSupport=' + updateSupport.value
  131. + '&updateType=' + updateType.value, formData, {
  132. headers: {
  133. ...uploadHeaders.value,
  134. 'Content-Type': 'multipart/form-data'
  135. }
  136. });
  137. console.log(123)
  138. submitFormSuccess(response)
  139. console.log(1233333)
  140. } catch (error) {
  141. submitFormError()
  142. } finally {
  143. formLoading.value = false;
  144. }
  145. };
  146. /** 文件上传成功 */
  147. const emits = defineEmits(['success'])
  148. const submitFormSuccess = (response: any) => {
  149. console.log('上传响应:', response)
  150. // console.log(response.data.code != 0 , response.data.code)
  151. if (response.data.code != 0) {
  152. message.error(response.data.msg)
  153. return
  154. }
  155. if (response.data.code == -19) {
  156. message.error(response.data.msg)
  157. // formLoading.value = false
  158. return
  159. }
  160. // 拼接提示语
  161. const data = response.data.data
  162. // console.log(data)
  163. let text = '';
  164. if(data.createSpecimenNumbers != null){
  165. text += '上传成功数量:' + data.createSpecimenNumbers.length + ';'
  166. for (let specimenInfodata of data.createSpecimenNumbers) {
  167. text += '标本编号:' + specimenInfodata + '、'
  168. }
  169. }
  170. if(data.updateSpecimenNumbers != null){
  171. text += '。更新成功数量:' + data.updateSpecimenNumbers.length + ';'
  172. for (const specimenInfodata of data.updateSpecimenNumbers) {
  173. text += '标本编号:' + specimenInfodata + '、'
  174. }
  175. }
  176. console.log('上传响应:2', response)
  177. if(JSON.stringify(data.failureSpecimenNumbers) != '{}' &&data.failureSpecimenNumbers != null) {
  178. text += '。更新失败数量:' + Object.keys(data.failureSpecimenNumbers).length + ';'
  179. for (const specimenInfodata in data.failureSpecimenNumbers) {
  180. text += '< ' + specimenInfodata + ': ' + data.failureSpecimenNumbers[specimenInfodata] + ' >'
  181. }
  182. }
  183. console.log('上传响应:3', response)
  184. if(data.createSpecimenImages != null){
  185. text += '图片上传成功数量:' + data.createSpecimenImages.length + ';'
  186. for (let specimenInfodata of data.createSpecimenImages) {
  187. text += '图片名称:' + specimenInfodata + '、'
  188. }
  189. }
  190. console.log('上传响应:4', response)
  191. if(JSON.stringify(data.failureSpecimenImages) != '{}' && data.failureSpecimenImages != null) {
  192. text += '图片上传失败数量:' + Object.keys(data.failureSpecimenImages).length + ';'
  193. for (let specimenInfodata in data.failureSpecimenImages) {
  194. text += '< ' + specimenInfodata + ': ' + data.failureSpecimenImages[specimenInfodata] + ' >'
  195. }
  196. }
  197. console.log(555)
  198. message.alert(text)
  199. // formLoading.value = false
  200. dialogVisible.value = false
  201. // 发送操作成功的事件
  202. emits('success')
  203. formLoading.value = false
  204. }
  205. /** 上传错误提示 */
  206. const submitFormError = (): void => {
  207. message.error('上传失败,请您重新上传!')
  208. formLoading.value = false
  209. dialogVisible.value = false
  210. }
  211. /** 重置表单 */
  212. const resetForm = async (): Promise<void> => {
  213. // 重置上传状态和文件
  214. formLoading.value = false
  215. updateSupport.value = true
  216. await nextTick()
  217. uploadRef.value?.clearFiles()
  218. uploadimageRef.value?.clearFiles()
  219. }
  220. /** 文件数超出提示 */
  221. const handleExceed = (): void => {
  222. message.error('最多只能上传一个文件!')
  223. }
  224. /** 下载模板操作 */
  225. const importTemplate = async () => {
  226. const res = await SpecimenInfoApi.importSpecimenInfoTemplate(updateType.value)
  227. download.excel(res, '标本批量导入模版.xls')
  228. }
  229. /** 图片上传成功 */
  230. // const submitImageSuccess = (response: any) => {
  231. // console.log('tj',response)
  232. // if (response.data.code !== 0) {
  233. // message.error(response.msg)
  234. // formLoading.value = false
  235. //
  236. // return
  237. // }
  238. // formLoading.value = false
  239. // dialogVisible.value = false
  240. // // 发送操作成功的事件
  241. //
  242. // }
  243. </script>