router.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. let userToken=''
  22. if (to.query.token ) {
  23. userToken = to.query.token;
  24. uni.setStorageSync('token',userToken)
  25. }else {
  26. userToken = uni.getStorageSync('token')
  27. }
  28. console.log('userToken:'+userToken)
  29. if(whiteList.indexOf(to.path)<0 && !userToken){
  30. console.log('----------------------')
  31. await tools.weiXinLogin().then(isLogin=>{
  32. console.log('-----------isLogin-----------')
  33. if(isLogin){
  34. next();
  35. }else {
  36. next('/pages/login/index');
  37. }
  38. }).catch(e=>{
  39. next('/pages/login/index');
  40. })
  41. // next();
  42. }else {
  43. next();
  44. }
  45. });
  46. // 全局路由后置守卫
  47. router.afterEach((to, from) => {
  48. // console.log('后置守卫to------', to)
  49. // tools.hideLoading()
  50. })
  51. export {
  52. router,
  53. RouterMount
  54. }