index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Router from 'uni-router-interceptor'
  2. const router = new Router({
  3. homePage: '/pages/home/index'
  4. })
  5. // 路由白名单
  6. const whiteList = [
  7. '/pages/login/index',
  8. '/pages/login/self',
  9. '/pages/login/sms',
  10. '/pages/login/setPwd',
  11. '/pages/login/register',
  12. '/pages/login/choose',
  13. '/pages_law/service',
  14. '/pages_law/privacy'
  15. ]
  16. router.beforeEach((to, from, next) => {
  17. console.log('router before', to, from)
  18. if (to.url.startsWith('/pages/login/index')) {
  19. const appid = uni.getStorageSync('appid') || null
  20. if (
  21. appid &&
  22. from &&
  23. !from.startsWith('pages/login/self')) {
  24. router.reLaunch('/pages/login/self')
  25. } else {
  26. next()
  27. }
  28. return
  29. }
  30. if (whiteList.includes(to.url) || (to.query && to.query.check == 0)) {
  31. next()
  32. return
  33. }
  34. const token = uni.getStorageSync('token') || null
  35. if (!token) {
  36. router.reLaunch('/pages/login/index')
  37. } else {
  38. next()
  39. }
  40. })
  41. router.afterEach((to, from) => {
  42. // console.log('router afterEach', to, from)
  43. })
  44. router.error(() => {
  45. if (process.env.NODE_ENV !== 'development') {
  46. router.reLaunch('/pages/home/index')
  47. }
  48. })
  49. export default router