router.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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===-1){
  45. next('/pages/login/register');
  46. }else if(isLogin){
  47. next();
  48. }else {
  49. next('/pages/login/index');
  50. }
  51. }).catch(e=>{
  52. next('/pages/login/index');
  53. })
  54. // next();
  55. }else {
  56. next();
  57. }
  58. });
  59. // 全局路由后置守卫
  60. router.afterEach((to, from) => {
  61. // console.log('后置守卫to------', to)
  62. // tools.hideLoading()
  63. })
  64. export {
  65. router,
  66. RouterMount
  67. }