From 7de1396e315896dbc72a9d54e44f77434ea90f18 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期日, 14 十二月 2025 23:47:34 +0800
Subject: [PATCH] feat:增加企业微信自动登录

---
 app/App.vue |  137 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 120 insertions(+), 17 deletions(-)

diff --git a/app/App.vue b/app/App.vue
index 650a7fd..15eb1ac 100644
--- a/app/App.vue
+++ b/app/App.vue
@@ -3,6 +3,7 @@
   import store from '@/store'
   import { getToken } from '@/utils/auth'
   import { getUnreadCount } from '@/api/message'
+  import storage from '@/utils/storage'
 
   export default {
     data() {
@@ -11,9 +12,23 @@
         lastToken: null // 鐢ㄤ簬妫�娴� token 鍙樺寲
       }
     },
-    onLaunch: function() {
+    onLaunch: function(options) {
       this.lastToken = getToken()
-      this.initApp()
+      this.initApp(options)
+      
+      // 妫�鏌ュ苟娓呯悊瀛樺偍绌洪棿
+      this.checkStorage()
+      
+      // 鐩戝惉鐢ㄦ埛鐧诲綍浜嬩欢
+      uni.$on('user-login', () => {
+        console.log('鎺ユ敹鍒扮敤鎴风櫥褰曚簨浠讹紝鍚姩娑堟伅杞')
+        const token = getToken()
+        if (token) {
+          this.lastToken = token
+          this.updateUnreadMessageBadge()
+          this.startMessagePolling()
+        }
+      })
       
       // 鐩戝惉鐢ㄦ埛鐧诲嚭浜嬩欢
       uni.$on('user-logout', () => {
@@ -42,7 +57,8 @@
       }
       
       // 搴旂敤鏄剧ず鏃跺埛鏂版湭璇绘秷鎭暟閲�
-      if (currentToken) {
+      // 娉ㄦ剰锛氬彧鏈夊凡鐧诲綍涓斾笉鍦ㄧ櫥褰曢〉闈㈡椂鎵嶈皟鐢�
+      if (currentToken && !this.isLoginPage()) {
         this.updateUnreadMessageBadge()
         // 閲嶆柊鍚姩杞锛堝鏋滀箣鍓嶅凡鍋滄锛�
         if (!this.messagePollingTimer) {
@@ -56,28 +72,87 @@
     },
     methods: {
       // 鍒濆鍖栧簲鐢�
-      initApp() {
+      initApp(options) {
         // 鍒濆鍖栧簲鐢ㄩ厤缃�
         this.initConfig()
-        // 妫�鏌ョ敤鎴风櫥褰曠姸鎬�
-        //#ifdef H5
-        this.checkLogin()
-        //#endif
+        // 妫�鏌ョ敤鎴风櫥褰曠姸鎬佸苟鑷姩璺宠浆鍒板悎閫傜殑鐧诲綍椤甸潰
+        this.checkLoginAndRedirect(options)
         
-        // 濡傛灉宸茬櫥褰曪紝鍚姩鏈娑堟伅杞
-        if (getToken()) {
-          this.updateUnreadMessageBadge()
-          // 姣�30绉掕疆璇竴娆�
-          this.startMessagePolling()
-        }
+        // 娉ㄦ剰锛氫笉鍦ㄥ簲鐢ㄥ惎鍔ㄦ椂鑷姩鍚姩杞
+        // 鍙湁鍦ㄧ敤鎴蜂富鍔ㄧ櫥褰曟垚鍔熷悗鎵嶅惎鍔紙閫氳繃 user-login 浜嬩欢瑙﹀彂锛�
       },
       initConfig() {
         this.globalData.config = config
       },
-      checkLogin() {
+      // 妫�鏌ョ櫥褰曠姸鎬佸苟鑷姩璺宠浆鍒板悎閫傜殑鐧诲綍椤甸潰
+      checkLoginAndRedirect(options) {
         if (!getToken()) {
-          this.$tab.reLaunch('/pages/login') 
+          // 妫�鏌ヨ繍琛岀幆澧冨苟璺宠浆鍒板搴旂殑鐧诲綍椤甸潰
+          // #ifdef MP-WEIXIN
+          // 鍦ㄥ井淇″皬绋嬪簭鐜涓�
+          try {
+            // 鑾峰彇绯荤粺淇℃伅
+            const systemInfo = uni.getSystemInfoSync()
+            console.log('绯荤粺淇℃伅:', systemInfo)
+            
+            // 妫�鏌nvironment瀛楁鏄惁涓簑xwork
+            if (systemInfo.environment === 'wxwork') {
+              console.log('妫�娴嬪埌浼佷笟寰俊鐜锛岃烦杞埌浼佷笟寰俊鍏嶇櫥椤甸潰')
+              // 鏋勯�犲甫鍙傛暟鐨刄RL
+              let url = '/pages/qylogin'
+              if (options && options.query) {
+                const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
+                if (queryParams) {
+                  url += '?' + queryParams
+                }
+              }
+              this.$tab.reLaunch(url)
+              return
+            } else {
+              console.log('妫�娴嬪埌鏅�氬井淇$幆澧冿紝璺宠浆鍒板井淇$櫥褰曢〉闈�')
+              // 鏋勯�犲甫鍙傛暟鐨刄RL
+              let url = '/pages/login'
+              if (options && options.query) {
+                const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
+                if (queryParams) {
+                  url += '?' + queryParams
+                }
+              }
+              this.$tab.reLaunch(url)
+              return
+            }
+          } catch (e) {
+            console.error('鑾峰彇绯荤粺淇℃伅澶辫触:', e)
+            // 榛樿璺宠浆鍒版櫘閫氱櫥褰曢〉闈�
+            this.$tab.reLaunch('/pages/login')
+          }
+          // #endif
+          
+          // #ifndef MP-WEIXIN
+          // 闈炲井淇″皬绋嬪簭鐜锛岃烦杞埌鏅�氱櫥褰曢〉闈�
+          console.log('闈炲井淇″皬绋嬪簭鐜锛岃烦杞埌鏅�氱櫥褰曢〉闈�')
+          let url = '/pages/login'
+          if (options && options.query) {
+            const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
+            if (queryParams) {
+              url += '?' + queryParams
+            }
+          }
+          this.$tab.reLaunch(url)
+          // #endif
         }
+      },
+      
+      // 鍒ゆ柇褰撳墠鏄惁鍦ㄧ櫥褰曢〉闈�
+      isLoginPage() {
+        const pages = getCurrentPages()
+        if (pages.length === 0) {
+          return false
+        }
+        const currentPage = pages[pages.length - 1]
+        const route = currentPage.route || ''
+        // 鍒ゆ柇鏄惁涓虹櫥褰曠浉鍏抽〉闈�
+        return route.includes('login') || route.includes('register')
       },
       
       // 鏇存柊鏈娑堟伅寰芥爣
@@ -128,6 +203,34 @@
           clearInterval(this.messagePollingTimer)
           this.messagePollingTimer = null
         }
+      },
+      
+      // 妫�鏌ュ瓨鍌ㄧ┖闂�
+      checkStorage() {
+        try {
+          // 鑾峰彇瀛樺偍淇℃伅
+          const info = storage.getStorageInfo()
+          
+          if (info) {
+            const usagePercent = (info.currentSize / info.limitSize) * 100
+            console.log(`馃捑 瀛樺偍浣跨敤鎯呭喌: ${usagePercent.toFixed(2)}% (${info.currentSize}KB / ${info.limitSize}KB)`)
+            
+            // 濡傛灉浣跨敤瓒呰繃 80%锛岃嚜鍔ㄦ竻鐞�
+            if (usagePercent > 80) {
+              console.warn('鈿狅笍 瀛樺偍绌洪棿浣跨敤瓒呰繃 80%锛屽紑濮嬭嚜鍔ㄦ竻鐞�...')
+              storage.checkAndClean()
+              
+              // 鍐嶆妫�鏌�
+              const newInfo = storage.getStorageInfo()
+              if (newInfo) {
+                const newUsagePercent = (newInfo.currentSize / newInfo.limitSize) * 100
+                console.log(`鉁� 娓呯悊鍚庝娇鐢ㄦ儏鍐�: ${newUsagePercent.toFixed(2)}%`)
+              }
+            }
+          }
+        } catch (e) {
+          console.error('妫�鏌ュ瓨鍌ㄥけ璐�:', e)
+        }
       }
     }
   }
@@ -135,4 +238,4 @@
 
 <style lang="scss">
   @import '@/static/scss/index.scss'
-</style>
+</style>
\ No newline at end of file

--
Gitblit v1.9.1