auth.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import axios from 'axios'
  2. import oldRoutes from '@/utils/old-routes'
  3. import { getQueryObject } from '~/utils'
  4. import modeMap from '~/configs/mode-map'
  5. const templateList = ['app', 'ross', 'ldm']
  6. const baseURL = process.env.BASE_URL + '/wx/auth/shop/info'
  7. const dev = process.env.NODE_ENV
  8. // 获取供应商信息1 通过authUserId获取
  9. function fetchSupplierInfo(params) {
  10. return axios.get(baseURL, { params })
  11. }
  12. // 旧链接初始化
  13. function oldLinkInit({ route, error, redirect }) {
  14. return new Promise((resolve, reject) => {
  15. let hash = route.hash
  16. const query = getQueryObject(hash)
  17. const appId = query.appId
  18. const index = hash.indexOf('?')
  19. hash = hash.slice(1, index)
  20. const oldRoute = oldRoutes.find((item) => hash === item.path)
  21. if (!oldRoute) {
  22. error({ statusCode: 404, message: '页面不存在' })
  23. reject()
  24. }
  25. fetchSupplierInfo({ appId })
  26. .then(({ data }) => {
  27. const res = data
  28. if (res.code === -1) {
  29. error({ statusCode: 404, message: '页面不存在' })
  30. }
  31. const path = `/${data.authUserId}` + oldRoute.redirect
  32. redirect(path)
  33. resolve()
  34. })
  35. .catch(() => {
  36. error({ statusCode: 404, message: '页面不存在' })
  37. resolve()
  38. })
  39. })
  40. }
  41. // 新链接初始化
  42. function newLinkInit({ route, error, redirect }) {
  43. return new Promise((resolve) => {
  44. const authUserId = parseInt(route.params.template)
  45. const mode = modeMap.find((mode) => mode.authUserId === authUserId)
  46. const prefixPath_app = `/${authUserId}/app`
  47. fetchSupplierInfo({ authUserId })
  48. .then(({ data }) => {
  49. if (dev === 'production') {
  50. const res = data
  51. const prefix = res.data.prefix
  52. const prefixPath = `/${authUserId}/${prefix}`
  53. // 获取供应商失败
  54. if (res.code === -1) {
  55. error({ statusCode: 404, message: '页面不存在' })
  56. }
  57. // 没有指定模板 && 模板不存在 && 不是app默认模板路由地址
  58. else if (
  59. !mode &&
  60. !templateList.includes(prefix) &&
  61. !route.fullPath.startsWith(prefixPath_app)
  62. ) {
  63. if (!route.fullPath.startsWith(prefixPath)) {
  64. error({ statusCode: 404, message: '页面不存在' })
  65. } else {
  66. const redirectPath = route.fullPath.replace(
  67. prefixPath,
  68. prefixPath_app
  69. )
  70. redirect(redirectPath)
  71. }
  72. }
  73. // 没有指定模板 && 是app默认模板路由地址
  74. else if (!mode && route.fullPath.startsWith(prefixPath_app)) {
  75. }
  76. // 模板未定义
  77. else if (!templateList.includes(prefix)) {
  78. error({ statusCode: 404, message: '页面不存在' })
  79. }
  80. // 模板与供应商不匹配
  81. else if (!route.fullPath.startsWith(prefixPath)) {
  82. error({ statusCode: 404, message: '页面不存在' })
  83. }
  84. }
  85. resolve()
  86. })
  87. .catch((err) => {
  88. error({ statusCode: 404, message: '页面不存在' })
  89. resolve(err)
  90. })
  91. })
  92. }
  93. // 公共页页面入口
  94. function publickLinkInit({ route, error, redirect }) {
  95. const whiteList = ['/download']
  96. return new Promise((resovle) => {
  97. if (whiteList.indexOf(route.path) === -1) {
  98. error({ statusCode: 404, message: '页面不存在' })
  99. }
  100. resovle()
  101. })
  102. }
  103. export default function (context) {
  104. let hash = context.route.hash
  105. const query = getQueryObject(hash)
  106. const appId = query.appId
  107. if (appId) return oldLinkInit(context) // 老连接适配
  108. const authUserId = parseInt(context.route.params.template)
  109. if (authUserId) return newLinkInit(context) // 模板入口页面
  110. return publickLinkInit(context) // 公共页页面入口
  111. }