intercept.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import modeMap from '~/configs/mode-map'
  2. const dev = process.env.EVN
  3. // 是否是公共页面入口
  4. function isPublicEntry(name) {
  5. return /^public-.+$/gi.test(name)
  6. }
  7. // 是否是模板页面入口
  8. function isTemplateEntry(name) {
  9. return /^template-.+$/gi.test(name)
  10. }
  11. // 错误信息
  12. function showError(error, options) {
  13. return error({
  14. statusCode: options.statusCode || 500,
  15. message:
  16. (dev === 'production' ? '服务器出错' : options.message) || '服务器出错',
  17. })
  18. }
  19. // 供应商与模板匹配
  20. function isMatchTemplate(authUserId, prefix) {
  21. const mode = modeMap.find((item) => item.authUserId == authUserId)
  22. return mode && mode.prefix === prefix
  23. }
  24. // 供应商是否存在指定模板
  25. function isFixedTemplate(authUserId) {
  26. const mode = modeMap.find((item) => item.authUserId == authUserId)
  27. return mode && true
  28. }
  29. // 修改路由
  30. function replaceFullPath(fullPath) {
  31. return fullPath.replace(/^(\/\d+\/)([a-z]+)(.*)/gi, (match, $1, $2, $3) => {
  32. return $1 + 'app' + $3
  33. })
  34. }
  35. // 初始化页面信息
  36. function initPageData(store, data) {
  37. // 保存页面路由前缀
  38. store.commit('app/SET_ROUTE_PREFIX', `/${data.authUserId}/${data.prefix}`)
  39. // 保存供应商信息
  40. store.commit('supplier/SET_SUPPLIER_INFO', data)
  41. }
  42. // 从模板入口进入
  43. async function initTemplateEntry(context) {
  44. const { route, $http, error, store, redirect } = context
  45. try {
  46. // 模板不存在
  47. let authUserId = parseInt(route.params.template)
  48. if (!authUserId) {
  49. showError(error, { statusCode: 500, message: '页面不存在' })
  50. return
  51. }
  52. const res = await $http.api.fetchSupplierInfo({ authUserId })
  53. const prefix = route.name.split('-')[1]
  54. // 未匹配模板
  55. if (prefix === 'mode') {
  56. const redirectPath = replaceFullPath(route.fullPath)
  57. redirect(redirectPath)
  58. return
  59. }
  60. // 默认模板
  61. if (prefix === 'app') {
  62. if (isFixedTemplate(authUserId)) {
  63. showError(error, { statusCode: 500, message: '模板与供应商不匹配' })
  64. return
  65. }
  66. }
  67. // 非默认模板
  68. if (['ross', 'ldm'].includes(prefix)) {
  69. // 供应商为指定模板
  70. if (!isMatchTemplate(authUserId, prefix)) {
  71. showError(error, { statusCode: 500, message: '模板与供应商不匹配' })
  72. return
  73. }
  74. }
  75. res.data.prefix = prefix
  76. // console.log(res.data)
  77. initPageData(store, res.data)
  78. } catch (err) {
  79. console.log(err)
  80. showError(error, { statusCode: 500, message: err.message || err.msg })
  81. }
  82. }
  83. // 公共入口进入
  84. // async function initPublicEntry(context) {}
  85. export default function (context) {
  86. const name = context.route.name
  87. // console.log(name)
  88. if (isPublicEntry(name)) return // initPublicEntry(context)
  89. if (isTemplateEntry(name)) return initTemplateEntry(context)
  90. context.error({ statusCode: 404, message: '页面不存在' })
  91. }