permission.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import store from '@/store/index.js';
  2. import {Router} from '@/libs/router';
  3. // 页面白名单
  4. const whiteList = [
  5. '/pages/login/index',
  6. '/pages/index/index'
  7. ];
  8. /*设置白名单后台可以返回权限,然后再匹配路由地址*/
  9. let permission=['icon'];//权限数组示例---可以通过store登录的时候缓存
  10. for(let i=0;i<permission.length;i++){
  11. let permitName=permission[i];
  12. let path=Router[permitName].path;
  13. if(path){
  14. if(!whiteList.includes(path)){
  15. whiteList.push(path)
  16. }
  17. }
  18. }
  19. function hasPermission (url) {
  20. // 在白名单中或有token,直接跳转
  21. //whiteList.indexOf(url) !== -1 //使用白名单校验页面的时候使用
  22. if(store.state.access_token) {
  23. return true
  24. }
  25. return false
  26. }
  27. uni.addInterceptor('navigateTo', {
  28. // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
  29. invoke (e) {
  30. if(!hasPermission(e.url)){
  31. uni.reLaunch({
  32. url: '/pages/login/index'
  33. })
  34. return false
  35. }
  36. return true
  37. },
  38. success (e) {
  39. console.log(e)
  40. },
  41. fail(e) {
  42. console.log(e)
  43. }
  44. })
  45. uni.addInterceptor('switchTab', {
  46. // tabbar页面跳转前进行拦截
  47. invoke (e) {
  48. if(!hasPermission(e.url)){
  49. uni.reLaunch({
  50. url: '/pages/login/index'
  51. })
  52. return false
  53. }
  54. return true
  55. },
  56. success (e) {
  57. console.log(e)
  58. },
  59. fail(e) {
  60. console.log(e)
  61. }
  62. })