router.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. import {userWxAppletLogin} from '@/api/user.js'
  5. const router = createRouter({
  6. platform: process.env.VUE_APP_PLATFORM,
  7. routerErrorEach:({type,level,...args})=>{
  8. // console.log({type,level,...args})
  9. // #ifdef APP-PLUS
  10. if(type===3){
  11. router.$lockStatus=false;
  12. tools.onBackPress()
  13. }
  14. // #endif
  15. },
  16. routes: [...ROUTES]
  17. });
  18. //全局路由前置守卫
  19. router.beforeEach((to, from, next) => {
  20. if(to.query.shopCode){
  21. uni.setStorageSync("invite_code", to.query.shopCode);
  22. }
  23. if(to.query.memberCode){
  24. uni.setStorageSync("memberCode", to.query.memberCode);
  25. }
  26. let isWeiXin = tools.isWx();
  27. if(isWeiXin){
  28. let userToken = uni.getStorageSync('token_str')
  29. if(userToken){
  30. next();
  31. }else{
  32. const href = window.location.href;
  33. if (href.indexOf('?code=') > -1 || href.indexOf('&code=') > -1){
  34. const urlArr = href.split('?')
  35. const rightUrlArr = urlArr[1].split('#/')
  36. const queryObj = {}
  37. rightUrlArr[0].split('&').map((item) => {
  38. const splitStr = item.split('=')
  39. return {
  40. key: splitStr[0],
  41. value: splitStr[1],
  42. }
  43. }).forEach((item) => {
  44. queryObj[item.key] = item.value
  45. })
  46. if(urlArr.length>=3){
  47. rightUrlArr[1]= "?"+urlArr[2]
  48. }
  49. let from = {
  50. code: queryObj.code,
  51. wx_type:1,
  52. invite_code:uni.getStorageSync("invite_code"),
  53. memberCode:uni.getStorageSync("memberCode"),
  54. }
  55. userWxAppletLogin(from).then((res)=>{
  56. if (res.code === 1) {
  57. uni.setStorageSync('userNmae',res.data.name)
  58. uni.setStorageSync('token_str',res.data.token)
  59. uni.setStorageSync('m_id',res.data.m_id)
  60. next(to.path)
  61. } else {
  62. tools.error(res.msg);
  63. }
  64. })
  65. }else{
  66. uni.removeStorageSync('token_str')
  67. uni.removeStorageSync('m_id')
  68. tools.wxLogin();
  69. }
  70. }
  71. }else{
  72. next()
  73. }
  74. });
  75. // 全局路由后置守卫
  76. router.afterEach((to, from) => {
  77. // console.log('后置守卫to------', to)
  78. })
  79. export {
  80. router,
  81. RouterMount
  82. }