routeInterceptor.js 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import { useLoginUserStore } from '@/stores/loginUser'
  2. const loginUserStore = useLoginUserStore()
  3. let repaireRouterList = [
  4. "/pages/homePage/homePage", //登录页
  5. "/pages/initBind/initBind", //绑定工号
  6. "/pages/repairEntrance/repairEntrance", //报修入口页
  7. ]
  8. let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
  9. //用遍历的方式分别为uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
  10. list.forEach(item => {
  11. uni.addInterceptor(item, {
  12. invoke(e) {
  13. // 调用前拦截
  14. const url = e.url.split('?')[0];
  15. // 报修人不可以访问除了登录页,绑定工号页,报修入口页的其他页面
  16. if (!repaireRouterList.includes(url) && loginUserStore.loginUser.user && loginUserStore.loginUser.user
  17. .engineer !== 1) {
  18. uni.reLaunch({
  19. url: "/pages/repairEntrance/repairEntrance"
  20. })
  21. return false
  22. }
  23. return true
  24. },
  25. fail(err) { // 失败回调拦截
  26. console.log(err);
  27. },
  28. })
  29. })