123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { getToken } from '@/utils/auth'
- const whiteList = [
- '/',
- { pattern: /^\/pages\/list.*/ },
- '/pages/home/home',
- '/pages/logo/index',
- '/pages/login/login',
- { pattern: /^\/pages\/login\/*/ }
- ]
- export default async function() {
- const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
-
- list.forEach(item => {
- uni.addInterceptor(item, {
- invoke(e) {
-
- const url = e.url.split('?')[0]
-
-
- let pass
- if (whiteList) {
- pass = whiteList.some((item) => {
- if (typeof (item) === 'object' && item.pattern) {
- return item.pattern.test(url)
- }
- return url === item
- })
- }
-
- if (!pass && !getToken()) {
- uni.showToast({
- title: '请先登录',
- icon: 'none'
- })
- setTimeout(()=>{
- uni.navigateTo({
- url: "/pages/logo/index"
- })
- },1000)
- return false
- }
- return e
- },
- fail(err) {
- console.log(err)
- }
- })
- })
- }
|