staffDailyReportNavigation.js 2.01 KB
import {
	fetchDailyReportNewVersionMenuVisible,
	resolveShopMerchantId
} from './dailyReportNewVersionGate.js'
import {
	buildStoreDailyReportNavigateUrl,
	canShowStaffDailyNew
} from './dynamicFormMenus.js'

export function resolveLegacyStaffDailyRoute(userInfo = {}) {
	if (userInfo.sceneType === 0) {
		return {
			type: 'router',
			route: 'tabWorkOrder.staffDaily',
			params: { scene: 0 }
		}
	}
	return {
		type: 'router',
		route: 'tabWorkOrder.upgradeStaffDaily',
		params: {}
	}
}

export function buildStoreDailyReportUrlWithDate(reportDate) {
	const base = buildStoreDailyReportNavigateUrl()
	if (!reportDate) {
		return base
	}
	const sep = base.includes('?') ? '&' : '?'
	return `${base}${sep}reportDate=${encodeURIComponent(reportDate)}`
}

/**
 * 是否跳转新版驻店日报(白名单 + 角色门禁)
 * @param {Object} [userInfo]
 * @returns {Promise<boolean>}
 */
export async function shouldUseNewStaffDailyReport(userInfo = {}) {
	if (!canShowStaffDailyNew(userInfo)) {
		return false
	}
	if (wx?.$hasPermission?.canSelectMultiStore?.(userInfo)) {
		return false
	}
	const merchantId = resolveShopMerchantId(userInfo)
	if (!merchantId) {
		return false
	}
	return fetchDailyReportNewVersionMenuVisible(merchantId)
}

/**
 * 按白名单跳转新/老驻店日报
 * @param {Object} [options]
 * @param {Object} [options.userInfo]
 * @param {string} [options.reportDate] yyyy-MM-dd
 */
export async function navigateToRequiredDailyReport(options = {}) {
	const info = options.userInfo || wx.$util?.getUserInfo?.() || {}
	if (await shouldUseNewStaffDailyReport(info)) {
		const url = buildStoreDailyReportUrlWithDate(options.reportDate)
		wx.navigateTo({ url })
		return { type: 'navigate', url }
	}
	const legacy = resolveLegacyStaffDailyRoute(info)
	const params = { ...(legacy.params || {}) }
	if (options.reportDate) {
		params.reportDate = options.reportDate
	}
	wx.$router.go(legacy.route, params)
	return { type: 'router', route: legacy.route, params }
}

export const navigateToStaffDailyReport = navigateToRequiredDailyReport