CustomerImportForm.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!-- 客户导入窗口 -->
  2. <template>
  3. <Dialog v-model="dialogVisible" title="客户导入" width="400">
  4. <el-upload
  5. ref="uploadRef"
  6. v-model:file-list="fileList"
  7. :auto-upload="false"
  8. :disabled="formLoading"
  9. :headers="uploadHeaders"
  10. :limit="1"
  11. :on-error="submitFormError"
  12. :on-exceed="handleExceed"
  13. :on-success="submitFormSuccess"
  14. accept=".xlsx, .xls"
  15. action="none"
  16. drag
  17. >
  18. <Icon icon="ep:upload" />
  19. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  20. <template #tip>
  21. <div class="el-upload__tip text-center">
  22. <div class="el-upload__tip">
  23. <el-checkbox v-model="updateSupport" />
  24. 是否更新已经存在的客户数据(“客户名称”重复)
  25. </div>
  26. <span>仅允许导入 xls、xlsx 格式文件。</span>
  27. <el-link
  28. :underline="false"
  29. style="font-size: 12px; vertical-align: baseline"
  30. type="primary"
  31. @click="importTemplate"
  32. >
  33. 下载模板
  34. </el-link>
  35. </div>
  36. </template>
  37. </el-upload>
  38. <template #footer>
  39. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  40. <el-button @click="dialogVisible = false">取 消</el-button>
  41. </template>
  42. </Dialog>
  43. </template>
  44. <script lang="ts" setup>
  45. import * as CustomerApi from '@/api/crm/customer'
  46. import { getAccessToken, getTenantId } from '@/utils/auth'
  47. import download from '@/utils/download'
  48. import type { UploadUserFile } from 'element-plus'
  49. defineOptions({ name: 'SystemUserImportForm' })
  50. const message = useMessage() // 消息弹窗
  51. const dialogVisible = ref(false) // 弹窗的是否展示
  52. const formLoading = ref(false) // 表单的加载中
  53. const uploadRef = ref()
  54. const uploadHeaders = ref() // 上传 Header 头
  55. const fileList = ref<UploadUserFile[]>([]) // 文件列表
  56. const updateSupport = ref(false) // 是否更新已经存在的客户数据
  57. /** 打开弹窗 */
  58. const open = () => {
  59. dialogVisible.value = true
  60. fileList.value = []
  61. resetForm()
  62. }
  63. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  64. /** 提交表单 */
  65. const submitForm = async () => {
  66. if (fileList.value.length == 0) {
  67. message.error('请上传文件')
  68. return
  69. }
  70. // 提交请求
  71. uploadHeaders.value = {
  72. Authorization: 'Bearer ' + getAccessToken(),
  73. 'tenant-id': getTenantId()
  74. }
  75. formLoading.value = true
  76. const formData = new FormData()
  77. formData.append('updateSupport', updateSupport.value)
  78. formData.append('file', fileList.value[0].raw)
  79. // TODO @芋艿:后面是不是可以采用这种形式,去掉 uploadHeaders
  80. await CustomerApi.handleImport(formData)
  81. }
  82. /** 文件上传成功 */
  83. const emits = defineEmits(['success'])
  84. const submitFormSuccess = (response: any) => {
  85. if (response.code !== 0) {
  86. message.error(response.msg)
  87. formLoading.value = false
  88. return
  89. }
  90. // 拼接提示语
  91. const data = response.data
  92. let text = '上传成功数量:' + data.createCustomerNames.length + ';'
  93. for (let customerName of data.createCustomerNames) {
  94. text += '< ' + customerName + ' >'
  95. }
  96. text += '更新成功数量:' + data.updateCustomerNames.length + ';'
  97. for (const customerName of data.updateCustomerNames) {
  98. text += '< ' + customerName + ' >'
  99. }
  100. text += '更新失败数量:' + Object.keys(data.failureCustomerNames).length + ';'
  101. for (const customerName in data.failureCustomerNames) {
  102. text += '< ' + customerName + ': ' + data.failureCustomerNames[customerName] + ' >'
  103. }
  104. message.alert(text)
  105. // 发送操作成功的事件
  106. emits('success')
  107. }
  108. /** 上传错误提示 */
  109. const submitFormError = (): void => {
  110. message.error('上传失败,请您重新上传!')
  111. formLoading.value = false
  112. }
  113. /** 重置表单 */
  114. const resetForm = () => {
  115. // 重置上传状态和文件
  116. formLoading.value = false
  117. uploadRef.value?.clearFiles()
  118. }
  119. /** 文件数超出提示 */
  120. const handleExceed = (): void => {
  121. message.error('最多只能上传一个文件!')
  122. }
  123. /** 下载模板操作 */
  124. const importTemplate = async () => {
  125. const res = await CustomerApi.importCustomerTemplate()
  126. download.excel(res, '客户导入模版.xls')
  127. }
  128. </script>