permission.js 836 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // create by wgy 0619
  2. import { getToken } from '@/utils/auth'
  3. // 登录页面
  4. const loginPage = "/pages/index/index"
  5. //const loginPage = "/pages/login"
  6. // 页面白名单
  7. const whiteList = [
  8. ]
  9. // 检查地址白名单
  10. function checkWhite(url) {
  11. const path = url.split('?')[0]
  12. return whiteList.indexOf(path) !== -1
  13. }
  14. // 页面跳转验证拦截器
  15. let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
  16. list.forEach(item => {
  17. uni.addInterceptor(item, {
  18. invoke(to) {
  19. if (getToken()) {
  20. if (to.url === loginPage) {
  21. uni.reLaunch({ url: "/" })
  22. }
  23. return true
  24. } else {
  25. if (checkWhite(to.url)) {
  26. return true
  27. }
  28. uni.reLaunch({ url: loginPage })
  29. return false
  30. }
  31. },
  32. fail(err) {
  33. console.log(err)
  34. }
  35. })
  36. })