123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 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: '页面不存在' })
- resolve(err)
- })
- })
- }
- // 公共页页面入口
- function publickLinkInit({ route, error, redirect }) {
- const whiteList = ['/download']
- return new Promise((resovle) => {
- if (whiteList.indexOf(route.path) === -1) {
- error({ statusCode: 404, message: '页面不存在' })
- }
- resovle()
- })
- }
- export default function (context) {
- let hash = context.route.hash
- const query = getQueryObject(hash)
- const appId = query.appId
- if (appId) return oldLinkInit(context) // 老连接适配
- const authUserId = parseInt(context.route.params.template)
- if (authUserId) return newLinkInit(context) // 模板入口页面
- return publickLinkInit(context) // 公共页页面入口
- }
|