permission.js 904 B

1234567891011121314151617181920212223242526272829303132333435
  1. // 白名单
  2. const whiteList = ['pages/login/index']
  3. export default async function() {
  4. const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
  5. // 给uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
  6. list.forEach(item => {
  7. uni.addInterceptor(item, {
  8. invoke(e) {
  9. // 获取要跳转的页面路径
  10. const url = e.url.split('?')[0]
  11. // 判断当前窗口是白名单,如果是则不重定向路由
  12. var pass = false
  13. if (whiteList) {
  14. pass = whiteList.indexOf(url) != -1
  15. }
  16. // 不是白名单并且没有token
  17. if (!pass && !uni.getStorageSync('token')) {
  18. uni.showToast({
  19. title: '请先登录',
  20. icon: 'none'
  21. })
  22. uni.navigateTo({
  23. url: "/pages/login/index"
  24. })
  25. return false
  26. }
  27. return e
  28. },
  29. fail(err) { // 失败回调拦截
  30. console.log(err)
  31. }
  32. })
  33. })
  34. }