router.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {
  2. RouterMount,
  3. createRouter,
  4. runtimeQuit
  5. } from './js_sdk/uni-simple-router';
  6. import {
  7. BACK_URL
  8. } from './config/cachekey'
  9. import store from './store'
  10. import {
  11. silentLogin
  12. } from '@/api/app'
  13. import {
  14. getWxCode
  15. } from './utils/login'
  16. import Cache from './utils/cache'
  17. import wechath5 from './utils/wechath5'
  18. import {
  19. isWeixinClient
  20. } from './utils/tools'
  21. const scrollInfo = {};
  22. let first = null;
  23. const whiteList = ['register', 'login', 'forget_pwd']
  24. const router = createRouter({
  25. platform: process.env.VUE_APP_PLATFORM,
  26. APP: {
  27. animation: {}
  28. },
  29. h5: {
  30. scrollBehavior: (to, from, savedPosition) => {
  31. const XY = scrollInfo[to.name];
  32. if (XY) return XY;
  33. return {
  34. x: 0,
  35. y: 0
  36. };
  37. }
  38. },
  39. routerErrorEach: ({
  40. type,
  41. msg
  42. }) => {
  43. router.$lockStatus = false;
  44. // #ifdef APP-PLUS
  45. if (type === 3) {
  46. runtimeQuit();
  47. }
  48. // #endif
  49. },
  50. debugger: false,
  51. routes: [
  52. ...ROUTES,
  53. {
  54. path: '*',
  55. redirect: (to) => {
  56. return {
  57. name: '404'
  58. }
  59. }
  60. },
  61. ]
  62. });
  63. console.log(router)
  64. let count = 0;
  65. router.beforeEach((to, from, next) => {
  66. // #ifdef H5
  67. // tab页面的滚动缓存
  68. if (from.meta.keepScroll === true) {
  69. scrollInfo[from.name] = {
  70. x: window.scrollX,
  71. y: window.scrollY
  72. }
  73. }
  74. // #endif
  75. console.log(to, from, 'beforeEach---开始跳转')
  76. let index = whiteList.findIndex((item) => from.path.includes(item))
  77. if (index == -1 && !store.getters.token) {
  78. Cache.set(BACK_URL, from.fullPath)
  79. }
  80. if (to.meta.auth && !store.getters.token) {
  81. next('/pages/login/login');
  82. return
  83. } else {
  84. next()
  85. }
  86. });
  87. router.afterEach((to, from, next) => {
  88. // #ifdef H5
  89. // 添加定时器防止拿到的域名是上一个域名
  90. setTimeout(async () => {
  91. if (isWeixinClient()) {
  92. // jssdk配置
  93. await wechath5.config()
  94. // 分享配置
  95. if (to.path.includes('goods_details')) return
  96. store.dispatch('wxShare')
  97. }
  98. })
  99. // #endif
  100. console.log('跳转完成');
  101. });
  102. export {
  103. router,
  104. RouterMount
  105. }