import { useLoginUserStore } from '@/stores/loginUser'
const loginUserStore = useLoginUserStore()
// 白名单
let repaireRouterList = [
    "/pages/homePage/homePage", //登录页
    "/pages/initBind/initBind", //绑定工号
    "/pages/repairEntrance/repairEntrance", //报修入口页
		"/pages/repository/repository", //知识库
		"/pages/repositoryDetails/repositoryDetails", //知识库详情
  ]
  let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
	let loginType = uni.getStorageSync('loginType'); //1:处理 2:报修
  //用遍历的方式分别为uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
  list.forEach(item => {
    uni.addInterceptor(item, {
      invoke(e) {
        // 调用前拦截
        const url = e.url.split('?')[0];
        // 报修人不可以访问除了登录页,绑定工号页,报修入口页的其他页面
        if (!repaireRouterList.includes(url) && !url.includes("repair") && loginUserStore.loginUser.user && loginUserStore.loginUser.user
          .engineer !== 1) {
						if(loginType==1){
							uni.reLaunch({
							  url: "/pages/repairEntrance/repairEntrance"
							})
						}else if(loginType==2){
							uni.reLaunch({
							  url: "/pages/repair/home"
							})
						}
          return false
        }
        return true
      },
      fail(err) { // 失败回调拦截
        console.log(err);
      },
    })
  })