toViewModel.js 1.87 KB
/**
 * 将注册表条目转为权益 WXML 兼容的 view model
 *
 * - `label` 映射为 `menuText`(模板 `item.menuText`)
 * - `action` 映射为 `path` / `navigateToUrl` / `params` 等过渡字段
 * - `extras` 合并 provider 运行时字段(noRead、tipsNum 等)
 *
 * router 类型写 `path` 的方式:
 * 1. `action.type` 为 `router` / `workReport` / `defeatOrder`
 * 2. `action.target` 为 packageEquity.extract / salaryAppeal / missingReception 时硬编码写 path
 * 3. `createOrderPrompt` 写空 `path`
 *
 * @param {Object} entry - registry 条目
 * @param {Object} [extras] - provider 附加字段
 * @returns {Object} 可直接 setData 的菜单项
 */
export function registryItemToViewModel(entry, extras = {}) {
	const {
		id,
		label,
		icon,
		section,
		sortVal,
		action,
		...rest
	} = entry

	const vm = {
		id,
		menuText: label,
		icon,
		section,
		sortVal,
		...rest,
		...extras
	}

	if (action) {
		vm.action = action
		if (
			action.type === 'router' ||
			action.type === 'workReport' ||
			action.type === 'defeatOrder'
		) {
			vm.path = action.target
		}
		if (action.type === 'createOrderPrompt') {
			vm.path = ''
		}
		if (action.type === 'router' && action.params) {
			vm.params = action.params
		}
		if (action.type === 'navigate') {
			vm.navigateToUrl = action.target
			if (action.trackEventCode) {
				vm.trackEventCode = action.trackEventCode
			}
		}
		if (action.type === 'residentTarget') {
			vm.path = action.target
		}
		if (action.type === 'salaryPerformance') {
			vm.path = action.target || ''
		}
		if (
			action.type === 'router' &&
			['packageEquity.extract', 'packageEquity.salaryAppeal', 'packageEquity.missingReception'].includes(
				action.target
			)
		) {
			vm.path = action.target
			if (action.params) vm.params = action.params
		}
		if (action.type === 'router' && action.usePath) {
			vm.path = action.target
		}
	}

	return vm
}