Jelajahi Sumber

配置文件中增加proxy地址,增加一网协同登录获取信息,增加跳转至一张图2024的拼接地址逻辑

wdq 1 bulan lalu
induk
melakukan
27bc3e0c3b
7 mengubah file dengan 173 tambahan dan 24 penghapusan
  1. 4 1
      public/static/config/config.js
  2. 115 8
      src/App.vue
  3. 7 0
      src/api/common.js
  4. 19 10
      src/components/user/user.vue
  5. 12 0
      src/store/index.js
  6. 9 5
      src/views/HomePage.vue
  7. 7 0
      vue.config.js

+ 4 - 1
public/static/config/config.js

@@ -1,8 +1,9 @@
 // 部署环境【互联网、政务网】
-const serverType = "互联网";
+const serverType = "政务网";
 var serverPath = serverType == "互联网" ? "http://121.43.55.7" : "http://10.235.245.174"; // 政
 let systemConfig = {
   /* 通用全局变量 */
+  serverType: serverType,
   defaultAccount: {
     username: "user_yztmh_dev", // 默认游客用户名
     adminRoleId: "1", //默认管理员角色id,Oauth中配置“系统管理员”角色
@@ -55,6 +56,8 @@ let systemConfig = {
   // oauth地址
   // oauthServiceUrl: "/proxy_oauth",
   oauthServiceUrl: serverPath + ":" + (serverType == "互联网" ? "10086" : "8888") + "/oauth", // App里使用  互 10086  政 8888
+  // 第三方登录代理地址:根据地址栏 ticket 获取用户信息
+  proxyUrl: serverPath + ":" + (serverType == "互联网" ? "10011" : "10011") + "/proxy",
   // oauth前端地址
   oauthWebUrlPort: "2100", // 运行管理中使用    无需动
   // dms前端地址

+ 115 - 8
src/App.vue

@@ -23,17 +23,23 @@ export default {
     //   this.scrollUpdate();
     // });
     let that = this;
+    // ticket 校验不依赖系统登录,有参数立即请求
+    that.fetchTicketUser();
+    this.$router.isReady().then(() => {
+      that.fetchTicketUser();
+    });
     let sessionUserInfo = sessionStorage.getItem("sessionUserInfo");
+    const afterLogin = () => {
+      // 登录成功之后要批量加载一下DMS的字典,后续全局可调用
+      that.getDMSTypesData();
+    };
     // console.log(sessionUserInfo);
     if (sessionUserInfo && sessionUserInfo !== "") {
       let user = JSON.parse(sessionUserInfo);
       // console.log(user);
       // 刷新页面登录
       encrypt‌(user)
-        .then(() => {
-          // 登录成功之后要批量加载一下DMS的字典,后续全局可调用
-          that.getDMSTypesData();
-        })
+        .then(afterLogin)
         .catch((err) => {
           that.$message({
             type: "error",
@@ -43,10 +49,7 @@ export default {
     } else {
       // 默认登录
       encrypt‌()
-        .then(() => {
-          // 登录成功之后要批量加载一下DMS的字典,后续全局可调用
-          that.getDMSTypesData();
-        })
+        .then(afterLogin)
         .catch((err) => {
           that.$message({
             type: "error",
@@ -55,7 +58,111 @@ export default {
         });
     }
   },
+  watch: {
+    "$route.query.ticket"() {
+      this.fetchTicketUser();
+    },
+  },
   methods: {
+    // 从地址栏或会话中读取第三方 ticket
+    getUrlTicket() {
+      const query = (this.$route && this.$route.query) || {};
+      const routeTicket = query.ticket || query.Ticket;
+      if (routeTicket) {
+        return Array.isArray(routeTicket) ? routeTicket[0] : routeTicket;
+      }
+      const search = window.location.search || "";
+      const hash = window.location.hash || "";
+      const searchParams = new URLSearchParams(search);
+      const fromSearch = searchParams.get("ticket") || searchParams.get("Ticket");
+      if (fromSearch) {
+        return fromSearch;
+      }
+      const hashQuery = hash.includes("?") ? hash.slice(hash.indexOf("?") + 1) : "";
+      const hashParams = new URLSearchParams(hashQuery);
+      return hashParams.get("ticket") || hashParams.get("Ticket");
+    },
+    // 解析接口返回的第三方用户对象
+    resolveTicketUser(result) {
+      if (!result || result.code != 200 || !result.content) {
+        return null;
+      }
+      let user = result.content;
+      if (Array.isArray(user)) {
+        user = user[0];
+      } else if (user.data && Array.isArray(user.data)) {
+        user = user.data[0];
+      }
+      return user || null;
+    },
+    // 使用完 ticket 后从地址栏移除,不留下历史记录
+    removeTicketFromUrl() {
+      const query = { ...(this.$route.query || {}) };
+      const hasRouteTicket = query.ticket != null || query.Ticket != null;
+      if (hasRouteTicket) {
+        delete query.ticket;
+        delete query.Ticket;
+        this.$router.replace({ path: this.$route.path, query }).catch(() => {});
+        return;
+      }
+      const url = new URL(window.location.href);
+      let changed = false;
+      ["ticket", "Ticket"].forEach((key) => {
+        if (url.searchParams.has(key)) {
+          url.searchParams.delete(key);
+          changed = true;
+        }
+      });
+      if (url.hash && url.hash.indexOf("?") > -1) {
+        const hashIndex = url.hash.indexOf("?");
+        const hashPath = url.hash.slice(0, hashIndex);
+        const hashParams = new URLSearchParams(url.hash.slice(hashIndex + 1));
+        ["ticket", "Ticket"].forEach((key) => {
+          if (hashParams.has(key)) {
+            hashParams.delete(key);
+            changed = true;
+          }
+        });
+        const qs = hashParams.toString();
+        url.hash = qs ? hashPath + "?" + qs : hashPath;
+      }
+      if (changed) {
+        window.history.replaceState(
+          window.history.state,
+          document.title,
+          url.pathname + url.search + url.hash
+        );
+      }
+    },
+    fetchTicketUser() {
+      const ticket = this.getUrlTicket();
+      // 地址中没有 ticket 时不请求;互联网环境且从未用过 ticket 时保持原游客逻辑
+      if (!ticket) {
+        if (systemConfig.serverType === "互联网" && !this._fetchedTicket) {
+          this.$store.commit("setThirdPartyUser", {});
+          sessionStorage.removeItem("ssoTicket");
+        }
+        return;
+      }
+      if (this._fetchedTicket === ticket) {
+        this.removeTicketFromUrl();
+        return;
+      }
+      this._fetchedTicket = ticket;
+      sessionStorage.setItem("ssoTicket", ticket);
+      this.removeTicketFromUrl();
+      api
+        .getUserByTicket({ ticket })
+        .then((result) => {
+          const user = this.resolveTicketUser(result);
+          if (user) {
+            this.$store.commit("setThirdPartyUser", user);
+          }
+        })
+        .catch(() => {
+          // ticket 用户获取失败时保持游客展示,不影响系统登录
+        });
+    },
     // scrollUpdate() {
     //   let that = this;
     //   setTimeout(() => {

+ 7 - 0
src/api/common.js

@@ -20,8 +20,15 @@ const updatePassword = (params) => {
   return postform(systemConfig.oauthServiceUrl + "/user/updatePassword", params);
 };
 
+// 根据第三方 ticket 获取用户信息(与 OAuth 登录用户不是同一套数据)
+const getUserByTicket = (params) => {
+  const ticket = typeof params === "string" ? params : params && params.ticket;
+  return postform(systemConfig.proxyUrl + "/verify/getInfoByTicket", { ticket:ticket,code:2111 });
+};
+
 export default {
   login,
   updatePassword,
   getDmsTypes,
+  getUserByTicket,
 };

+ 19 - 10
src/components/user/user.vue

@@ -7,21 +7,15 @@
             <UserFilled />
           </el-icon>
           <span style="padding: 0 6px">
-            {{
-              $store.state.userInfo.username == defaultAccountUsername ? "游客" : $store.state.userInfo.username
-            }}
+            {{ displayName }}
           </span>
         </span>
         <template #dropdown>
           <el-dropdown-menu>
-            <el-dropdown-item command="login">账号登录</el-dropdown-item>
+            <el-dropdown-item command="login">切换账户</el-dropdown-item>
             <el-dropdown-item command="register">申请账号</el-dropdown-item>
-            <el-dropdown-item command="upPassword" v-if="$store.state.userInfo.username != defaultAccountUsername"
-              >修改密码</el-dropdown-item
-            >
-            <el-dropdown-item command="logout" v-if="$store.state.userInfo.username != defaultAccountUsername"
-              >退出登录</el-dropdown-item
-            >
+            <el-dropdown-item command="upPassword" v-if="isSystemUser">修改密码</el-dropdown-item>
+            <el-dropdown-item command="logout" v-if="isSystemUser">退出登录</el-dropdown-item>
           </el-dropdown-menu>
         </template>
       </el-dropdown>
@@ -78,6 +72,21 @@ export default {
       contentItem: {},
     };
   },
+  computed: {
+    // 已切换为本系统账号(非游客)
+    isSystemUser() {
+      const username = this.$store.state.userInfo && this.$store.state.userInfo.username;
+      return username && username != this.defaultAccountUsername;
+    },
+    // 游客显示第三方姓名,已切换系统账号则显示系统用户名
+    displayName() {
+      if (this.isSystemUser) {
+        return this.$store.state.userInfo.username;
+      }
+      const realName = this.$store.state.thirdPartyUser && this.$store.state.thirdPartyUser.c_real_name;
+      return realName || "游客";
+    },
+  },
   mounted() {
     this.initData();
   },

+ 12 - 0
src/store/index.js

@@ -7,6 +7,10 @@ export default createStore({
     token: "",
     userState: false,
     userInfo: localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo")) : {},
+    // 第三方 ticket 用户(游客权限下仅用于展示姓名)
+    thirdPartyUser: sessionStorage.getItem("thirdPartyUser")
+      ? JSON.parse(sessionStorage.getItem("thirdPartyUser"))
+      : {},
     // DMS字典,系统app.vue初始化一次之后,后续直接全局$getDmsTypes可调用,传入字典cName和index,返回对应字典数据
     DMSTypes: ["appstatus", "yzt_task_type", "applevel", "task_status"],
     // appstatus: 应用状态;yzt_task_type: 任务类型
@@ -63,6 +67,14 @@ export default createStore({
       state.userInfo = obj;
       localStorage.setItem("userInfo", JSON.stringify(obj));
     },
+    setThirdPartyUser(state, obj) {
+      state.thirdPartyUser = obj || {};
+      if (obj && Object.keys(obj).length) {
+        sessionStorage.setItem("thirdPartyUser", JSON.stringify(obj));
+      } else {
+        sessionStorage.removeItem("thirdPartyUser");
+      }
+    },
     setActiveMenu(state, id) {
       state.activeMenu = id;
     },

+ 9 - 5
src/views/HomePage.vue

@@ -28,7 +28,7 @@
     </el-drawer> -->
 
     <!-- 一张图可视化平台 全息按钮 -->
-  <a href="" @click="handleOpenClick()" target="_blank" class="btn-map-new">
+  <a href="javascript:void(0)" @click.prevent="handleOpenClick" target="_blank" class="btn-map-new">
     <span class="btn-title">一张图可视化平台</span>
     <svg class="out-icon" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="1.8">
       <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path>
@@ -870,7 +870,7 @@ export default {
     handleTotalCallNumber(num) {
       // 667507060
       let str = num.toString();
-      debugger;
+      // debugger;
       this.totalCallNumber = str;
       let strArr = str.split("").reverse();
       this.totalCall = [];
@@ -1391,9 +1391,13 @@ export default {
       return `${month}/${day}`;
     },
     handleOpenClick() {
-      // console.log(systemConfig.iframeUrl + "?token=" + localStorage.getItem("token"));
-      // window.open(systemConfig.iframeUrl + "?token=" + localStorage.getItem("token"), "_blank");
-      window.open(systemConfig.iframeUrl + "?token=1", "_blank");
+      const thirdPartyUser = this.$store.state.thirdPartyUser || {};
+      const phone = thirdPartyUser.c_phone || "";
+      let url = systemConfig.iframeUrl;
+      if (phone) {
+        url += (url.indexOf("?") > -1 ? "&" : "?") + "phone=" + encodeURIComponent(phone);
+      }
+      window.open(url, "_blank");
     },
   },
 };

+ 7 - 0
vue.config.js

@@ -89,6 +89,13 @@ module.exports = defineConfig({
           "^/proxy_dms": "",
         },
       },
+      "/proxy_proxy/": {
+        target: "http://10.235.245.174:10011/proxy/",
+        changeOrigin: true,
+        pathRewrite: {
+          "^/proxy_dms": "",
+        },
+      },
       "/oneMap/": {
         // 本地环境
         // target: 'http://127.0.0.1:10099/qpyzt',