permission.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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','/pages/weitiandi/bluetooth/status'
  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.showToast({
  29. icon:"error",
  30. title:"需要登录"
  31. })
  32. uni.reLaunch({ url: loginPage })
  33. return false
  34. }
  35. },
  36. fail(err) {
  37. console.log(err)
  38. }
  39. })
  40. })