wlzboy
5 天以前 3bbd80a63ac7728ac01b641a48a26befcb171a0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { getToken } from '@/utils/auth'
 
// 登录页面
const loginPage = "/pages/login"
  
// 页面白名单
const whiteList = [
  '/pages/login', 
  '/pages/qylogin',  // 企业微信免登页面(匿名访问)
  '/pages/register', 
  '/pages/common/webview/index',
  '/pages/mine/privacy-policy/index',  // 隐私政策(匿名访问)
  '/pages/mine/user-agreement/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) {
      if (getToken()) {
        if (to.url === loginPage) {
          uni.reLaunch({ url: "/" })
        }
        return true
      } else {
        if (checkWhite(to.url)) {
          return true
        }
        uni.reLaunch({ url: loginPage })
        return false
      }
    },
    fail(err) {
      console.log(err)
    }
  })
})