From caf56217dc2bf898b63b0e1f31a7098202c32825 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期六, 15 十一月 2025 16:50:17 +0800
Subject: [PATCH] Merge branch 'feature_gps'
---
app/App.vue | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 156 insertions(+), 0 deletions(-)
diff --git a/app/App.vue b/app/App.vue
index 797f513..86cbf17 100644
--- a/app/App.vue
+++ b/app/App.vue
@@ -2,10 +2,73 @@
import config from './config'
import store from '@/store'
import { getToken } from '@/utils/auth'
+ import { getUnreadCount } from '@/api/message'
+ import storage from '@/utils/storage'
export default {
+ data() {
+ return {
+ messagePollingTimer: null,
+ lastToken: null // 鐢ㄤ簬妫�娴� token 鍙樺寲
+ }
+ },
onLaunch: function() {
+ this.lastToken = getToken()
this.initApp()
+
+ // 妫�鏌ュ苟娓呯悊瀛樺偍绌洪棿
+ this.checkStorage()
+
+ // 鐩戝惉鐢ㄦ埛鐧诲綍浜嬩欢
+ 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() {
+ 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: {
// 鍒濆鍖栧簲鐢�
@@ -16,6 +79,9 @@
//#ifdef H5
this.checkLogin()
//#endif
+
+ // 娉ㄦ剰锛氫笉鍦ㄥ簲鐢ㄥ惎鍔ㄦ椂鑷姩鍚姩杞
+ // 鍙湁鍦ㄧ敤鎴蜂富鍔ㄧ櫥褰曟垚鍔熷悗鎵嶅惎鍔紙閫氳繃 user-login 浜嬩欢瑙﹀彂锛�
},
initConfig() {
this.globalData.config = config
@@ -24,6 +90,96 @@
if (!getToken()) {
this.$tab.reLaunch('/pages/login')
}
+ },
+
+ // 鍒ゆ柇褰撳墠鏄惁鍦ㄧ櫥褰曢〉闈�
+ 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)
+
+ if (count > 0) {
+ // 璁剧疆寰芥爣
+ uni.setTabBarBadge({
+ index: 3, // 娑堟伅椤甸潰鍦╰abBar涓殑绱㈠紩浣嶇疆锛�0寮�濮嬶級
+ text: count > 99 ? '99+' : count.toString()
+ })
+ } else {
+ // 绉婚櫎寰芥爣
+ uni.removeTabBarBadge({
+ index: 3
+ })
+ }
+ }).catch(error => {
+ console.error('鑾峰彇鏈娑堟伅鏁伴噺澶辫触:', error)
+ })
+ },
+
+ // 鍚姩娑堟伅杞
+ startMessagePolling() {
+ // 姣�30绉掕疆璇竴娆�
+ this.messagePollingTimer = setInterval(() => {
+ if (getToken()) {
+ this.updateUnreadMessageBadge()
+ } else {
+ // 濡傛灉鐢ㄦ埛宸茬櫥鍑猴紝鍋滄杞
+ this.stopMessagePolling()
+ }
+ }, 30000) // 30绉�
+ },
+
+ // 鍋滄娑堟伅杞
+ stopMessagePolling() {
+ if (this.messagePollingTimer) {
+ 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)
+ }
}
}
}
--
Gitblit v1.9.1