CustomerImportForm.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!-- 客户导入窗口 -->
  2. <template>
  3. <Dialog v-model="dialogVisible" title="客户导入" width="400">
  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. :on-success="submitFormSuccess"
  15. accept=".xlsx, .xls"
  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. defineOptions({ name: 'SystemUserImportForm' })
  49. const message = useMessage() // 消息弹窗
  50. const dialogVisible = ref(false) // 弹窗的是否展示
  51. const formLoading = ref(false) // 表单的加载中
  52. const uploadRef = ref()
  53. const importUrl =
  54. import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/crm/customer/import'
  55. const uploadHeaders = ref() // 上传 Header 头
  56. const fileList = ref([]) // 文件列表
  57. const updateSupport = ref(0) // 是否更新已经存在的客户数据
  58. /** 打开弹窗 */
  59. const open = () => {
  60. dialogVisible.value = true
  61. fileList.value = []
  62. resetForm()
  63. }
  64. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  65. /** 提交表单 */
  66. const submitForm = async () => {
  67. if (fileList.value.length == 0) {
  68. message.error('请上传文件')
  69. return
  70. }
  71. // 提交请求
  72. uploadHeaders.value = {
  73. Authorization: 'Bearer ' + getAccessToken(),
  74. 'tenant-id': getTenantId()
  75. }
  76. formLoading.value = true
  77. uploadRef.value!.submit()
  78. }
  79. /** 文件上传成功 */
  80. const emits = defineEmits(['success'])
  81. const submitFormSuccess = (response: any) => {
  82. if (response.code !== 0) {
  83. message.error(response.msg)
  84. formLoading.value = false
  85. return
  86. }
  87. // 拼接提示语
  88. const data = response.data
  89. let text = '上传成功数量:' + data.createCustomerNames.length + ';'
  90. for (let customerName of data.createCustomerNames) {
  91. text += '< ' + customerName + ' >'
  92. }
  93. text += '更新成功数量:' + data.updateCustomerNames.length + ';'
  94. for (const customerName of data.updateCustomerNames) {
  95. text += '< ' + customerName + ' >'
  96. }
  97. text += '更新失败数量:' + Object.keys(data.failureCustomerNames).length + ';'
  98. for (const customerName in data.failureCustomerNames) {
  99. text += '< ' + customerName + ': ' + data.failureCustomerNames[customerName] + ' >'
  100. }
  101. message.alert(text)
  102. // 发送操作成功的事件
  103. emits('success')
  104. }
  105. /** 上传错误提示 */
  106. const submitFormError = (): void => {
  107. message.error('上传失败,请您重新上传!')
  108. formLoading.value = false
  109. }
  110. /** 重置表单 */
  111. const resetForm = () => {
  112. // 重置上传状态和文件
  113. formLoading.value = false
  114. uploadRef.value?.clearFiles()
  115. }
  116. /** 文件数超出提示 */
  117. const handleExceed = (): void => {
  118. message.error('最多只能上传一个文件!')
  119. }
  120. /** 下载模板操作 */
  121. const importTemplate = async () => {
  122. const res = await CustomerApi.importCustomerTemplate()
  123. download.excel(res, '客户导入模版.xls')
  124. }
  125. </script>