import { getToken } from '@/utils/auth'

// 登录页面
const loginPageH5 = "/pages/login"
const loginPageDingDing = "/pages/loginAuthen/authenDingDing"
const loginPageWeChat = "/pages/loginAuthen/authenWeChat"

// 页面白名单   //页面进行跳转时，会判断是否是白名单页面，如何不是，不运行跳转
const whiteList = [
  '/pages/login', '/pages/loginAuthen/authenDingDing', '/pages/loginAuthen/authenWeChat', '/pages/common/webview/index','/pages/loginAuthen/loginDingDing','/pages/loginAuthen/loginWeChat','/pages/loginAuthen/authenDingDingAudit','/pages/loginAuthen/selectCompany'
]

// 检查地址白名单
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) {
      if (getToken()) {
        if (to.url === loginPageH5 || to.url === loginPageDingDing || to.url === loginPageWeChat) {
          uni.reLaunch({ url: "/" })
        }
        return true
      } else {
        if (checkWhite(to.url)) {
          return true
        }
        // uni.reLaunch({ url: loginPage })
        return false
      }
    },
    fail(err) {
      console.log(err)
    }
  })
})
