chenjj 21 시간 전
부모
커밋
678faef193
5개의 변경된 파일54개의 추가작업 그리고 38개의 파일을 삭제
  1. 3 2
      pages/chat.vue
  2. 8 2
      pages/mine.vue
  3. 4 2
      pages_orderuser/pages/talk/pages/index/index.vue
  4. 24 19
      store/modules/user.ts
  5. 15 13
      utils/WebSocketManager.js

+ 3 - 2
pages/chat.vue

@@ -9,12 +9,12 @@
                         <img src="/static/serverImg/mine/user.png" alt="" class="chat-img" v-else/>
                         <view class="chat-box">
                             <view class="chat-top">
-                                <text class="chat-name">{{ item.msgType === '1'? '系统消息': type?item.userName:item.volunteerName }}</text>
+                                <text class="chat-name">{{ item.conversationType === '1'? '系统消息': type?item.volunteerName: item.userName }}</text>
                                 <text class="chat-time">{{ handlerData(item.newestMsgTime || item.createTime) }}</text>
                             </view>
                             <view class="chat-bottom">
                                 <text class="chat-text">
-                                   {{ item.msgContent }}
+                                   {{ item.newestMsgContent }}
                                 </text>
                                 <!-- <view class="chat-num">
                                     2
@@ -50,6 +50,7 @@ import { getList } from '@/api/conversation.js';
 
 const { } = useDict();
 const userType = uni.getStorageSync('userType') //读取本地存储
+console.log("TCL: userType", userType)
 const list = ref([])
 
 

+ 8 - 2
pages/mine.vue

@@ -175,9 +175,12 @@ const adminList = ref(
 			iconName: 'account',
 			operate: () => {
 				uni.setStorageSync('userType', 1);
+				store.dispatch('GetInfo').then((res) => {
+				userInfo.value = store.state.user;
 				uni.reLaunch({
 					url: '/pages/index'
 				});
+			});
 			}
 		},
 	]);
@@ -213,8 +216,11 @@ const userList = ref([
 		iconName: 'account',
 		operate: () => {
 			uni.setStorageSync('userType', 2);
-			uni.reLaunch({
-				url: '/pages/index'
+			store.dispatch('GetInfo').then((res) => {
+				userInfo.value = store.state.user;
+				uni.reLaunch({
+					url: '/pages/index'
+				});
 			});
 		}
 	}

+ 4 - 2
pages_orderuser/pages/talk/pages/index/index.vue

@@ -14,7 +14,7 @@
 								<img src="/static/serverImg/mine/user.png" alt="" class="pic" mode="aspectFill"
 									v-else />
 								<view class="content">
-									<template v-if="item.contentType === 'image'">
+									<template v-if="item.msgType === '2'">
 										<image :src="item.msgContent" mode="widthFix" style="width: 400rpx;"></image>
 									</template>
 									<template v-else>
@@ -208,11 +208,13 @@ export default {
 				msgContent: content,
 				msgSendTime: new Date().getTime(),
 				senderId: this.userId,
+				msgType: contentType =='text'?'1':'2',//消息类型 1文字消息 2图片消息
 			}
 			const res = await sendMsg({
 				...this.orderInfo,
 				msgContent: content,
-				system: this.system
+				system: this.system,
+				msgType: data.msgType
 			});
 			this.talkList.unshift({ ...data, code: res.code });
 			this.scrollView.intoView = `msg-${data.msgSendTime}`;

+ 24 - 19
store/modules/user.ts

@@ -15,10 +15,11 @@ const user: Module<UserState, UserState> = {
     avatar: storage.get(constant.avatar),
     roles: storage.get(constant.roles),
     permissions: storage.get(constant.permissions),
-	  userOrWorker: storage.get(constant.userOrWorker),
-    nickName:  storage.get(constant.nickName),
+    userOrWorker: storage.get(constant.userOrWorker),
+    nickName: storage.get(constant.nickName),
     userId: storage.get(constant.userId),
-    messageCount:0
+    messageCount: 0,
+    wsManager: null
   },
   mutations: {
     SET_TOKEN: (state, token: string) => {
@@ -79,7 +80,7 @@ const user: Module<UserState, UserState> = {
       return new Promise((resolve, reject) => {
         getInfo().then((res: any) => {
           const user = res.user
-          const avatar = (user == null || user.avatar == "" || user.avatar == null) ? "/static/serverImg/mine/user.png" :  user.avatar
+          const avatar = (user == null || user.avatar == "" || user.avatar == null) ? "/static/serverImg/mine/user.png" : user.avatar
           const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
           if (res.roles && res.roles.length > 0) {
             commit('SET_ROLES', res.roles)
@@ -87,32 +88,36 @@ const user: Module<UserState, UserState> = {
           } else {
             commit('SET_ROLES', ['ROLE_DEFAULT'])
           }
-		  
-		  console.log(res.user.userOrWorker, '>>>>>>res.user.userOrWorker');
-		  
-		  if (res.user)  commit('SET_USERORWORKER', res.user.userOrWorker)
-		  
-		  console.log(state.userOrWorker, '>>>>>>');
-		  
+
+          console.log(res.user.userOrWorker, '>>>>>>res.user.userOrWorker');
+
+          if (res.user) commit('SET_USERORWORKER', res.user.userOrWorker)
+
+          console.log(state.userOrWorker, '>>>>>>');
+
           commit('SET_NAME', username)
           commit('SET_AVATAR', avatar)
           commit('SET_NICKNAME', res.user.nickName)
           commit('SET_USERID', res.user.userId)
 
 
-
-          //获取账户时,连接soket 
-          const wsManager = new WebSocketManager(res.user.userId);
+          console.log("TCL: GetInfo -> wsManager1", this.wsManager)
+          if (this.wsManager) {
+            this.wsManager.closeConnection();
+          }else{
+            //获取账户时,连接soket 
+            this.wsManager = new WebSocketManager(res.user.userId);
             // 设置消息回调
-            wsManager.onMessage(data => {
-              console.log('app-接收到的消息:', data);
+            this.wsManager.onMessage(data => {
               // 处理消息逻辑
-              if(data.type === 'msgUnreadCount'){
+              if (data.type === 'msgUnreadCount') {
                 commit('SET_MESSAGECOUNT', data.data);
               }
             });
             // 建立连接
-            wsManager.connect();
+            this.wsManager.connect();
+          }
+          
 
           resolve(res)
         }).catch(error => {
@@ -140,7 +145,7 @@ const user: Module<UserState, UserState> = {
         })
       })
     },
-    handleMessageCount({ commit, state },count) { 
+    handleMessageCount({ commit, state }, count) {
       commit('SET_MESSAGECOUNT', count);
     },
   }

+ 15 - 13
utils/WebSocketManager.js

@@ -14,6 +14,7 @@ class WebSocketManager {
         this.reconnectTimer = null;
         this.isConnecting = false;
         this.onMessageCallback = null;
+
     }
 
     /**
@@ -23,17 +24,18 @@ class WebSocketManager {
         if (this.isConnecting || !this.userId) {
             console.warn('正在连接或缺少用户ID');
             return;
+        }
+         // 如果已有连接,先关闭旧连接
+        if (this.isConnected) {
+            console.log('已存在连接,正在关闭旧连接...');
+            this.socketTask.close();
         }
         this.isConnecting = true;
-
         console.log('建立 WebSocket 连接:', this.url);
-        if(!this.socketTask){
-            this.socketTask = uni.connectSocket({
-                url: this.url,
-                success: () => console.log('WebSocket 连接创建成功'),
-            });
-        }
-       
+        this.socketTask = uni.connectSocket({
+            url: this.url,
+            success: () => console.log('WebSocket 连接创建成功'),
+        });
 
         this._setupEventListeners();
     }
@@ -128,11 +130,11 @@ class WebSocketManager {
      * 主动关闭连接
      */
     closeConnection() {
-        if (this.socketTask) {
-            this.socketTask.close();
-            this.socketTask = null;
-        }
-        this.stopHeartbeat();
+        console.log('主动关闭连接',this.socketTask);
+        
+        this.socketTask.close();
+        this.socketTask = null;
+        // this.stopHeartbeat();
     }
 
     /**