129 lines
3.6 KiB
JavaScript
129 lines
3.6 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../common/vendor.js");
|
|
const store_index = require("../store/index.js");
|
|
const config = require("../config.js");
|
|
const init = () => {
|
|
if (!common_vendor.index.getStorageSync("token")) {
|
|
common_vendor.index.__f__("log", "at utils/sok.js:7", "sok-未登录");
|
|
return;
|
|
} else {
|
|
common_vendor.index.__f__("log", "at utils/sok.js:10", "sok-登录");
|
|
}
|
|
var timeout = 1e4;
|
|
var timeoutObj = null;
|
|
var timeAnt = 0;
|
|
setInterval(() => {
|
|
timeAnt++;
|
|
}, 1e3);
|
|
openConnection();
|
|
function checkOpenSocket() {
|
|
common_vendor.index.sendSocketMessage({
|
|
data: "ping",
|
|
success: (res) => {
|
|
timeAnt = 0;
|
|
return;
|
|
},
|
|
fail: (err) => {
|
|
openConnection();
|
|
}
|
|
});
|
|
}
|
|
function openConnection() {
|
|
common_vendor.index.connectSocket({
|
|
url: `wss://${config.config.sok}/ws/deviceUpMessage?token=${common_vendor.index.getStorageSync("token")}`,
|
|
success(res) {
|
|
},
|
|
fail(err) {
|
|
}
|
|
});
|
|
common_vendor.index.onSocketOpen((res) => {
|
|
checkOpenSocket();
|
|
});
|
|
common_vendor.index.onSocketError((err) => {
|
|
});
|
|
onSocketMessage();
|
|
}
|
|
function onSocketMessage() {
|
|
timeout = 1e4;
|
|
timeoutObj = null;
|
|
common_vendor.index.onSocketMessage((res) => {
|
|
timeAnt = 0;
|
|
getSocketMsg(res.data);
|
|
});
|
|
}
|
|
function getSocketMsg(data) {
|
|
common_vendor.index.__f__("log", "at utils/sok.js:72", data);
|
|
var obj;
|
|
let pages = getCurrentPages();
|
|
let currentPage = pages[pages.length - 1];
|
|
let url = currentPage.route;
|
|
if (data !== "pong") {
|
|
store_index.store.commit("setSokStatus", 1);
|
|
common_vendor.index.hideLoading();
|
|
obj = JSON.parse(data);
|
|
store_index.store.commit("setCommand", obj.command);
|
|
store_index.store.commit("setDataObj", obj);
|
|
common_vendor.index.__f__("log", "at utils/sok.js:84", obj);
|
|
if (obj.insertStatus || obj.insertStatus === 0) {
|
|
store_index.store.commit("setInsertStatus", obj.insertStatus);
|
|
}
|
|
if (obj.command == "startResult" && obj.result == "-1") {
|
|
common_vendor.index.showModal({
|
|
title: "提示",
|
|
content: "启动失败,请重试",
|
|
showCancel: false,
|
|
confirmText: "确认",
|
|
success: () => {
|
|
}
|
|
});
|
|
}
|
|
if (obj.command == "startResult" && obj.result == "-2") {
|
|
common_vendor.index.showModal({
|
|
title: "提示",
|
|
content: "启动超时,请重试",
|
|
showCancel: false,
|
|
confirmText: "确认",
|
|
success: () => {
|
|
}
|
|
});
|
|
}
|
|
if (obj.command == "startResult" && obj.result == "01") {
|
|
if (url != "pageOrder/recharge/recharge") {
|
|
common_vendor.index.navigateTo({
|
|
url: `/pageOrder/recharge/recharge?transactionNo=${obj.transactionNo}&type=order`
|
|
});
|
|
}
|
|
}
|
|
if (obj.command == "transactionResult") {
|
|
if (url == "pageOrder/recharge/recharge") {
|
|
common_vendor.index.navigateBack();
|
|
}
|
|
}
|
|
}
|
|
timeAnt = 0;
|
|
reset();
|
|
}
|
|
function reset() {
|
|
clearInterval(timeoutObj);
|
|
start();
|
|
}
|
|
function start() {
|
|
timeoutObj = setInterval(function() {
|
|
if (timeAnt > 10) {
|
|
openConnection();
|
|
return;
|
|
}
|
|
common_vendor.index.sendSocketMessage({
|
|
data: "ping",
|
|
success: (res) => {
|
|
},
|
|
fail: (err) => {
|
|
openConnection();
|
|
}
|
|
});
|
|
}, timeout);
|
|
}
|
|
};
|
|
exports.init = init;
|
|
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/sok.js.map
|