permission.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import router from "@/router";
  2. import NProgress from "@/utils/nprogress";
  3. import { useDictionaryStore } from "@/store/modules/dictionary";
  4. import { getUserDicts } from "@/api/auth";
  5. import { isNewVersion } from "@/utils/version.js";
  6. export function setupPermission() {
  7. // 白名单路由
  8. const whiteList: string[] = ["client-traceability"]; //由于包含动态路由,这里面存储的是路由的name而不是path
  9. router.beforeEach(async (to, from, next) => {
  10. isNewVersion();
  11. NProgress.start();
  12. //在路由跳转前判断是否为不需要全屏加载的二级路由界面(目前项目中只有pro-step界面涉及)
  13. const isShow = inject("isShow");
  14. const route = useRoute();
  15. if (
  16. !(
  17. route.fullPath.substr(0, 11) == "/pro-steps/" &&
  18. to.fullPath.substr(0, 11) == "/pro-steps/"
  19. )
  20. ) {
  21. //@ts-ignore
  22. isShow.value = true;
  23. }
  24. const hasToken = localStorage.getItem("token");
  25. if (hasToken) {
  26. if (to.path === "/login") {
  27. // 如果已登录,跳转首页,并且清空token
  28. localStorage.setItem("token", "");
  29. next();
  30. // next({ path: "/" });
  31. NProgress.done();
  32. } else {
  33. const dictStore = useDictionaryStore();
  34. dictStore.checkAllData();
  35. if (
  36. !dictStore.dicts.value ||
  37. JSON.stringify(dictStore.dicts.value) === "{}"
  38. ) {
  39. const res = await getUserDicts(dictStore.types);
  40. if (res.data) {
  41. dictStore.dicts = res?.data ?? [];
  42. }
  43. }
  44. next();
  45. NProgress.done();
  46. }
  47. } else {
  48. // 未登录可以访问白名单页面
  49. if (whiteList.indexOf(to.name as string) !== -1) {
  50. const dictStore = useDictionaryStore();
  51. dictStore.checkAllData();
  52. next();
  53. } else {
  54. if (to.path === "/login") {
  55. next();
  56. } else {
  57. next(`/login`);
  58. }
  59. NProgress.done();
  60. }
  61. }
  62. });
  63. router.afterEach(() => {
  64. const isShow = inject("isShow");
  65. const route = useRoute();
  66. //@ts-ignore
  67. if (isShow.value == true) {
  68. //@ts-ignore
  69. isShow.value = false;
  70. }
  71. NProgress.done();
  72. });
  73. }