routeInterceptor.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. let loginType = uni.getStorageSync('loginType'); //1:处理 2:报修
  13. //用遍历的方式分别为uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
  14. list.forEach(item => {
  15. uni.addInterceptor(item, {
  16. invoke(e) {
  17. // 调用前拦截
  18. const url = e.url.split('?')[0];
  19. // 报修人不可以访问除了登录页,绑定工号页,报修入口页的其他页面
  20. if (!repaireRouterList.includes(url) && !url.includes("repair") && loginUserStore.loginUser.user) {
  21. if(loginUserStore.loginUser.user.engineer == 0){
  22. if(loginType==1){
  23. uni.reLaunch({
  24. url: "/pages/repairEntrance/repairEntrance"
  25. })
  26. }else if(loginType==2){
  27. uni.reLaunch({
  28. url: "/pages/repair/home"
  29. })
  30. }
  31. return false
  32. }else{
  33. if(loginType==1){
  34. uni.reLaunch({
  35. url: "/pages/incidentList/incidentList"
  36. })
  37. }else if(loginType==2){
  38. uni.reLaunch({
  39. url: "/pages/repair/home"
  40. })
  41. }
  42. return false
  43. }
  44. }
  45. return true
  46. },
  47. fail(err) { // 失败回调拦截
  48. console.log(err);
  49. },
  50. })
  51. })