import webSocket from '../webSocket.js';
export default {
isIosAndroid() {
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1,
isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
isChrome = u.indexOf('Safari');
return {
isAndroid,
isiOS,
isChrome
}
},
websocktSend(paramsObj) {
var userId = localStorage.getItem('userId');
var sendMsg = () => {
window.socketServer.send(JSON.stringify(paramsObj));
}
if (window.socketServer.readyState == 1) {
sendMsg();
} else {
webSocket.call(this, wsconPath[sceneParam] + userId).then(() => {
sendMsg();
});
}
},
showDiffTime: function (startDate) {
if (!startDate) {
return;
}
var startDate = startDate.replace(new RegExp(/-/gm), "/");
var startDateB = new Date(startDate);
var updateHour = startDateB.getHours(),
updateMin = startDateB.getMinutes();
updateHour = updateHour < 10 ? '0' + updateHour : updateHour;
updateMin = updateMin < 10 ? '0' + updateMin : updateMin;
var endDate = new Date();
var diff = endDate.getTime() - startDateB.getTime();
var days = Math.floor(diff / (24 * 3600 * 1000));
if (days > 0) {
if (days == 1) {
return "昨天 " + updateHour + ':' + updateMin;
}
if (days == 2) {
return "前天 " + updateHour + ':' + updateMin;
}
if (days > 2) {
return startDate.split(' ')[0] + ' ' + updateHour + ':' + updateMin;
}
}
if (days == 0) {
return updateHour + ':' + updateMin;
}
},
debounce(fn, delay) {
delay = delay || 200;
var timer = null;
return function () {
var arg = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(this, arg);
}, delay);
}
},
}
var heartCheck = {
timeout: 3000,
timeoutObj: null,
reset: function(){
clearTimeout(this.timeoutObj);
this.start();
},
start: function(){
this.timeoutObj = setTimeout(function(){
var userId = localStorage.getItem('userId');
if(!userId){
return;
}
window.socketServer.send(JSON.stringify({
photographerObjectId:'',
type:'6',
leavingContent:'',
photographerId:userId
}));
},this.timeout)
}
}
function createWSConnect(path){
var _store = this;
if (typeof WebSocket === "undefined") {
alert("您的浏览器不支持socket");
} else {
return new Promise((resolve,reject)=>{
var socket = new WebSocket(path);
socket.onopen = function(){
console.log("socket连接成功!!!");
window.socketServer = socket;
resolve(socket);
heartCheck.start();
};
socket.onclose = function (e) {
console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean)
console.log(e);
reconnect(path);
}
socket.onerror = function(){
socket.close();
reject();
console.log("连接错误");
reconnect(path);
};
socket.onmessage = function(res){
if(res.data==1){
heartCheck.reset();
return;
}
var data = res.data && JSON.parse(res.data);
updateMsgForType.call(_store,data);
};
})
}
}
reconnect.lockReconnect = false;
reconnect.timer = '';
function reconnect(url) {
if (reconnect.lockReconnect) return;
reconnect.lockReconnect = true;
reconnect.timer && clearTimeout(reconnect.timer);
reconnect.timer = setTimeout(function () {
createWSConnect(url);
reconnect.lockReconnect = false;
}, 4000);
}
function updateMsgForType(data){
if(data.error){
this.$Toast(data.error);
return;
}
this.dispatch('UpdateChatBadge',{count:data.count})
switch(data.type){
case '0':
this.dispatch('communication/addOneToOne',data.siteComm)
break;
case '1':
this.dispatch('communication/addOneToOne',data.siteComm)
break;
case '2':
break;
case '3':
this.dispatch('communication/withdrawOneToOne',{count:data.siteComm})
break;
case '4':
this.dispatch('communication/delOneToOne',data.siteComm)
break;
case '5':
this.dispatch('communication/delAllOneToOne',data.siteComm)
break;
}
}
export default createWSConnect;
utils.websocktSend.call(this.$store, paramsObj);