fieldOptionProviders.js
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* 字段选项 HTTP 封装 — 在职员工列表等
* @see docs/features/packageDynamicForm/field-options-api.md
*
* fetchStoreStaffListOptions:api.getEmployeeList,仅传 merchantId(不传 sceneList)
* 驻店由 adapter 传入 yqMerchantId;门店/洽谈 storeStaffList 同;4sStaffList 传 4S shopMerchantId
*/
let employeeListApiModule = null
/** 懒加载 getEmployeeList API 模块 */
function getEmployeeListApi() {
if (!employeeListApiModule) {
employeeListApiModule = require('../../api/api.js')
}
return employeeListApiModule
}
/** 测试注入:覆盖 getEmployeeList API 模块 */
function __setEmployeeListApi(api) {
employeeListApiModule = api
}
/**
* 将员工列表映射为 { label, value } 选项
* @param {Array} [list=[]] getEmployeeList 返回的数据
* @returns {Array<{label: string, value: string}>}
*/
function mapEmployeeListToOptions(list = []) {
if (!Array.isArray(list)) {
return []
}
return list
.map(item => {
const value = item?.uuid ?? item?.staffUuid ?? item?.userId ?? ''
const label = item?.staffName ?? item?.name ?? item?.employeeName ?? ''
if (value === '' || label === '') {
return null
}
return { label: String(label), value: String(value) }
})
.filter(Boolean)
}
/**
* 拉取在职员工选项(onTheJob.getByMerchantId)
* @param {Object} [options={}]
* @param {string|number} options.merchantId 商户 ID(驻店为园区店,门店/洽谈为 4S 店)
* @returns {Promise<{code: number, message: string, data: Array}>}
*/
async function fetchStoreStaffListOptions({ merchantId } = {}) {
if (!merchantId) {
return { code: -1, message: '缺少门店信息', data: [] }
}
const params = { merchantId }
const result = await getEmployeeListApi().getEmployeeList(params)
const code = result?.code ?? -1
if (code != 0) {
return {
code: code ?? -1,
message: result?.message || '获取驻店人员失败',
data: []
}
}
return {
code: 0,
message: 'ok',
data: mapEmployeeListToOptions(result?.data)
}
}
module.exports = {
mapEmployeeListToOptions,
fetchStoreStaffListOptions,
__setEmployeeListApi
}