permission.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { to as tos } from 'await-to-js';
  2. import router from './router';
  3. import NProgress from 'nprogress';
  4. import 'nprogress/nprogress.css';
  5. import { getToken } from '@/utils/auth';
  6. import { isHttp } from '@/utils/validate';
  7. import { isRelogin } from '@/utils/request';
  8. import useUserStore from '@/store/modules/user';
  9. import setPageTitle from "@/utils/set-page-title";
  10. import usePermissionStore from '@/store/modules/permission';
  11. NProgress.configure({ showSpinner: false });
  12. const whiteList = ['/login', '/yzy/callback', '/mplogin', '/signPage', '/signOK'];
  13. router.beforeEach(async (to, from, next) => {
  14. NProgress.start();
  15. if (getToken()) {
  16. to.meta.title && setPageTitle(to.meta.title);
  17. /* has token*/
  18. if ((to.path === '/login') || (to.path === '/mplogin')) {
  19. next({ path: '/' });
  20. NProgress.done();
  21. } else if (whiteList.indexOf(to.path as string) !== -1) {
  22. next();
  23. } else {
  24. if (useUserStore().roles.length === 0) {
  25. isRelogin.show = true;
  26. // 判断当前用户是否已拉取完user_info信息
  27. const [err] = await tos(useUserStore().getInfo());
  28. if (err) {
  29. await useUserStore().logout();
  30. ElMessage.error(err);
  31. next({ path: '/' });
  32. } else {
  33. isRelogin.show = false;
  34. const role = localStorage.getItem('role');
  35. const roles = useUserStore().roles;
  36. if(to.path === '/' || (!!role && roles.includes(role))) {
  37. next();
  38. } else if (!!role) {
  39. useUserStore().setRoles(role);
  40. next({ path: to.path, replace: true, params: to.params, query: to.query, hash: to.hash, name: to.name as string }); // hack方法 确保addRoutes已完成
  41. } else {
  42. // const accessRoutes = await usePermissionStore().generateRoutes();
  43. // // 根据roles权限生成可访问的路由表
  44. // accessRoutes.forEach((route) => {
  45. // if (!isHttp(route.path)) {
  46. // router.addRoute(route); // 动态添加可访问路由表
  47. // }
  48. // });
  49. next({ path: to.path, replace: true, params: to.params, query: to.query, hash: to.hash, name: to.name as string }); // hack方法 确保addRoutes已完成
  50. }
  51. }
  52. } else {
  53. next();
  54. }
  55. }
  56. } else {
  57. // 没有token
  58. if (whiteList.indexOf(to.path as string) !== -1) {
  59. // 在免登录白名单,直接进入
  60. next();
  61. } else {
  62. console.log('navigator.userAgent:', navigator.userAgent);
  63. // 密信浏览器
  64. if (/(MeSignBrowser)/i.test(navigator.userAgent)) {
  65. const redirect = encodeURIComponent(to.fullPath || '/');
  66. next(`/mplogin?redirect=${redirect}`); // 否则全部重定向到登录页
  67. NProgress.done();
  68. }
  69. else if (/(wxworklocal)/i.test(navigator.userAgent)) {
  70. const rnd = Math.floor(Math.random() * 900000) + 100000;
  71. const state = `F${rnd}`;
  72. const redirect_uri = encodeURIComponent(window.location.protocol + "//" + window.location.host + "/api/yzy/callback.html")
  73. let next_url = `https://open.weixin.qq.com/connect/Oauth2/authorize?appid=wl2bee594e73&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&agentid=1004000&state=${state}#wechat_redirect`;
  74. sessionStorage.setItem(state, to.fullPath || '/');
  75. window.location.href = next_url; // 粤政易自动登录
  76. }
  77. else {
  78. const redirect = encodeURIComponent(to.fullPath || '/');
  79. next(`/login?redirect=${redirect}`); // 否则全部重定向到登录页
  80. NProgress.done();
  81. }
  82. }
  83. }
  84. });
  85. router.afterEach(() => {
  86. NProgress.done();
  87. });