import { getToken } from "@/utils/auth";
import setting from "@/plugins/setting.plugins";

// 登录页面
const loginPage = "/pages/login";

// 页面白名单
const whiteList = [
  "/pages/login",//登录
  "/pages/register",//注册
  "/pages/serveConfig",//服务器配置
  "/pages/serveConfigSelect",//服务器配置
  "/pages/common/textview/index",//浏览文本
  "/pages/common/webview/index",
  "/pages/business/mhxf/unitInfoCollection/index",//单位信息采集
  "/pages/common/invoicing/index",//开票管理
  "/pages/common/success/index",//成功
  "/pages/common/phoneVerify/index",//手机号验证
  "/pages/business/fireIot/repairReport/index",//报修申请
  "/pages/business/fireIot/repairReport/record",//报修历史
  "/pages/common/evaluate/record",//服务评价
  "/pages/common/NFC/index",//NFC读取
  "/pages/common/appMessage/details",//消息详情
  "/pages/common/face/index",//人脸识别
];

// 检查地址白名单
function checkWhite(url) {
  const path = url.split("?")[0];
  return whiteList.indexOf(path) !== -1;
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
list.forEach((item) => {
  uni.addInterceptor(item, {
    invoke(to) {
      // #ifdef APP-PLUS
      setting.formatSize(); //获取缓存大小
      // #endif

      if (getToken()) {
        if (to.url === loginPage) {
          uni.reLaunch({ url: "/pages/index" });
        }
        return true;
      } else {

        if (checkWhite(to.url)) {
          return true;
        }
        uni.reLaunch({ url: loginPage });
        return false;
      }
    },
    fail(err) {
      console.log(err);
    },
  });
});