luke 6 лет назад
Родитель
Сommit
cb033f5d42
1 измененных файлов с 45 добавлено и 28 удалено
  1. 45 28
      pc_chat/src/components/chat.vue

+ 45 - 28
pc_chat/src/components/chat.vue

@@ -403,6 +403,7 @@
 <script>
 	import '../css/index.css';
 	import frce from '../assets/frce.js';
+
 	export default {
 		data() {
 			return {
@@ -478,12 +479,12 @@
 				allLoading: true, //整个页面加载中
 				webTime: '',//网络时间
 				/*****************websockt心跳变量*******************/
-				reconnectData:null,
-				lockReconnect:false,    //避免重复连接,因为onerror之后会立即触发 onclose
-				timeout:10000,          //10s一次心跳检测
-				timeoutObj:null,
-				serverTimeoutObj:null,
-				apiToken:''
+				reconnectData: null,
+				lockReconnect: false,    //避免重复连接,因为onerror之后会立即触发 onclose
+				timeout: 10000,          //10s一次心跳检测
+				timeoutObj: null,
+				serverTimeoutObj: null,
+				apiToken: ''
 			}
 		},
 		methods: {
@@ -529,7 +530,7 @@
 				}
 				//对图片进行压缩
 				const imgSize = file.size / 1024 / 1024;
-				if(imgSize > 0.5) {
+				if (imgSize > 0.5) {
 					const _this = this
 					return new Promise(resolve => {
 						const reader = new FileReader()
@@ -547,7 +548,9 @@
 							const blobData = _this.dataURItoBlob(dataUrl, file.type);
 							resolve(blobData)
 						}
-						reader.onload = (e => { image.src = e.target.result; });
+						reader.onload = (e => {
+							image.src = e.target.result;
+						});
 						reader.readAsDataURL(file);
 					})
 				}
@@ -556,7 +559,7 @@
 			dataURItoBlob(dataURI, type) {
 				var binary = atob(dataURI.split(',')[1]);
 				var array = [];
-				for(var i = 0; i < binary.length; i++) {
+				for (var i = 0; i < binary.length; i++) {
 					array.push(binary.charCodeAt(i));
 				}
 				return new Blob([new Uint8Array(array)], {type: type});
@@ -604,8 +607,8 @@
 				this.returnTimer = '';
 				// this.automaticRolling();
 				this.initWebSocket();
-				this.chatCont =[];
-				this.customerSviceChat =[];
+				this.chatCont = [];
+				this.customerSviceChat = [];
 
 				//this.questionShow = true;
 			},
@@ -825,15 +828,15 @@
 					if (this.machineAndAtl == 100) {
 						let num = this.customerSviceChat.length - 1;
 						if (this.customerSviceChat[num].type == "user") {
-							if(redata.data.webTime.split(":")[1] - this.webTime.split(":")[1] > 2){
-								this.$set(this.customerSviceChat[num],'time', redata.data.webTime);
+							if (redata.data.webTime.split(":")[1] - this.webTime.split(":")[1] > 2) {
+								this.$set(this.customerSviceChat[num], 'time', redata.data.webTime);
 								this.webTime = redata.data.webTime;
-							}else{
-								this.$set(this.customerSviceChat[num],'time', '');
+							} else {
+								this.$set(this.customerSviceChat[num], 'time', '');
 							}
 						}
-					} 
-					
+					}
+
 				}
 
 				//会话超时提示
@@ -907,6 +910,20 @@
 						})
 						this.automaticRolling();
 					}
+
+					let num = this.chatCont.length - 2;
+					if (this.webTime) {
+						if (redata.data.time.split(":")[1] - this.webTime.split(":")[1] > 2) {
+							this.$set(this.chatCont[num], 'time', redata.data.time);
+							this.webTime = redata.data.time;
+						} else {
+							this.$set(this.chatCont[num], 'time', '');
+						}
+					} else {
+						this.webTime = redata.data.time;
+						this.$set(this.chatCont[num], 'time', redata.data.time);
+					}
+
 				}
 				// 没有客服上线.
 				if (redata.message_type == 'notice') {
@@ -951,32 +968,32 @@
 
 			/***************************************/
 			//socket重连
-			reconnect(){
-				if(this.lockReconnect){       //这里很关键,因为连接失败之后之后会相继触发 连接关闭,不然会连接上两个 WebSocket
+			reconnect() {
+				if (this.lockReconnect) {       //这里很关键,因为连接失败之后之后会相继触发 连接关闭,不然会连接上两个 WebSocket
 					return
 				}
 				this.lockReconnect = true;
 				this.reconnectData && clearTimeout(this.reconnectData);
-				this.reconnectData = setTimeout(()=>{
+				this.reconnectData = setTimeout(() => {
 
 					this.initWebSocket();
 					this.lockReconnect = false;
-				},5000)
-			},  
+				}, 5000)
+			},
 
 			/*************************************/
 			//心跳检测
-			heatBeat(){
+			heatBeat() {
 				this.timeoutObj && clearTimeout(this.timeoutObj);
 				this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
-				this.timeoutObj = setTimeout(()=>{
-					console.log('发送',{type:'ping'});
-					this.websocketsend(JSON.stringify({type:'ping'}))   //根据后台要求发送
-					this.serverTimeoutObj = setTimeout(()=> {
+				this.timeoutObj = setTimeout(() => {
+					console.log('发送', {type: 'ping'});
+					this.websocketsend(JSON.stringify({type: 'ping'}))   //根据后台要求发送
+					this.serverTimeoutObj = setTimeout(() => {
 						this.websock.close(); //如果  5秒之后我们没有收到 后台返回的心跳检测数据 断开socket,断开后会启动重连机制
 					}, 5000);
 				}, this.timeout)
-			},  
+			},
 
 
 			/******************消息发送数据处理********************/