You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.0 KiB
JavaScript
141 lines
4.0 KiB
JavaScript
9 months ago
|
var app = getApp();
|
||
|
const Paho = require('./paho-mqtt.js');
|
||
|
|
||
|
var mqttApi = {
|
||
|
/*---- 获取连接 ----*/
|
||
|
connect: function (cb) {
|
||
|
var that = this;
|
||
|
var client = app.mqttParam.client;
|
||
|
if (client && client.isConnected()) {
|
||
|
return;
|
||
|
}
|
||
|
var userInfo = wx.getStorageSync("userInfo");
|
||
|
var randomString = "";
|
||
|
for (var i = 1; i <= 5; i++) {
|
||
|
randomString += parseInt(Math.random() * 10);
|
||
|
}
|
||
|
var clientId = "swxclientId-member-" + app.globalData.tenantId + "-" + app.globalData.shopNo + "-" + app.globalData.posNo + randomString;
|
||
|
client = new Paho.Client(app.mqttParam.wsServer, 0, clientId);
|
||
|
client.connect({
|
||
|
useSSL: false,
|
||
|
cleanSession: false,
|
||
|
keepAliveInterval: 2,
|
||
|
onSuccess: function () {
|
||
|
app.mqttParam.client = client;
|
||
|
client.onMessageArrived = function (msg) {
|
||
|
console.log("----------订阅消息", msg);
|
||
|
var title = msg.destinationName;
|
||
|
if (title.indexOf('pay') > -1) {
|
||
|
if (typeof app.globalFunction.scanPayCodeCallBack === 'function') {
|
||
|
app.globalFunction.scanPayCodeCallBack(msg.payloadString);
|
||
|
}
|
||
|
} else if (title.indexOf('couponconsume') > -1) {
|
||
|
app.globalFunction.consumeCouponCallBack(msg.payloadString);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
client.onConnectionLost = function (responseObject) {
|
||
|
if (typeof app.globalFunction.onConnectionLost === 'function') {
|
||
|
return app.globalData.onConnectionLost(responseObject)
|
||
|
}
|
||
|
if (responseObject.errorCode !== 0) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (typeof cb === 'function') {
|
||
|
cb();
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
/*----订阅 ----*/
|
||
|
subscribe: function (filter, subscribeOptions) {
|
||
|
var that = this;
|
||
|
var client = app.mqttParam.client;
|
||
|
if (client && client.isConnected()) {
|
||
|
client.subscribe(filter, {
|
||
|
qos: 2
|
||
|
});
|
||
|
console.log("mqtt订阅成功")
|
||
|
} else {
|
||
|
that.connect(function () {
|
||
|
client = app.mqttParam.client;
|
||
|
if (client && client.isConnected()) {
|
||
|
client.subscribe(filter, {
|
||
|
qos: 2
|
||
|
});
|
||
|
console.log("mqtt订阅成功")
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
|
||
|
/*----废弃订阅 ----*/
|
||
|
unsubscribe: function (filter, subscribeOptions) {
|
||
|
var client = app.mqttParam.client;
|
||
|
if (client && client.isConnected()) {
|
||
|
client.unsubscribe(filter);
|
||
|
}
|
||
|
},
|
||
|
|
||
|
/*----发布消息----*/
|
||
|
publish: function (topic, message, qos = 0, retained = false) {
|
||
|
// 发布
|
||
|
var that = this;
|
||
|
var client = app.mqttParam.client;
|
||
|
if (client && client.isConnected()) {
|
||
|
var message = new Paho.Message(message);
|
||
|
message.destinationName = topic;
|
||
|
message.qos = qos;
|
||
|
message.retained = retained;
|
||
|
return client.send(message);
|
||
|
} else {
|
||
|
that.connect(function () {
|
||
|
client = app.mqttParam.client;
|
||
|
if (client && client.isConnected()) {
|
||
|
var message = new Paho.Message(message);
|
||
|
message.destinationName = topic;
|
||
|
message.qos = qos;
|
||
|
message.retained = retained;
|
||
|
return client.send(message);
|
||
|
return;
|
||
|
}
|
||
|
});
|
||
|
wx.showToast({
|
||
|
title: '发送失败',
|
||
|
icon: 'success',
|
||
|
duration: 2000
|
||
|
});
|
||
|
}
|
||
|
},
|
||
|
|
||
|
setOnMessageArrived: function (onMessageArrived) {
|
||
|
if (typeof onMessageArrived === 'function') {
|
||
|
onMessageArrived();
|
||
|
}
|
||
|
},
|
||
|
|
||
|
startReconnect: function () {
|
||
|
// 重连机制
|
||
|
var that = this;
|
||
|
var interval = app.interval;
|
||
|
clearInterval(interval);
|
||
|
interval = setInterval(function () {
|
||
|
var client = app.mqttParam.client;
|
||
|
if (client != null) {
|
||
|
that.connect();
|
||
|
}
|
||
|
}, 10000);
|
||
|
},
|
||
|
|
||
|
setOnConnectionLost: function (onConnectionLost) {
|
||
|
if (typeof onConnectionLost === 'function') {
|
||
|
onConnectionLost();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
api: mqttApi
|
||
|
}
|