// type为true只获取时间戳10位 否则把连带毫秒数都拿出来
timestamp(type) {
var timestamp = Date.parse(new Date());
if (type != undefined) {
var temp = timestamp.toString();
timestamp = temp.substr(0, 10);
}
return timestamp;
},
websoket() {
var that = this;
console.log("websoket");
if (common.websocket.socket_open == true) {
console.log("已经链接了,不必重复请求");
return false;
}
common.websocket.socket_open = false;
var socket_first = true;
uni.connectSocket({
url: 'wss://server.connect.la/wss',
header: {
'content-type': 'application/json'
}
});
//打开成功
uni.onSocketOpen(function(res) {
common.websocket.socket_open = true;
clearTimeout(common.websocket.timer_id);
console.log('WebSocket连接已打开!');
console.log(res);
if (socket_first == true) {
socket_first = false;
that.sendSocketMessage(0);
}
});
//打开失败
uni.onSocketError(function(res) {
console.log('WebSocket连接打开失败,请检查!');
common.websocket.socket_open = false;
console.log(res);
clearTimeout(common.websocket.timer_id);
common.websocket.timer_id = setTimeout(function() {
that.websoket();
}, 8000);
});
//关闭回调
uni.onSocketClose(function(res) {
console.log('WebSocket 已关闭!');
console.log(res);
if (common.websocket.socket_open == true) {
return false;
}
//发一次心跳包 看能否接收成功 接收成功 说明还有长链接 不做处理
that.sendSocketMessage(0);
});
//接收消息
uni.onSocketMessage(function(res) {
console.log('收到服务器内容:' + res.data);
console.log(typeof(res));
// console.log(res.data.key);
console.log(res);
console.log(res.data);
var params = JSON.parse(res.data);
if (params == undefined) {
console.log("收到服务器空内容");
return false;
}
console.log(params);
console.log('类型:' + params.kind)
if (params.kind != undefined && params.kind == 110) {
common.websocket.socket_open = false;
common.websocket.timer_id = setTimeout(function() {
that.websoket();
}, 8000);
} else if (params.kind == 99) {
common.websocket.socket_open = true;
console.log("被挤下线 不做处理");
return false;
} else if (params.kind == 'all') {
//检测新版本
} else if (params.kind == 'order_receive_rider') {
// #ifdef APP-PLUS
plus.push.createMessage(params.data.shop_name + ' 已接单,记得前往领取商品.', {
'data': {
kind: params.kind,
order_id: params.data.order_id
}
}, {
sound: "none",
cover: false
});
// #endif
uni.$emit('update_orderview_map', {
msg: '商家接单,更新订单详情UI'
});
} else if (params.kind == 'order_refund_rider') {
// #ifdef APP-PLUS
const bgAudioMannager = uni.getBackgroundAudioManager();
bgAudioMannager.title = "退款订单提醒";
bgAudioMannager.stop();
var tiemstmap = that.timestamp();
bgAudioMannager.src = 'http://xxxx/static/yida/sounds/tuikuan.mp3?a=' + tiemstmap;
try {
bgAudioMannager.play();
} catch (e) {
console.log("获取不到内容 退款订单提醒 bgAudioMannager.play()");
}
bgAudioMannager.onPlay(() = > {
console.log('开始播放退款背景声音');
common.playsound = false;
});
bgAudioMannager.onStop(() = > {
console.log('播放退款背景声音停止');
common.playsound = true;
});
bgAudioMannager.onEnded(() = > {
console.log('播放退款背景声音自然播放结束');
common.playsound = true;
});
bgAudioMannager.onError(() = > {
console.log('播放退款错误事件');
common.playsound = true;
});
plus.push.createMessage('有退款订单,请查看', {
'data': {
kind: params.kind,
order_id: params.data.order_id
}
}, {
sound: "none",
cover: false
});
// #endif
uni.$emit('update_orderview_map', {
msg: '有退款订单,更新订单详情UI'
});
} else if (params.kind == 'apply_failed') {
plus.push.createMessage('您有一笔资金提现失败,原因:' + params.data.message, {
'data': {
kind: params.kind,
apply_id: params.data.apply_id
}
}, {
sound: "none",
cover: false
});
}
});
},
//发送消息
sendSocketMessage(kind) {
console.log("sendSocketMessage:" + kind);
var that = this;
console.log(common.websocket.socket_open);
if (common.websocket.socket_open == true) {
if (kind == 0) {
//开启心跳包检测
//清除上次定时器
if (common.websocket.heart_id != undefined) {
clearTimeout(common.websocket.heart_id);
common.websocket.heart_id = undefined;
}
common.websocket.heart_id = setTimeout(function() {
that.sendSocketMessage(kind);
}, 5000);
}
var data = {
kind: kind,
token: common.userinfo.token,
'from': common.client,
'longitude': common.longitude,
'latitude': common.latitude
};
uni.sendSocketMessage({
data: JSON.stringify(data),
success: function(res) {
console.log('WebSocket sendSocketMessage success');
console.log(res);
},
fail: function(res) {
console.log('WebSocket sendSocketMessage fail');
common.websocket.socket_open = false;
that.closeWebSocket();
console.log(res);
common.websocket.timer_id = setTimeout(function() {
that.websoket();
}, 1000);
}
});
}
},
//主动关闭socket
closeWebSocket() {
if (common.websocket.socket_open == true) {
common.websocket.socket_open = false;
uni.closeSocket({
reason: '正常关闭websocket连接'
});
}
}
由于离线不会推送消息。所以消息池里的数据会一直存在
超时消息需要隐藏
var cur_timestamp = parseInt(that.timestamp(true));
//检测消息是否过期
if (params.timestamp!=undefined) {
var news_timestamp = parseInt(params.timestamp);
if (cur_timestamp-news_timestamp>86400*2){
//过期消息 不予以显示
return false;
}
//补全为13位时间戳
var newsDate = new Date(params.timestamp+'000');
} else {
var newsDate = new Date();
}