CustomerImportForm.vue 4.8 KB

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