auth.js 720 B

123456789101112131415161718192021222324252627282930313233
  1. // 引入vuex配置
  2. import store from '../store'
  3. // 页面白名单
  4. const whiteList = ['/pages/login/login']
  5. const funs = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'];
  6. function hasPermission(url) {
  7. console.log('跳转', url);
  8. // 在白名单中或有token,直接跳转
  9. if (whiteList.indexOf(url) !== -1 || store.state.login.loginInfo.user) {
  10. return true
  11. }
  12. return false
  13. }
  14. funs.forEach(v => {
  15. uni.addInterceptor(v, {
  16. // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
  17. invoke(e) {
  18. if (!hasPermission(e.url)) {
  19. uni.reLaunch({
  20. url: '/pages/login/login'
  21. })
  22. return false
  23. }
  24. return true
  25. },
  26. success(e) {
  27. // console.log(e)
  28. }
  29. })
  30. })