|
|
@@ -3,23 +3,215 @@ import {
|
|
|
setStorage
|
|
|
} from '@/utils/localStorage';
|
|
|
window.sessionTime = new Date();
|
|
|
-var util = {
|
|
|
- reformParam(methodName, para) {
|
|
|
- var parameter = {}
|
|
|
- parameter['merchantid'] = '1'
|
|
|
- parameter['version'] = '1'
|
|
|
- parameter['sign_type'] = 'RSA'
|
|
|
- parameter['sign'] = '123'
|
|
|
- parameter['charset'] = 'UTF-8'
|
|
|
- parameter['method'] = methodName
|
|
|
- var context = ''
|
|
|
- for (var key in para) {
|
|
|
- context += '&' + key + '=' + para[key]
|
|
|
- }
|
|
|
- parameter['context'] = context
|
|
|
- return parameter
|
|
|
- },
|
|
|
+/**
|
|
|
+ * websocket配置项
|
|
|
+ * @type {{serverTimeoutObj: null, timeoutObj: null, timeoutnum: null, lockReconnect: boolean, ws: null, params: {houseId: null, openid: null, userid: null}, timeout: number}}
|
|
|
+ */
|
|
|
+let wsConfig = {
|
|
|
+ ws: null,
|
|
|
+ lockReconnect: false, //是否真正建立连接
|
|
|
+ interval: null,
|
|
|
+ timeoutObj: null, //心跳心跳倒计时
|
|
|
+ serverTimeoutObj: null, // 服务连接超时时间
|
|
|
+ timeoutnum: null, // 断开重连倒计时
|
|
|
+ timeout: 58 * 1000, // 58秒一次心跳
|
|
|
+ // 建立ws连接时,所需参数
|
|
|
+ params: {
|
|
|
+ openid: null,
|
|
|
+ userid: null,
|
|
|
+ houseId: null
|
|
|
+ }
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 初始化websocket
|
|
|
+ */
|
|
|
+function initWebsocket() {
|
|
|
+ let str1 = parseInt(Math.random() * 10);
|
|
|
+ let str2 = parseInt(Math.random() * 10);
|
|
|
+ let str3 = parseInt(Math.random() * 10);
|
|
|
+ let str4 = "" + str1 + str2 + str3;
|
|
|
+ //初始化weosocket
|
|
|
+ let websocketUrl = $config.ws_url;
|
|
|
+ const wsuri =
|
|
|
+ `${websocketUrl}gs-guide-websocket/` +
|
|
|
+ str4 +
|
|
|
+ "/shi23jfrw" +
|
|
|
+ "/websocket";
|
|
|
+ wsConfig.ws = new WebSocket(wsuri);
|
|
|
+ // 连接建立时触发
|
|
|
+ wsConfig.ws.onopen = websocketonopen;
|
|
|
+ // 客户端接收服务端数据时触发
|
|
|
+ wsConfig.ws.onmessage = websocketonmessage;
|
|
|
+ // 通信发生错误时触发
|
|
|
+ wsConfig.ws.onerror = websocketonerror;
|
|
|
+ // 连接关闭时触发
|
|
|
+ wsConfig.ws.onclose = websocketclose;
|
|
|
+ return wsConfig.ws;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 连接建立时触发
|
|
|
+ */
|
|
|
+function websocketonopen() {
|
|
|
+ let data = initPage();
|
|
|
+ console.log('建立ws连接', data)
|
|
|
+ start();
|
|
|
+ //发送链接身份数据
|
|
|
+ connectSend(data.openId, data.userId, data.houseId);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 客户端接收服务端数据时触发
|
|
|
+ * @param e
|
|
|
+ */
|
|
|
+function websocketonmessage(e) {
|
|
|
+ console.log('客户端接收服务端数据时触发', e);
|
|
|
+ //收到服务器信息,心跳重置
|
|
|
+ reset();
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通信发生错误时触发
|
|
|
+ */
|
|
|
+function websocketonerror() {
|
|
|
+ console.log("出现错误");
|
|
|
+ reconnect();
|
|
|
+}
|
|
|
|
|
|
+/**
|
|
|
+ * 连接关闭时触发
|
|
|
+ * @param e
|
|
|
+ */
|
|
|
+function websocketclose(e) {
|
|
|
+ //关闭
|
|
|
+ console.log("断开连接", e);
|
|
|
+ //重连
|
|
|
+ reconnect();
|
|
|
+}
|
|
|
+
|
|
|
+function websocketsend(Data) {
|
|
|
+ console.log('ws状态', wsConfig.ws.readyState)
|
|
|
+ if(wsConfig.ws.readyState==1){
|
|
|
+ //数据发送
|
|
|
+ wsConfig.ws.send(Data);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 解析页面路由参数
|
|
|
+ * @param name
|
|
|
+ * @returns {null}
|
|
|
+ */
|
|
|
+function getQueryString(name) {
|
|
|
+ let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
|
|
|
+ let arr = window.location.href.split('?');
|
|
|
+ let result = null;
|
|
|
+ arr.forEach((item, index) => {
|
|
|
+ let res = item.match(reg);
|
|
|
+ if (res != null) {
|
|
|
+ result = unescape(res[2]);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return result
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 初始进入页面参数
|
|
|
+ */
|
|
|
+function initPage(){
|
|
|
+ let queryObj = getStorage('queryObj') ? JSON.parse(getStorage('queryObj')) : null;
|
|
|
+ let urlObj = queryObj || util.getUrlParams(location.href) || {};
|
|
|
+ let brandId = urlObj.special_ID || $config.brandId || ''
|
|
|
+ return {
|
|
|
+ session: urlObj.session || '',
|
|
|
+ // 项目id,默认仙女山项目
|
|
|
+ houseId: urlObj.xcxHouseId || '',
|
|
|
+ userId: urlObj.leavePhoneCustomerId || '',
|
|
|
+ openId: urlObj.openid || '',
|
|
|
+ brandId: brandId || '',
|
|
|
+ product: urlObj.xcxHouseId || '',
|
|
|
+ fromPlatform: urlObj.fromProduce || '',
|
|
|
+ platform: urlObj.platform || '',
|
|
|
+ scene: urlObj.scene1 || '',
|
|
|
+ layoutName: urlObj.layoutName || '',
|
|
|
+ }
|
|
|
+}
|
|
|
+/**
|
|
|
+ * 发送ws连接请求
|
|
|
+ */
|
|
|
+function connectSend(openid, userid, houseId) {
|
|
|
+ console.log('建立ws通信', openid, userid, houseId)
|
|
|
+ const param = [
|
|
|
+ "CONNECT" +
|
|
|
+ "\nopenId:" +
|
|
|
+ openid +
|
|
|
+ "\nplatform:" +
|
|
|
+ "1" +
|
|
|
+ "\nhouseId:" +
|
|
|
+ houseId +
|
|
|
+ "\nuserId:" +
|
|
|
+ userid +
|
|
|
+ "\naccept-version:1.1,1.0\nheart-beat:5000,5000\n\n\u0000",
|
|
|
+ ];
|
|
|
+ websocketsend(JSON.stringify(param));
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 重新连接ws
|
|
|
+ */
|
|
|
+function reconnect() {
|
|
|
+ console.log('重新连接ws');
|
|
|
+ //重新连接
|
|
|
+ if (wsConfig.lockReconnect) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ wsConfig.lockReconnect = true;
|
|
|
+ //没连接上会一直重连,设置延迟避免请求过多
|
|
|
+ wsConfig.timeoutnum && clearTimeout(wsConfig.timeoutnum);
|
|
|
+ wsConfig.timeoutnum = setTimeout(() => {
|
|
|
+ //新连接
|
|
|
+ initWebsocket()
|
|
|
+ wsConfig.lockReconnect = false
|
|
|
+ }, 5000)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 重置ws连接
|
|
|
+ */
|
|
|
+function reset() {
|
|
|
+ console.log('重置ws', wsConfig)
|
|
|
+ //重置心跳
|
|
|
+ clearTimeout(wsConfig.timeoutObj);
|
|
|
+ //清除时间
|
|
|
+ clearTimeout(wsConfig.serverTimeoutObj);
|
|
|
+ //重启心跳
|
|
|
+ start();
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 开启ws连接
|
|
|
+ */
|
|
|
+function start() {
|
|
|
+ let ws = wsConfig.ws;
|
|
|
+ //开启心跳
|
|
|
+ console.log("开启心跳", wsConfig);
|
|
|
+ wsConfig.timeoutObj && clearTimeout(wsConfig.timeoutObj);
|
|
|
+ wsConfig.serverTimeoutObj && clearTimeout(wsConfig.serverTimeoutObj);
|
|
|
+ wsConfig.timeoutObj = setTimeout(function() {
|
|
|
+ //这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
|
+ if (ws.readyState == 1) {
|
|
|
+ //如果连接正常
|
|
|
+ // websock.send("heartCheck"); //这里可以自己跟后端约定
|
|
|
+ } else {
|
|
|
+ //否则重连
|
|
|
+ reconnect();
|
|
|
+ }
|
|
|
+ wsConfig.serverTimeoutObj = setTimeout(function() {
|
|
|
+ //超时关闭
|
|
|
+ ws.close();
|
|
|
+ }, wsConfig.timeout);
|
|
|
+ }, wsConfig.timeout);
|
|
|
+}
|
|
|
+var util = {
|
|
|
dateFormat(date, fmt) {
|
|
|
let ret
|
|
|
const opt = {
|
|
|
@@ -225,7 +417,7 @@ var util = {
|
|
|
// return data;
|
|
|
// app.globalData.session_id = data.session
|
|
|
// app.globalData.sessionTime = timeNow;
|
|
|
- requestConfig('upload', data, true);
|
|
|
+ // requestConfig('upload', data, true);
|
|
|
// let param = ["SEND" +
|
|
|
// "\nproject:" + "elab-marketing-system" +
|
|
|
// "\nmethod:" + 'POST' +
|
|
|
@@ -236,6 +428,22 @@ var util = {
|
|
|
// ];
|
|
|
// app.wsSendOrder(param,data);//socket 消息发送
|
|
|
console.warn("***mook***", (data.pvId || data.clkId || data.eventId), data)
|
|
|
+ let param = [
|
|
|
+ "SEND" +
|
|
|
+ "\nproject:" +
|
|
|
+ "elab-marketing-system" +
|
|
|
+ "\nmethod:" +
|
|
|
+ "POST" +
|
|
|
+ "\npath:" +
|
|
|
+ "/behavior/brandMiniWeb/upload" +
|
|
|
+ "\ndestination:" +
|
|
|
+ "/ws/remote/invoke" +
|
|
|
+ "\n\n" +
|
|
|
+ JSON.stringify(data) +
|
|
|
+ "\u0000",
|
|
|
+ ];
|
|
|
+ console.log("上报埋点数据", param);
|
|
|
+ websocketsend(JSON.stringify(param));
|
|
|
} catch (e) {
|
|
|
console.warn("***util.js-onError***", e);
|
|
|
}
|
|
|
@@ -249,6 +457,7 @@ var util = {
|
|
|
})
|
|
|
return session;
|
|
|
},
|
|
|
+ initWebsocket:initWebsocket,
|
|
|
};
|
|
|
window.from_session = util.getUrlParams(location.href).session || '';
|
|
|
window.from_cookie = util.getUrlParams(location.href).cookie || '';
|