You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
870 B
30 lines
870 B
import store from './store' |
|
import { getToken } from './common/auth' |
|
|
|
const routeInterceptor = () => { |
|
console.log('routeInterceptor') |
|
const methodToPatch = ["navigateTo", "redirectTo", "switchTab", "navigateBack"]; |
|
methodToPatch.map((type) => { |
|
// 通过遍历的方式分别取出,uni.navigateTo、uni.redirectTo、uni.switchTab、uni.navigateBack |
|
// 并且对相应的方法做重写 |
|
const original = uni[type]; |
|
uni[type] = function (options = {}) { |
|
if (options.url === '/pages/login/index') { |
|
return original.call(this, options); |
|
} |
|
if (!getToken()) { |
|
// 判断是否存在token,不存在重定向到登录页 |
|
uni.navigateTo({ |
|
url: "/pages/login/index", |
|
}); |
|
return |
|
} |
|
if (!store.state.user.id) { |
|
store.commit('setUser') |
|
} |
|
return original.call(this, options); |
|
}; |
|
}); |
|
} |
|
|
|
routeInterceptor() |