Skip to content

Commit 26daa3a

Browse files
author
puhui999
committed
CRM-客户:完善客户导入
1 parent 64cfcbf commit 26daa3a

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

src/api/crm/customer/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export const exportCustomer = async (params: any) => {
6363
return await request.download({ url: `/crm/customer/export-excel`, params })
6464
}
6565

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

src/views/crm/customer/index.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
<Icon class="mr-5px" icon="ep:plus" />
8585
新增
8686
</el-button>
87+
<el-button v-hasPermi="['crm:customer:import']" plain type="warning" @click="handleImport">
88+
<Icon icon="ep:upload" />
89+
导入
90+
</el-button>
8791
<el-button
8892
v-hasPermi="['crm:customer:export']"
8993
:loading="exportLoading"
@@ -204,6 +208,7 @@
204208

205209
<!-- 表单弹窗:添加/修改 -->
206210
<CustomerForm ref="formRef" @success="getList" />
211+
<CustomerImportForm ref="customerImportFormRef" @success="getList" />
207212
</template>
208213

209214
<script lang="ts" setup>
@@ -212,6 +217,7 @@ import { dateFormatter } from '@/utils/formatTime'
212217
import download from '@/utils/download'
213218
import * as CustomerApi from '@/api/crm/customer'
214219
import CustomerForm from './CustomerForm.vue'
220+
import CustomerImportForm from './CustomerImportForm.vue'
215221
import { TabsPaneContext } from 'element-plus'
216222
217223
defineOptions({ name: 'CrmCustomer' })
@@ -333,7 +339,10 @@ const handleDelete = async (id: number) => {
333339
await getList()
334340
} catch {}
335341
}
336-
342+
const customerImportFormRef = ref<InstanceType<typeof CustomerImportForm>>()
343+
const handleImport = () => {
344+
customerImportFormRef.value?.open()
345+
}
337346
/** 导出按钮操作 */
338347
const handleExport = async () => {
339348
try {

src/views/system/user/UserImportForm.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const updateSupport = ref(0) // 是否更新已经存在的用户数据
6161
/** 打开弹窗 */
6262
const open = () => {
6363
dialogVisible.value = true
64+
fileList.value = []
6465
resetForm()
6566
}
6667
defineExpose({ open }) // 提供 open 方法,用于打开弹窗

0 commit comments

Comments
 (0)