From 6676a35122fd9c97d1b1679c211bc8a9b97f08f2 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期二, 24 三月 2026 23:17:37 +0800
Subject: [PATCH] feat: 增加日志记录历史消息

---
 app/pages/qylogin.vue |  258 +++++++++++++++++++++++++++++++++++----------------
 1 files changed, 178 insertions(+), 80 deletions(-)

diff --git a/app/pages/qylogin.vue b/app/pages/qylogin.vue
index 80ff189..2e47b30 100644
--- a/app/pages/qylogin.vue
+++ b/app/pages/qylogin.vue
@@ -20,6 +20,7 @@
 
 <script>
 import { qyWechatAutoLogin } from "@/api/login";
+import { buildUrlWithParams } from "@/utils/wechat";
 
 export default {
   data() {
@@ -33,33 +34,68 @@
   },
 
   onLoad(options) {
-    // 淇濆瓨椤甸潰鍙傛暟
-    this.pageOptions = options || {};
-    // 椤甸潰鍔犺浇鏃舵墽琛屽厤鐧绘祦绋�
-    this.qyWechatAutoLogin();
+    // 娣诲姞瀹归敊澶勭悊
+    try {
+      // 淇濆瓨椤甸潰鍙傛暟
+      this.pageOptions = options || {};
+      
+      // 鎵撳嵃鐜淇℃伅
+      this.logEnvironmentInfo();
+      
+      // 椤甸潰鍔犺浇鏃舵墽琛屽厤鐧绘祦绋�
+      this.qyWechatAutoLogin();
+    } catch (e) {
+      console.error('椤甸潰鍔犺浇鍑洪敊:', e);
+      this.loading = false;
+      this.error = true;
+      this.errorMessage = "椤甸潰鍒濆鍖栧け璐ワ紝璇烽噸璇�";
+    }
   },
 
   methods: {
-    async getLoginCode() {
-      //杩欓噷瑕佽皟鐢╳x.qy.login鑾峰彇code,鏄惁鏈夐泦鎴愬埌uni-app涓憿
-      return new Promise((resolve, reject) => {
-        wx.qy.login({
-          success: (res) => {
-            if (res.code) {
-              console.log("浼佷笟寰俊灏忕▼搴� ---> code锛�", res.code);
-              resolve(res.code); // 杩斿洖 code 缁欏悗绔�
-            } else {
-              reject(new Error("鑾峰彇 code 澶辫触锛�" + res.errMsg));
-            }
-          },
-          fail: (err) => {
-            console.error("wx.qy.login 璋冪敤澶辫触锛�", err);
-            // 甯歌澶辫触鍘熷洜锛氶潪浼佷笟寰俊瀹㈡埛绔墦寮�銆佸皬绋嬪簭鏈叧鑱斾紒涓氬井淇$瓑
-            reject(new Error("浼佷笟寰俊鐧诲綍鎺ュ彛璋冪敤澶辫触锛�" + err.errMsg));
-          },
-        });
-      });
+    /**
+     * 鎵撳嵃鐜淇℃伅锛堣皟璇曠敤锛�
+     */
+    logEnvironmentInfo() {
+      // #ifdef MP-WEIXIN
+      console.log('========== 鐜淇℃伅 ==========');
+      console.log('wx 鏄惁瀛樺湪:', typeof wx !== 'undefined');
+      console.log('wx.qy 鏄惁瀛樺湪:', typeof wx !== 'undefined' && typeof wx.qy !== 'undefined');
+      
+      if (typeof wx !== 'undefined') {
+        console.log('wx.login 鏄惁瀛樺湪:', typeof wx.login === 'function');
+        
+        if (typeof wx.qy !== 'undefined') {
+          console.log('wx.qy.login 鏄惁瀛樺湪:', typeof wx.qy.login === 'function');
+          console.log('wx.qy 瀵硅薄:', wx.qy);
+        } else {
+          console.warn('鈿狅笍 wx.qy 涓嶅瓨鍦紒');
+          console.warn('鍙兘鍘熷洜锛�');
+          console.warn('1. 褰撳墠鍦ㄥ紑鍙戣�呭伐鍏蜂腑锛寃x.qy API 鍙兘涓嶅彲鐢�');
+          console.warn('2. 闇�瑕佸湪鐪熸満涓婅皟璇�');
+          console.warn('3. appid 閰嶇疆鍙兘涓嶆纭�');
+        }
+        
+        // 鑾峰彇绯荤粺淇℃伅
+        try {
+          const systemInfo = wx.getSystemInfoSync();
+          console.log('绯荤粺淇℃伅:', systemInfo);
+        } catch (e) {
+          console.error('鑾峰彇绯荤粺淇℃伅澶辫触:', e);
+        }
+        
+        // 鑾峰彇璐﹀彿淇℃伅
+        try {
+          const accountInfo = wx.getAccountInfoSync();
+          console.log('璐﹀彿淇℃伅:', accountInfo);
+        } catch (e) {
+          console.error('鑾峰彇璐﹀彿淇℃伅澶辫触:', e);
+        }
+      }
+      console.log('==============================');
+      // #endif
     },
+
     /**
      * 浼佷笟寰俊鍏嶇櫥娴佺▼
      */
@@ -72,13 +108,11 @@
         // 鍦ㄥ井淇″皬绋嬪簭鐜涓紝閫氳繃浼佷笟寰俊鍏嶇櫥
         console.log("浼佷笟寰俊灏忕▼搴忕幆澧冨厤鐧�");
 
-        // 鑾峰彇URL鍙傛暟涓殑code
+        // 鐩存帴閫氳繃浼佷笟寰俊API鑾峰彇code锛屼笉浣跨敤URL涓殑code
         const code = await this.getLoginCode();
 
         if (!code) {
-          // 濡傛灉娌℃湁code锛屽皾璇曢�氳繃浼佷笟寰俊API鑾峰彇
-          this.handleWxWorkLogin();
-          return;
+          throw new Error("鏃犳硶鑾峰彇浼佷笟寰俊鎺堟潈code");
         }
 
         // 璋冪敤鍚庣鎺ュ彛杩涜鍏嶇櫥
@@ -89,7 +123,7 @@
           const token = response.data.token;
           
           this.$store.commit('SET_TOKEN', token)
-           // 蹇呴』璋冪敤setToken淇濆瓨鍒版湰鍦板瓨鍌�
+          // 蹇呴』璋冪敤setToken淇濆瓨鍒版湰鍦板瓨鍌�
           const { setToken } = require('@/utils/auth')
           setToken(token)
           // 鑾峰彇鐢ㄦ埛淇℃伅
@@ -109,10 +143,10 @@
         }
 
         // 鑾峰彇URL鍙傛暟涓殑code
-        const codeH5 = this.getUrlParam("code");
-
+        let codeH5 = this.getUrlParam("code");
+        
+        // 濡傛灉URL涓病鏈塩ode锛屽垯璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
         if (!codeH5) {
-          // 濡傛灉娌℃湁code锛屽垯璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
           this.redirectToWxWorkAuth();
           return;
         }
@@ -148,56 +182,100 @@
     },
 
     /**
-     * 澶勭悊浼佷笟寰俊鐧诲綍
+     * 绛夊緟 wx.qy 瀵硅薄鍒濆鍖�
      */
-    handleWxWorkLogin() {
-      // #ifdef MP-WEIXIN
-      // 鍦ㄤ紒涓氬井淇″皬绋嬪簭涓紝鍙互鐩存帴璋冪敤浼佷笟寰俊鐧诲綍API
-      uni.login({
-        provider: "weixin",
-        success: (loginRes) => {
-          console.log("浼佷笟寰俊鐧诲綍鎴愬姛", loginRes);
-          // 璋冪敤鍚庣鎺ュ彛杩涜鍏嶇櫥
-          qyWechatAutoLogin(loginRes.code)
-            .then((response) => {
-              if (response.code === 200) {
-                // 鍏嶇櫥鎴愬姛锛屼繚瀛榯oken
-                const token = response.data.token;
-                this.$store.commit("SET_TOKEN", token);
-                uni.setStorageSync("token", token);
-
-                // 鑾峰彇鐢ㄦ埛淇℃伅
-                this.$store.dispatch("GetInfo").then(() => {
-                  // 璺宠浆鍒伴椤垫垨鍏朵粬鎸囧畾椤甸潰
-                  this.redirectAfterLogin();
-                });
-              } else {
-                throw new Error(response.msg || "鍏嶇櫥澶辫触");
-              }
-            })
-            .catch((error) => {
-              console.error("鍏嶇櫥澶辫触:", error);
-              this.loading = false;
-              this.error = true;
-              this.errorMessage = error.message || "鍏嶇櫥澶辫触锛岃绋嶅悗閲嶈瘯";
+    waitForWxQy(timeout = 5000) {
+      return new Promise((resolve, reject) => {
+        const startTime = Date.now();
+        let checkCount = 0;
+        
+        const checkWxQy = () => {
+          checkCount++;
+          const elapsed = Date.now() - startTime;
+          
+          if (typeof wx !== 'undefined' && typeof wx.qy !== 'undefined' && typeof wx.qy.login === 'function') {
+            console.log(`鉁� wx.qy 宸插垵濮嬪寲 (鑰楁椂: ${elapsed}ms, 妫�鏌ユ鏁�: ${checkCount})`);
+            resolve(true);
+          } else if (elapsed > timeout) {
+            console.error(`鉂� 绛夊緟 wx.qy 鍒濆鍖栬秴鏃� (${timeout}ms)`);
+            console.error('褰撳墠鐘舵��:', {
+              wx瀛樺湪: typeof wx !== 'undefined',
+              'wx.qy瀛樺湪': typeof wx !== 'undefined' && typeof wx.qy !== 'undefined',
+              妫�鏌ユ鏁�: checkCount
             });
-        },
-        fail: (err) => {
-          console.error("浼佷笟寰俊鐧诲綍澶辫触:", err);
-          this.loading = false;
-          this.error = true;
-          this.errorMessage = "浼佷笟寰俊鐧诲綍澶辫触锛岃绋嶅悗閲嶈瘯";
-        },
+            
+            // 鎻愪緵鏇磋缁嗙殑閿欒淇℃伅
+            const errorMsg = '浼佷笟寰俊鐜鍒濆鍖栬秴鏃躲�俓n\n' +
+                           '鍙兘鐨勫師鍥狅細\n' +
+                           '1. 褰撳墠鍦ㄥ紑鍙戣�呭伐鍏蜂腑锛寃x.qy API 涓嶅彲鐢╘n' +
+                           '2. 璇蜂娇鐢ㄧ湡鏈洪瑙堟垨鐪熸満璋冭瘯\n' +
+                           '3. 妫�鏌ュ皬绋嬪簭 appid 鏄惁閰嶇疆姝g‘';
+            reject(new Error(errorMsg));
+          } else {
+            if (checkCount % 10 === 1) {
+              console.log(`鈴� 绛夊緟 wx.qy 鍒濆鍖�... (${elapsed}ms, 绗�${checkCount}娆℃鏌�)`);
+            }
+            setTimeout(checkWxQy, 100);
+          }
+        };
+        
+        checkWxQy();
       });
-      // #endif
+    },
+
+    /**
+     * 鑾峰彇浼佷笟寰俊鐧诲綍code
+     */
+    getLoginCode() {
+      return new Promise(async (resolve, reject) => {
+        // #ifdef MP-WEIXIN
+        try {
+          // 妫�鏌ユ槸鍚﹀湪浼佷笟寰俊灏忕▼搴忕幆澧�
+          console.log('妫�鏌ヤ紒涓氬井淇$幆澧冿紝wx.qy 鏄惁瀛樺湪:', typeof wx !== 'undefined' && typeof wx.qy !== 'undefined');
+          
+          // 绛夊緟 wx.qy 鍒濆鍖栧畬鎴�
+          await this.waitForWxQy();
+          
+          // 浣跨敤浼佷笟寰俊灏忕▼搴廇PI鑾峰彇code
+          console.log('浣跨敤 wx.qy.login 鑾峰彇code');
+          wx.qy.login({
+            success: (res) => {
+              if (res.code) {
+                console.log("浼佷笟寰俊灏忕▼搴� ---> code锛�", res.code);
+                resolve(res.code);
+              } else {
+                reject(new Error("鑾峰彇code澶辫触锛�" + (res.errMsg || '鏈煡閿欒')));
+              }
+            },
+            fail: (err) => {
+              console.error("wx.qy.login璋冪敤澶辫触锛�", err);
+              reject(new Error("浼佷笟寰俊鐧诲綍鎺ュ彛璋冪敤澶辫触锛�" + (err.errMsg || '鏈煡閿欒')));
+            },
+          });
+        } catch (error) {
+          console.error('鑾峰彇鐧诲綍code寮傚父:', error);
+          reject(error);
+        }
+        // #endif
+        
+        // #ifndef MP-WEIXIN
+        resolve(null);
+        // #endif
+      });
     },
 
     /**
      * 妫�鏌ユ槸鍚﹀湪浼佷笟寰俊鐜
      */
     isWxWorkEnvironment() {
+      // #ifdef H5
       const userAgent = navigator.userAgent.toLowerCase();
       return userAgent.includes("wxwork");
+      // #endif
+      
+      // #ifndef H5
+      return false;
+      // #endif
     },
 
     /**
@@ -211,17 +289,20 @@
       // #endif
 
       // #ifdef MP-WEIXIN
-      // 鍦ㄥ皬绋嬪簭涓彲浠ラ�氳繃鍏朵粬鏂瑰紡鑾峰彇鍙傛暟
-      // 杩欓噷绠�鍖栧鐞嗭紝瀹為檯椤圭洰涓彲浠ユ牴鎹渶瑕佽皟鏁�
+      // 鍦ㄥ皬绋嬪簭涓紝鍙傛暟閫氳繃onLoad鐨刼ptions浼犻��
+      if (this.pageOptions && this.pageOptions[name]) {
+        return decodeURIComponent(this.pageOptions[name]);
+      }
       // #endif
 
       return null;
     },
 
     /**
-     * 璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈�
+     * 璺宠浆鍒颁紒涓氬井淇℃巿鏉冮〉闈紙H5鐜锛�
      */
     redirectToWxWorkAuth() {
+      // #ifdef H5
       // 浠庡叏灞�閰嶇疆涓幏鍙栦紒涓氬井淇¢厤缃�
       const config = getApp().globalData.config;
       const corpId = config.qyWechatCorpId || "your_corp_id"; // 浼佷笟ID
@@ -232,19 +313,36 @@
       const authUrl = `https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=${corpId}&agentid=${agentId}&redirect_uri=${redirectUri}&state=${state}`;
 
       window.location.href = authUrl;
+      // #endif
     },
 
     /**
      * 鐧诲綍鎴愬姛鍚庣殑璺宠浆澶勭悊
      */
     redirectAfterLogin() {
-      // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
-      if (this.pageOptions.redirect) {
-        // 瑙g爜redirect鍙傛暟
-        const redirectUrl = decodeURIComponent(this.pageOptions.redirect);
-        this.$tab.reLaunch(redirectUrl);
-      } else {
-        // 榛樿璺宠浆鍒伴椤�
+      try {
+        // 妫�鏌ユ槸鍚︽湁redirect鍙傛暟鎸囧畾璺宠浆椤甸潰
+        let redirectUrl = this.getUrlParam("redirect");
+        
+        // 濡傛灉娌℃湁redirect鍙傛暟锛屾鏌ユ槸鍚︽湁淇濆瓨鐨勭洰鏍囬〉闈�
+        if (!redirectUrl) {
+          const { getTargetUrl } = require('@/utils/auth')
+          redirectUrl = getTargetUrl()
+        }
+        
+        if (redirectUrl) {
+          // 瑙g爜redirect鍙傛暟
+          redirectUrl = decodeURIComponent(redirectUrl);
+          console.log("鑷姩璺宠浆鍒版寚瀹氶〉闈�:", redirectUrl);
+          this.$tab.reLaunch(redirectUrl);
+        } else {
+          // 榛樿璺宠浆鍒伴椤�
+          console.log("璺宠浆鍒伴椤�");
+          this.$tab.reLaunch("/pages/index");
+        }
+      } catch (e) {
+        console.error("璺宠浆澶辫触锛屼娇鐢ㄩ粯璁よ烦杞�:", e);
+        // 鍑虹幇寮傚父鏃讹紝榛樿璺宠浆鍒伴椤�
         this.$tab.reLaunch("/pages/index");
       }
     },

--
Gitblit v1.9.1