import axios from 'axios' import oldRoutes from '@/utils/old-routes' import { getQueryObject } from '~/utils' import modeMap from '~/configs/mode-map' const templateList = ['app', 'ross', 'ldm'] const baseURL = process.env.BASE_URL + '/wx/auth/shop/info' const dev = process.env.NODE_ENV // 获取供应商信息1 通过authUserId获取 function fetchSupplierInfo(params) { return axios.get(baseURL, { params }) } // 旧链接初始化 function oldLinkInit({ route, error, redirect }) { return new Promise((resolve, reject) => { let hash = route.hash const query = getQueryObject(hash) const appId = query.appId const index = hash.indexOf('?') hash = hash.slice(1, index) const oldRoute = oldRoutes.find((item) => hash === item.path) if (!oldRoute) { error({ statusCode: 404, message: '页面不存在' }) reject() } fetchSupplierInfo({ appId }) .then(({ data }) => { const res = data if (res.code === -1) { error({ statusCode: 404, message: '页面不存在' }) } const path = `/${data.authUserId}` + oldRoute.redirect redirect(path) resolve() }) .catch(() => { error({ statusCode: 404, message: '页面不存在' }) resolve() }) }) } // 新链接初始化 function newLinkInit({ route, error, redirect }) { return new Promise((resolve) => { const authUserId = parseInt(route.params.template) const mode = modeMap.find((mode) => mode.authUserId === authUserId) const prefixPath_app = `/${authUserId}/app` fetchSupplierInfo({ authUserId }) .then(({ data }) => { if (dev === 'production') { const res = data const prefix = res.data.prefix const prefixPath = `/${authUserId}/${prefix}` // 获取供应商失败 if (res.code === -1) { error({ statusCode: 404, message: '页面不存在' }) } // 没有指定模板 && 模板不存在 && 不是app默认模板路由地址 else if ( !mode && !templateList.includes(prefix) && !route.fullPath.startsWith(prefixPath_app) ) { if (!route.fullPath.startsWith(prefixPath)) { error({ statusCode: 404, message: '页面不存在' }) } else { const redirectPath = route.fullPath.replace( prefixPath, prefixPath_app ) redirect(redirectPath) } } // 没有指定模板 && 是app默认模板路由地址 else if (!mode && route.fullPath.startsWith(prefixPath_app)) { } // 模板未定义 else if (!templateList.includes(prefix)) { error({ statusCode: 404, message: '页面不存在' }) } // 模板与供应商不匹配 else if (!route.fullPath.startsWith(prefixPath)) { error({ statusCode: 404, message: '页面不存在' }) } } resolve() }) .catch((err) => { error({ statusCode: 404, message: '页面不存在' }) console.log(err) }) }) } export default function (context) { return context.route.hash ? oldLinkInit(context) : newLinkInit(context) }