jumpHandlers.js 7.15 KB
/**
 * 精品首页菜单跳转 Command Dispatcher(按 item.id / action.type,无 name 回退)
 *
 * 点击时 shutDownTaskApi 副作用(taskId 不在 registry,见下表):
 *
 * | 触发方式 | 菜单 id / handler | taskId |
 * |----------|-------------------|--------|
 * | `SHUTDOWN_TASK_BY_ID` | `training_materials` | 5 |
 * | `marketingMaterials` handler | `marketing_materials` | 6(硬编码) |
 *
 * 详见 tabIndex/docs/MENU-ARCHITECTURE.md「跳转副作用」。
 */
import commonApi from '../../api/api.js'
import {
	checkDailyReport,
	checkStudyTask,
	checkAbnormalOrder
} from '../../store/ModalToQueueControl.js'
import { trackDynamicFormMenuEntry } from '../../utils/dynamicFormMenus.js'
import { resolveActionType } from './resolveActionType.js'
import { openWorkReportTemp } from './workReportHandler.js'
import { openOrderPromptWithRestrictState } from '../services/newOrderAbnormalFlowRestrict.service.js'

export { resolveActionType }

/** 使用 path + params 跳转、跳过员工锁单校验的菜单 id */
const ROUTER_PATH_PARAM_IDS = new Set([
	'script_extract',
	'script_verify',
	'missing_reception',
	'salary_appeal'
])

/** 跳过员工锁单校验、直接 router.go 的菜单 id */
const ROUTER_DIRECT_IDS = new Set([
	'pending_approval',
	'abnormal_flow_approval'
])

/** 点击前需 shutDownTaskApi 的菜单 id → taskId */
const SHUTDOWN_TASK_BY_ID = {
	training_materials: 5
}

function getAppSafe() {
	return typeof getApp === 'function' ? getApp() : null
}

/**
 * 创建页面绑定的菜单点击处理器
 * @param {Object} page - tabIndex 首页 Page 实例
 * @returns {Function}
 */
export function createMenuJumpHandler(page) {
	const handlers = {
		workReport: async (item, ctx) => {
			const { userInfo, mentorFlag } = ctx
			if (
				wx.$hasPermission?.canViewMentorship?.(userInfo) ||
				mentorFlag
			) {
				wx.$track.setTrackData(0, 'supervisor_daily_page', {}, '', 1)
			}
			openWorkReportTemp(page, item)
		},
		switchTab: async (item) => {
			wx.$util.pointRecord(item.recordId)
			if (item.isRepairOrderTabFromMenu) {
				const app = getAppSafe()
				if (app) app.globalData.repairOrderOpenFromHomeMenu = true
			}
			return wx.switchTab({ url: item.action?.target || item.url })
		},
		navigate: async (item, ctx) => {
			const { userInfo } = ctx
			const code = item.trackEventCode || item.action?.trackEventCode
			if (code === 'scenario_marketplace') {
				wx.$track.setTrackData(0, 'scenario_marketplace', {}, '', 1)
			}
			const url = item.navigateToUrl || item.action?.target
			trackDynamicFormMenuEntry(
				url,
				userInfo?.sceneType ?? 1,
				item.trackEventCode
			)
			return wx.navigateTo({ url })
		},
		salaryPerformance: async () => {
			return wx.$router.go('packageEquity.collegeScene', {
				reLaunchPath: 'packageEquity.equityPage',
				tabIndex: 0
			})
		},
		residentTarget: async (item, ctx) => {
			return wx.$router.go(item.url || item.action?.target, {
				id: ctx.taskId
			})
		},
		marketingMaterials: async (item, ctx) => {
			const { userInfo } = ctx
			commonApi.shutDownTaskApi({
				merchantId: userInfo?.default4SStoreInfo?.merchantId,
				staffUuid: userInfo?.uuid,
				scene: 1,
				taskId: 6
			})
			return wx.navigateTo({ url: item.url || item.action?.target })
		},
		miniProgram: async (item) => {
			const action = item.action || {}
			return wx.navigateToMiniProgram({
				appId: action.appId || 'wx2c28337db96364d2',
				path: action.path || 'pages/tab/index/index',
				envVersion: action.envVersion || 'trial',
				success() {}
			})
		},
		scanCode: async (item, ctx) => {
			return handleStaffGatedAction(page, item, ctx, async () => {
				if (await wx.$globalDataFunction.inspectionOfProhibitionBill()) return
				wx.scanCode({
					success(res) {
						page.showSelectType({ scene: res.result })
					}
				})
			})
		},
		router: async (item, ctx) => {
			const { userInfo } = ctx
			const target = item.path || item.action?.target || item.url
			const routerParams = item.params || item.action?.params || {}

			if (item.id === 'customer_clue' || target === 'packageEquity.clueList') {
				wx.$track.setTrackData(0, 'leads_page', {}, '', 1)
			}
			if (
				target === 'tabWorkOrder.mentorshipManage' &&
				wx.$hasPermission?.canViewMentorship?.(userInfo)
			) {
				wx.$track.setTrackData(0, 'mentorship_page', {}, '', 1)
			}

			const shutdownTaskId = SHUTDOWN_TASK_BY_ID[item.id]
			if (shutdownTaskId) {
				commonApi.shutDownTaskApi({
					merchantId: userInfo?.default4SStoreInfo?.merchantId,
					staffUuid: userInfo?.uuid,
					scene: userInfo?.sceneType ?? 1,
					taskId: shutdownTaskId
				})
			}

			if (ROUTER_DIRECT_IDS.has(item.id)) {
				return wx.$router.go(target)
			}

			if (ROUTER_PATH_PARAM_IDS.has(item.id) || item.path) {
				return wx.$router.go(target, { params: routerParams })
			}

			return handleStaffGatedAction(page, item, ctx, async () => {
				if (
					item.beforeClick &&
					(await wx.$globalDataFunction[item.beforeClick]())
				) {
					return
				}
				if (target === 'tabWorkOrder.createOrder') {
					await openOrderPromptWithRestrictState(page, { sceneObj: {} })
				} else {
					wx.$router.go(target, { params: routerParams })
				}
			})
		}
	}

	return async function dispatchMenuJump(e) {
		let { item, type } = e.currentTarget.dataset
		let { userInfo, taskId, mentorFlag } = page.data

		if (userInfo?.merchantAccountList?.length) {
			try {
				userInfo.merchantAccountList.map(acc => {
					if (acc == 6 || acc == 8) throw true
				})
			} catch (error) {
				userInfo.merchantType = error
			}
		}

		const ctx = { userInfo, type, taskId, mentorFlag }
		const actionType = resolveActionType(item)
		const handler = handlers[actionType] || handlers.router
		return handler(item, ctx)
	}
}

async function handleStaffGatedAction(page, item, ctx, actionFn) {
	const { userInfo, type } = ctx
	const isStaffMerchant =
		userInfo?.merchantType && !wx.$hasPermission?.isSuper?.(userInfo)

	if (isStaffMerchant && type == 2) {
		wx.$util.pointRecord(item.recordId)
		if (item.type === 'scanCode' || item.action?.type === 'scanCode') {
			let staffCheck = await checkDailyReport()
			if (staffCheck) return false
			let staffStudyTask = await checkStudyTask()
			if (staffStudyTask) return false
			let abnormalOrderCheck = await checkAbnormalOrder('pages.index')
			if (abnormalOrderCheck) return false
		} else if (item.type === 'mini' || item.action?.type === 'miniProgram') {
			return wx.navigateToMiniProgram({
				appId: 'wx2c28337db96364d2',
				path: 'pages/tab/index/index',
				envVersion: 'trial',
				success() {}
			})
		} else {
			if (!item.noCheckdailyReport) {
				let staffCheck = await checkDailyReport()
				if (staffCheck) return false
				let staffStudyTask = await checkStudyTask()
				if (staffStudyTask) return false
				let abnormalOrderCheck = await checkAbnormalOrder('pages.index')
				if (abnormalOrderCheck) return false
			}
		}
		return actionFn()
	}

	const target = item.url || item.action?.target
	if (
		![
			'tabWorkOrder.createOrder',
			'tabWorkOrder.createArchives',
			'tabWorkOrder.defeatOrder'
		].includes(target)
	) {
		wx.$util.pointRecord(item.recordId)
		return wx.$router.go(target, {
			scene: 1,
			params: item.params || item.action?.params || {}
		})
	}
	wx.$util.showNoAccess()
}