Browse Source

[Feat 0000] 将CAD处使用的模拟登录改为自动登录,优化自动登录模块代码

houzekong 7 months ago
parent
commit
f24a69f79b

+ 27 - 7
src/hooks/vent/useAutoLogin.ts

@@ -7,22 +7,35 @@ import { AxiosError } from 'axios';
 
 /** 自动登录功能的Hook,该Hook是为了部署在同一局域网内的多套系统之间能够无缝切换 */
 export function useAutoLogin() {
-  /** 启用自动登录功能来跳转新的页面 */
-  function open(url: string, target?: string) {
+  /** 获取自动登录的url */
+  function getUrl(baseUrl: string, extraQuery: Record<string, string> = {}) {
     const userStore = useUserStore();
     const qs = QueryString.stringify({
       [AUTO_LOGIN_URL_QUERY.key]: AUTO_LOGIN_URL_QUERY.val,
       realname: userStore.getUserInfo.realname,
       workNo: userStore.getUserInfo.workNo,
+      ...extraQuery,
     });
-    window.open(`${url}?${qs}`, target);
+    return `${baseUrl}?${qs}`;
+  }
+
+  /** 启用自动登录功能来跳转新的页面 */
+  function open(url: string, target?: string) {
+    window.open(getUrl(url), target);
+  }
+
+  /** 判断当前路由是否需要自动登录,本方法是同步方法,可以用于事先判断 */
+  function validateRoute(route: RouteLocationNormalized) {
+    if (route.query && route.query[AUTO_LOGIN_URL_QUERY.key] === AUTO_LOGIN_URL_QUERY.val) {
+      return true;
+    }
+
+    return false;
   }
 
   /** 用在路由守卫里,执行自动登录的逻辑,如果存在符合自动登录标准的query则执行自动登录,返回是否自动登录 */
   async function doAutoLogin(route: RouteLocationNormalized): Promise<void> {
-    if (!route.query) return;
-    // 这部分在外部的路由守卫里写过了
-    // if (route.query[AUTO_LOGIN_URL_QUERY.key] !== AUTO_LOGIN_URL_QUERY.val) return;
+    if (!validateRoute(route)) return;
 
     const { realname, workNo } = route.query;
     if (!realname || !workNo) return;
@@ -38,6 +51,9 @@ export function useAutoLogin() {
         ...params,
         goHome: false,
       });
+      delete route.query[AUTO_LOGIN_URL_QUERY.key];
+      delete route.query['username'];
+      delete route.query['workNo'];
       return;
     } catch (e) {
       const message = useMessage().createMessage;
@@ -47,9 +63,13 @@ export function useAutoLogin() {
   }
 
   return {
-    /** 启用单点登录功能来跳转新的页面,参数与window.open一致,但需要注意url需要指向登录页 */
+    /** 启用单点登录功能来跳转新的页面,参数与window.open一致 */
     open,
     /** 用在跳转到的页面上,执行单点登录的逻辑 */
     doAutoLogin,
+    /** 获取自动登录的url */
+    getUrl,
+    /** 判断当前路由是否需要自动登录,本方法是同步方法,可以用于事先判断 */
+    validateRoute,
   };
 }

+ 13 - 13
src/router/guard/permissionGuard.ts

@@ -41,7 +41,7 @@ const glob = useGlobSetting();
 export function createPermissionGuard(router: Router) {
   const userStore = useUserStoreWithOut();
   const permissionStore = usePermissionStoreWithOut();
-  const { doAutoLogin } = useAutoLogin();
+  const { doAutoLogin, validateRoute } = useAutoLogin();
 
   router.beforeEach(async (to, from, next) => {
     RootRoute.redirect = glob.homePath || PageEnum.BASE_HOME;
@@ -62,7 +62,7 @@ export function createPermissionGuard(router: Router) {
     }
 
     // 如果符合自动登录的相关条件则直接执行自动登录,覆盖原有的登录信息
-    if (to.query[AUTO_LOGIN_URL_QUERY.key] === AUTO_LOGIN_URL_QUERY.val) {
+    if (validateRoute(to)) {
       await doAutoLogin(to);
       // 自动登录后会动态添加路由,此处应当重定向到fullPath,否则会加载404页面内容
       return next({ path: to.fullPath, replace: true });
@@ -123,17 +123,17 @@ export function createPermissionGuard(router: Router) {
         // 如果需要重定向到sso页面则取消路由导航
         if (redirectSso) return;
       }
-      // query中要求自动登录的执行自动登录
-      if (to.query[AUTO_LOGIN_URL_QUERY.key] === AUTO_LOGIN_URL_QUERY.val) {
-        const userStore = useUserStoreWithOut();
-        await userStore.mockLogin({
-          goHome: false,
-        });
-        return next({
-          path: to.path,
-          query: to.query,
-        });
-      }
+      // @deprecated query中要求自动登录的执行自动登录
+      // if (to.query[AUTO_LOGIN_URL_QUERY.key] === AUTO_LOGIN_URL_QUERY.val) {
+      //   const userStore = useUserStoreWithOut();
+      //   await userStore.mockLogin({
+      //     goHome: false,
+      //   });
+      //   return next({
+      //     path: to.path,
+      //     query: to.query,
+      //   });
+      // }
 
       //update-begin---author:wangshuai ---date:20220629  for:[issues/I5BG1I]vue3 Auth2未实现------------
       let path = LOGIN_PATH;

+ 10 - 2
src/views/vent/performance/comment/CADModal.vue

@@ -35,9 +35,11 @@
   import { onMounted } from 'vue';
   import CADViewer from '/@/views/vent/performance/fileDetail/commen/CADViewer.vue';
   import { useGlobSetting } from '/@/hooks/setting';
-  import { MOCK_LOGIN_URL_QUERY, SKIP_SSO_URL_QUERY } from '/@/router/constant';
+  import { useAutoLogin } from '/@/hooks/vent/useAutoLogin';
+  import { MOCK_LOGIN_UESRNAME } from '/@/store/constant';
 
   const { sysOrgCode } = useGlobSetting();
+  const { getUrl } = useAutoLogin();
   // 不是布尔台的使用 mxcad 模式,是布尔台的使用 iframe 模式以避免“法律风险”
   const mxcadmode = ref(sysOrgCode !== 'sdmtjtbetmk');
 
@@ -60,7 +62,13 @@
       // 当以 iframe 模式运行时,origin 在生产模式下需要指向本项目公司端所部署的地址
       const origin = import.meta.env.PROD ? 'http://10.248.235.101:8092' : window.location.origin;
       // const origin = import.meta.env.DEV ? 'http://182.92.126.35:8092' : window.location.origin;
-      iframesrc.value = `${origin}/fileManager/cad-viewer?${SKIP_SSO_URL_QUERY.key}=${SKIP_SSO_URL_QUERY.val}&${MOCK_LOGIN_URL_QUERY.key}=${MOCK_LOGIN_URL_QUERY.val}&id=${record.id}&filename=${record.fileName}`;
+      iframesrc.value = getUrl(`${origin}/fileManager/cad-viewer`, {
+        // [SKIP_SSO_URL_QUERY.key]: SKIP_SSO_URL_QUERY.val,
+        id: record.id,
+        filename: record.fileName,
+        workNo: MOCK_LOGIN_UESRNAME,
+      });
+      // iframesrc.value = `${origin}/fileManager/cad-viewer?${SKIP_SSO_URL_QUERY.key}=${SKIP_SSO_URL_QUERY.val}&${MOCK_LOGIN_URL_QUERY.key}=${MOCK_LOGIN_URL_QUERY.val}&id=${record.id}&filename=${record.fileName}`;
     }
   });