Prechádzať zdrojové kódy

修复发送消息时不及时显示当前消息问题
添加发送频率限制

liling 1 rok pred
rodič
commit
9c359b2eb6

+ 9 - 2
src/store/modules/websocket.js

@@ -11,7 +11,8 @@ const useWebsoctStore = defineStore('websoct', {
     socket: null,
     messOne:{},
     falseMessag:false,//消息是否发送成功
-    noReadList: null
+    noReadList: null,
+    lastSendTime:0, //最后发送消息时间
   }),
   actions: {
     connect() {
@@ -57,10 +58,16 @@ const useWebsoctStore = defineStore('websoct', {
       })
     },
     sendMessage(message) {
+      if(this.lastSendTime>0 && (new Date().getTime()- this.lastSendTime)<4000){
+        return new Promise((resolve, reject) => {
+            ElMessage({ message: "消息发送频率太快", type: "error" });
+            reject('发送频率太快')
+        })
+      }
       var that=this
       return new Promise((resolve, reject) => {
 				msgSend(message).then(res => {
-           this.newMessage = true // 标记有新的消息
+          that.lastSendTime = new Date().getTime();
 					resolve(res)
 				}).catch(error => {
           ElMessage({ message: "发送消息失败", type: "error" });

+ 20 - 7
src/views/liveChat/index.vue

@@ -279,7 +279,7 @@ const msgSendClick = (event) => {
 };
 const msgSendClick2 = (event,icon) => {
   if(sendCont.data.toId==null||sendCont.data.toId==0){
-    return ElMessage({ message: "请添加或者会话人员", type: "error" });
+    return ElMessage({ message: "请添加或者选择会话人员", type: "error" });
     return
   }
   if ((!event.shiftKey && event.keyCode == 13 && !event.ctrlKey)||icon) {
@@ -294,16 +294,21 @@ const msgSendClick2 = (event,icon) => {
       fileList: [],
       toId: sendCont.data.toId,
     };
-    websoctStore.sendMessage(message);
-    boottmScroll();
-    messageText.value = "";
-    //  将光标设置到输入框第一行
+    websoctStore.sendMessage(message).then((res) => {
+      if(res.code==200){
+        //发送成功
+        messageText.value = "";
+        msgRecordEvent(sendCont.data.toId)
+      }
+    }).catch((err) => {
+      //发送失败了
+    })
   }
 }
 //发送文件确认按钮
 const fileChangeMsg = async (val) => {
   if(sendCont.data.toId==null||sendCont.data.toId==0){
-    return ElMessage({ message: "请添加或者会话人员", type: "error" });
+    return ElMessage({ message: "请添加或者选择会话人员", type: "error" });
     return
   }
   const message = {
@@ -313,7 +318,15 @@ const fileChangeMsg = async (val) => {
     toId: sendCont.data.toId,
   };
   sendId.value = val.id;
-  websoctStore.sendMessage(message);
+  websoctStore.sendMessage(message).then((res) => {
+      if(res.code==200){
+        //发送成功
+        messageText.value = "";
+        msgRecordEvent(sendCont.data.toId)
+      }
+  }).catch((err) => {
+      //发送失败了
+  })
 };
 //点击发送文件图标
 const fileUserTreeData = reactive({ data: {} });