router.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // router.js
  2. import { RouterMount, createRouter, runtimeQuit } from '@/js_sdk/hhyang-uni-simple-router/uni-simple-router';
  3. import tools from "@/service/tools";
  4. const router = createRouter({
  5. platform: process.env.VUE_APP_PLATFORM,
  6. routerErrorEach:({type,level,...args})=>{
  7. // console.log({type,level,...args})
  8. // #ifdef APP-PLUS
  9. if(type===3){
  10. router.$lockStatus=false;
  11. tools.onBackPress()
  12. }
  13. // #endif
  14. },
  15. routes: [...ROUTES]
  16. });
  17. //全局路由前置守卫
  18. const whiteList = ['/pages/login/index','/pages/login/register']
  19. router.beforeEach(async (to, from, next) => {
  20. // tools.showLoading()
  21. console.log(to)
  22. console.log(from)
  23. if(to.path==='/pages/login/index'){
  24. uni.setStorageSync('pageFullPath',from.fullPath)
  25. }else if(from.path==='/pages/login/index'){
  26. let oldFullPath= uni.getStorageSync('pageFullPath')
  27. if(oldFullPath){
  28. uni.setStorageSync('pageFullPath','')
  29. next(oldFullPath);
  30. }
  31. }
  32. let userToken=''
  33. if (to.query.token ) {
  34. userToken = to.query.token;
  35. uni.setStorageSync('token',userToken)
  36. }else {
  37. userToken = uni.getStorageSync('token')
  38. }
  39. console.log('userToken:'+userToken)
  40. if(whiteList.indexOf(to.path)<0 && !userToken){
  41. console.log('----------------------')
  42. await tools.weiXinLogin().then(isLogin=>{
  43. console.log('-----------isLogin-----------')
  44. if(isLogin){
  45. next();
  46. // next('/pages/login/index');
  47. }else {
  48. next('/pages/login/index');
  49. }
  50. }).catch(e=>{
  51. next('/pages/login/index');
  52. })
  53. // next();
  54. }else {
  55. next();
  56. }
  57. });
  58. // 全局路由后置守卫
  59. router.afterEach((to, from) => {
  60. // console.log('后置守卫to------', to)
  61. // tools.hideLoading()
  62. })
  63. export {
  64. router,
  65. RouterMount
  66. }