routeInterceptor.js 1.0 KB

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