From 2aebbc9601ab439707f69b08e467808df9f7549c Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期五, 07 十一月 2025 11:50:26 +0800
Subject: [PATCH] feat: weixin

---
 app/App.vue |   84 +++++++++++++++++++++++++++++++++++++----
 1 files changed, 75 insertions(+), 9 deletions(-)

diff --git a/app/App.vue b/app/App.vue
index 3610e7c..0e788bc 100644
--- a/app/App.vue
+++ b/app/App.vue
@@ -5,14 +5,66 @@
   import { getUnreadCount } from '@/api/message'
 
   export default {
+    data() {
+      return {
+        messagePollingTimer: null,
+        lastToken: null // 鐢ㄤ簬妫�娴� token 鍙樺寲
+      }
+    },
     onLaunch: function() {
+      this.lastToken = getToken()
       this.initApp()
+      
+      // 鐩戝惉鐢ㄦ埛鐧诲綍浜嬩欢
+      uni.$on('user-login', () => {
+        console.log('鎺ユ敹鍒扮敤鎴风櫥褰曚簨浠讹紝鍚姩娑堟伅杞')
+        const token = getToken()
+        if (token) {
+          this.lastToken = token
+          this.updateUnreadMessageBadge()
+          this.startMessagePolling()
+        }
+      })
+      
+      // 鐩戝惉鐢ㄦ埛鐧诲嚭浜嬩欢
+      uni.$on('user-logout', () => {
+        console.log('鎺ユ敹鍒扮敤鎴风櫥鍑轰簨浠讹紝鍋滄娑堟伅杞')
+        this.stopMessagePolling()
+        this.lastToken = null
+        // 娓呴櫎娑堟伅寰芥爣
+        uni.removeTabBarBadge({ index: 3 })
+      })
     },
     onShow: function() {
-      // 搴旂敤鏄剧ず鏃跺埛鏂版湭璇绘秷鎭暟閲�
-      if (getToken()) {
-        this.updateUnreadMessageBadge()
+      const currentToken = getToken()
+      
+      // 妫�娴� token 鍙樺寲锛氫粠鏈夊埌鏃狅紙鐧诲嚭锛�
+      if (this.lastToken && !currentToken) {
+        console.log('妫�娴嬪埌鐢ㄦ埛宸茬櫥鍑猴紝鍋滄娑堟伅杞')
+        this.stopMessagePolling()
+        this.lastToken = currentToken
+        return
       }
+      
+      // 妫�娴� token 鍙樺寲锛氫粠鏃犲埌鏈夛紙鐧诲綍锛�
+      if (!this.lastToken && currentToken) {
+        console.log('妫�娴嬪埌鐢ㄦ埛宸茬櫥褰曪紝鍚姩娑堟伅杞')
+        this.lastToken = currentToken
+      }
+      
+      // 搴旂敤鏄剧ず鏃跺埛鏂版湭璇绘秷鎭暟閲�
+      // 娉ㄦ剰锛氬彧鏈夊凡鐧诲綍涓斾笉鍦ㄧ櫥褰曢〉闈㈡椂鎵嶈皟鐢�
+      if (currentToken && !this.isLoginPage()) {
+        this.updateUnreadMessageBadge()
+        // 閲嶆柊鍚姩杞锛堝鏋滀箣鍓嶅凡鍋滄锛�
+        if (!this.messagePollingTimer) {
+          this.startMessagePolling()
+        }
+      }
+    },
+    onHide: function() {
+      // 搴旂敤闅愯棌鏃跺仠姝㈣疆璇紝鑺傜渷璧勬簮
+      this.stopMessagePolling()
     },
     methods: {
       // 鍒濆鍖栧簲鐢�
@@ -24,12 +76,8 @@
         this.checkLogin()
         //#endif
         
-        // 濡傛灉宸茬櫥褰曪紝鍚姩鏈娑堟伅杞
-        if (getToken()) {
-          this.updateUnreadMessageBadge()
-          // 姣�30绉掕疆璇竴娆�
-          this.startMessagePolling()
-        }
+        // 娉ㄦ剰锛氫笉鍦ㄥ簲鐢ㄥ惎鍔ㄦ椂鑷姩鍚姩杞
+        // 鍙湁鍦ㄧ敤鎴蜂富鍔ㄧ櫥褰曟垚鍔熷悗鎵嶅惎鍔紙閫氳繃 user-login 浜嬩欢瑙﹀彂锛�
       },
       initConfig() {
         this.globalData.config = config
@@ -40,8 +88,26 @@
         }
       },
       
+      // 鍒ゆ柇褰撳墠鏄惁鍦ㄧ櫥褰曢〉闈�
+      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')
+      },
+      
       // 鏇存柊鏈娑堟伅寰芥爣
       updateUnreadMessageBadge() {
+        // 妫�鏌ユ槸鍚﹀凡鐧诲綍锛屾湭鐧诲綍鍒欎笉璇锋眰
+        if (!getToken()) {
+          console.log('鐢ㄦ埛鏈櫥褰曪紝璺宠繃鑾峰彇鏈娑堟伅鏁伴噺')
+          return
+        }
+        
         getUnreadCount().then(response => {
           const count = response.data || 0
           console.log('鏈娑堟伅鏁伴噺:', count)

--
Gitblit v1.9.1