Bladeren bron

消息红点优化

chenjj 3 maanden geleden
bovenliggende
commit
1a8a5393b8
4 gewijzigde bestanden met toevoegingen van 15 en 4 verwijderingen
  1. 6 2
      components/CustomTabBar/index.vue
  2. 3 1
      store/modules/user.ts
  3. 5 0
      utils/WebSocketManager.js
  4. 1 1
      utils/index.js

+ 6 - 2
components/CustomTabBar/index.vue

@@ -48,13 +48,14 @@ export default {
             },
             immediate: true,
             deep: true
-        }
+        },
+        
     },
     data() {
         return {
             selectIndex: null,
             userType: uni.getStorageSync('userType') || 1, //读取本地存储
-            messageCount: store.state.user.messageCount
+            // messageCount: store.state.user.messageCount
         }
     },
     computed: {
@@ -144,6 +145,9 @@ export default {
                     key: 'mine'
                 }
             ]
+        },
+        messageCount(){
+            return store.state.user.messageCount;
         }
     },
     //uniapp子组件不支持应用生命周期,所以只能用vue生命周期

+ 3 - 1
store/modules/user.ts

@@ -63,6 +63,7 @@ const user: Module<UserState, UserState> = {
       state.userId = id
     },
     SET_MESSAGECOUNT: (state, num: Number) => {
+			console.log("TCL: num", num)
       state.messageCount = num
     },
     SET_MESSAGE: (state, ms:any) => {
@@ -119,7 +120,7 @@ const user: Module<UserState, UserState> = {
           commit('SET_USERID', res.user.userId)
 
 
-          console.log("TCL: GetInfo -> wsManager1", this.wsManager)
+         
           if (this.wsManager) {
             this.wsManager.closeConnection();
           }else{
@@ -127,6 +128,7 @@ const user: Module<UserState, UserState> = {
             const ms = new WebSocketManager(res.user.userId);
             // 设置消息回调
             ms.onMessage(data => {
+              console.log("处理消息逻辑", data.data)
               // 处理消息逻辑
               if (data.type === 'msgUnreadCount') {
                 commit('SET_MESSAGECOUNT', data.data);

+ 5 - 0
utils/WebSocketManager.js

@@ -1,5 +1,6 @@
 import config from '@/config'
 const baseUrl = config.baseUrl
+import store from '@/store'
 class WebSocketManager {
     constructor(userId) {
         // 1用户 2志愿者
@@ -59,6 +60,10 @@ class WebSocketManager {
                 if (typeof this.onMessageCallback === 'function') {
                     this.onMessageCallback(data); // 回调通知外部
                 }
+                if (data.type === 'msgUnreadCount') {
+					console.log("TCL: WebSocketManager -> _setupEventListeners -> msgUnreadCount", data.data)
+                    store.dispatch('handleMessageCount', data.data)
+                 }
             } catch (e) {
                 console.error('消息解析失败:', res.data);
             }

+ 1 - 1
utils/index.js

@@ -10,7 +10,7 @@ export const getNavBarHeight = () =>{
             console.log('res.platform',res.platform);
             
             const navBarHeight =
-                res.platform === 'android' ? 48 : 44 + res.statusBarHeight;
+               (res.platform === 'android' ? 48 : 44) + res.statusBarHeight;
                 resolve( { statusBarHeight, navBarHeight })
             },
             fail: (err) => {