router.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 {isWeixinClient} from './utils/tools'
  19. const scrollInfo = {};
  20. let first = null;
  21. const whiteList = ['register', 'login', 'forget_pwd']
  22. const router = createRouter({
  23. platform: process.env.VUE_APP_PLATFORM,
  24. APP: {
  25. animation: {}
  26. },
  27. h5: {
  28. scrollBehavior: (to, from, savedPosition) => {
  29. const XY = scrollInfo[to.name];
  30. if (XY) return XY;
  31. return {
  32. x: 0,
  33. y: 0
  34. };
  35. }
  36. },
  37. routerErrorEach: ({
  38. type,
  39. msg
  40. }) => {
  41. router.$lockStatus = false;
  42. // #ifdef APP-PLUS
  43. if (type === 3) {
  44. runtimeQuit();
  45. }
  46. // #endif
  47. },
  48. debugger: false,
  49. routes: [
  50. ...ROUTES,
  51. {
  52. path: '*',
  53. redirect: (to) => {
  54. return {
  55. name: '404'
  56. }
  57. }
  58. },
  59. ]
  60. });
  61. let count = 0;
  62. router.beforeEach((to, from, next) => {
  63. // #ifdef H5
  64. // tab页面的滚动缓存
  65. if (from.meta.keepScroll === true) {
  66. scrollInfo[from.name] = {
  67. x: window.scrollX,
  68. y: window.scrollY
  69. }
  70. }
  71. // #endif
  72. let index = whiteList.findIndex((item) => from.path.includes(item))
  73. if (index == -1 && !store.getters.token) {
  74. let fullPath = from.fullPath;
  75. if(from.path == '/pages/launch/launch') {
  76. fullPath = '/pages/index/index'
  77. }
  78. Cache.set(BACK_URL, 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. });
  101. export {
  102. router,
  103. RouterMount
  104. }