routeInterceptor.js 1.2 KB

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