import modeMap from '~/configs/mode-map' const dev = process.env.EVN // 是否是公共页面入口 function isPublicEntry(name) { return /^public-.+$/gi.test(name) } // 是否是模板页面入口 function isTemplateEntry(name) { return /^template-.+$/gi.test(name) } // 错误信息 function showError(error, options) { return error({ statusCode: options.statusCode || 500, message: (dev === 'production' ? '页面不存在' : options.message) || '页面不存在', }) } // 供应商与模板匹配 function isMatchTemplate(authUserId, prefix) { const mode = modeMap.find((item) => item.authUserId == authUserId) return mode && mode.prefix === prefix } // 供应商是否存在指定模板 function isFixedTemplate(authUserId) { const mode = modeMap.find((item) => item.authUserId == authUserId) return mode && true } // 修改路由 function replaceFullPath(fullPath) { return fullPath.replace(/^(\/\d+\/)([a-z\d]+)(.*)/gi, (match, $1, $2, $3) => { return $1 + 'app' + $3 }) } // 初始化页面信息 function initPageData(store, data) { // 保存页面路由前缀 store.commit('app/SET_ROUTE_PREFIX', `/${data.authUserId}/${data.prefix}`) // 保存供应商信息 store.commit('supplier/SET_SUPPLIER_INFO', data) } // 从模板入口进入 async function initTemplateEntry(context) { const { route, $http, error, store, redirect } = context try { // 模板不存在 let authUserId = parseInt(route.params.template) if (!authUserId) { showError(error, { statusCode: 500, message: '页面不存在' }) return } const res = await $http.api.fetchSupplierInfo({ authUserId }) const prefix = route.name.split('-')[1] // 未匹配模板 if (prefix === 'mode' || prefix === 'all') { const redirectPath = replaceFullPath(route.fullPath) redirect(redirectPath) return } if (dev !== 'development' || true) { // 默认模板 if (prefix === 'app') { if (isFixedTemplate(authUserId)) { showError(error, { statusCode: 500, message: '模板与供应商不匹配' }) return } } // 非默认模板 if (['ross', 'ldm', 'ph'].includes(prefix)) { // 供应商为指定模板 if (!isMatchTemplate(authUserId, prefix)) { showError(error, { statusCode: 500, message: '模板与供应商不匹配' }) return } } } res.data.prefix = prefix // console.log(res.data) initPageData(store, res.data) } catch (err) { console.log(err) showError(error, { statusCode: 500, message: err.message || err.msg }) } } // 公共入口进入 // async function initPublicEntry(context) {} export default function (context) { const name = context.route.name if (isPublicEntry(name)) return // initPublicEntry(context) if (isTemplateEntry(name)) return initTemplateEntry(context) console.log(123) context.error({ statusCode: 404, message: '页面不存在' }) }