fieldOptionProviders.js 2.09 KB
/**
 * 字段选项 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
}