permission.js 919 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { getToken } from '@/utils/auth'
  2. // 登录页面
  3. const loginPage = "/pages/login"
  4. // 页面白名单
  5. const whiteList = [
  6. '/pages/login', '/pages/common/webview/index','/pages/bluetooth/index/index','/pages/weitiandi/bluetooth/index',
  7. '/pages/weitiandi/bluetooth/setting'
  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. })