index.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import Vue from "vue";
  2. import VueRouter from "vue-router";
  3. import layout from "@/layout/index.vue";
  4. import { checkWxWorkEnvAndUserCache } from '@/utils/index'
  5. import { checkLoginStatus, initGuidInfo, doWecomLogin, getQyCode } from '@/utils/wecomLogin.ts';
  6. Vue.use(VueRouter);
  7. const router = new VueRouter({
  8. mode: process.env.NODE_ENV !== "development" ? "history" : "history",
  9. base: process.env.BASE_URL,
  10. scrollBehavior(to, from, savedPosition) {
  11. if (savedPosition) {
  12. return savedPosition;
  13. } else {
  14. return { x: 0, y: 0 };
  15. }
  16. },
  17. routes: [
  18. {
  19. path: "/",
  20. component: layout,
  21. redirect: "/AIDesign",
  22. children: [
  23. {
  24. path: "/AIDesign",
  25. component: () => import("../views/AIDesign/index.vue"),
  26. meta: { requiresAuth: true } // 标记需要登录的页面
  27. },
  28. {
  29. path: "/login",
  30. component: () => import("../views/login/index.vue"),
  31. meta: { title: "登录" }
  32. },
  33. {
  34. path: "/error/:code",
  35. component: () => import("../views/errorPage/index.vue")
  36. },
  37. {
  38. path: "/error",
  39. component: () => import("../views/errorPage/weixinerr.vue")
  40. },
  41. //设计页
  42. {
  43. path: "/AIDesign/design",
  44. component: () => import("../views/AIDesign/design.vue"),
  45. meta: { requiresAuth: true } // 标记需要登录的页面
  46. },
  47. //结果页
  48. {
  49. path: "/AIDesign/result",
  50. component: () => import("../views/AIDesign/result.vue"),
  51. meta: { requiresAuth: true } // 标记需要登录的页面
  52. },
  53. //历史页
  54. {
  55. path: "/AIDesign/history",
  56. component: () => import("../views/AIDesign/history.vue"),
  57. meta: { requiresAuth: true } // 标记需要登录的页面
  58. },
  59. //内墙-设计页
  60. {
  61. path: "/AIDesign/insideDesign",
  62. component: () => import("../views/AIDesign/insideDesign.vue"),
  63. meta: { requiresAuth: true } // 标记需要登录的页面
  64. },
  65. //一键诊断-生成页
  66. {
  67. path: "/AIDesign/diagnose",
  68. component: () => import("../views/AIDesign/diagnose.vue"),
  69. meta: { requiresAuth: true } // 标记需要登录的页面
  70. },
  71. //一键诊断-历史页
  72. {
  73. path: "/AIDesign/diagnoseHistory",
  74. component: () => import("../views/AIDesign/diagnoseHistory.vue"),
  75. meta: { requiresAuth: true } // 标记需要登录的页面
  76. },
  77. //一键诊断-结果页
  78. {
  79. path: "/AIDesign/diagnoseResult",
  80. component: () => import("../views/AIDesign/diagnoseResult.vue"),
  81. meta: { requiresAuth: true } // 标记需要登录的页面
  82. },
  83. // 生成方案
  84. {
  85. path: "/AIDesign/GeneratePlan",
  86. component: () => import("../views/AIDesign/GeneratePlan.vue"),
  87. meta: { requiresAuth: true }
  88. },
  89. ]
  90. },
  91. ]
  92. });
  93. // 全局前置守卫:只对需要登录的页面进行登录校验
  94. router.beforeEach((to, from, next) => {
  95. // console.log("-------页面链接=", window.location.href)
  96. if (!to.meta || (to.meta && !to.meta.requiresAuth)) {
  97. next();
  98. return;
  99. }
  100. try {
  101. checkWxWorkEnvAndUserCache();//先判断企微环境
  102. initGuidInfo();
  103. const isLoggedIn = checkLoginStatus();
  104. if (isLoggedIn) {
  105. next();
  106. return;
  107. }
  108. const code = to.query.code as string;
  109. let finalCode;
  110. if (Array.isArray(code)) {
  111. finalCode = code.length > 0 ? code[code.length - 1] : null;
  112. } else {
  113. finalCode = code || null;
  114. }
  115. if (finalCode) {
  116. doWecomLogin(finalCode)
  117. .then(() => {
  118. next();
  119. console.log(`登录成功,即将进入 ${to.path}`);
  120. })
  121. .catch(() => {
  122. next({ path: "/error" });
  123. });
  124. } else {
  125. console.log(`访问 ${to.path} 需登录,正在跳转到授权页面`);
  126. getQyCode();
  127. }
  128. } catch (error) {
  129. console.error("登录校验过程异常:", error);
  130. next({ path: "/error" });
  131. }
  132. });
  133. export default router;