auth.js 878 B

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