123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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]+)(.*)/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') {
- const redirectPath = replaceFullPath(route.fullPath)
- redirect(redirectPath)
- return
- }
- // 默认模板
- if (prefix === 'app') {
- if (isFixedTemplate(authUserId)) {
- showError(error, { statusCode: 500, message: '模板与供应商不匹配' })
- return
- }
- }
- // 非默认模板
- if (['ross', 'ldm'].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
- // console.log(name)
- if (isPublicEntry(name)) return // initPublicEntry(context)
- if (isTemplateEntry(name)) return initTemplateEntry(context)
- context.error({ statusCode: 404, message: '页面不存在' })
- }
|