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.

156 lines
3.1 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const app = getApp()
const mqtt = require("mqtt.js")
/**
*
*/
var mqttClient = function() {
app.client = mqtt.connect('wxs://iotv2.ffcygl.com/mqtt', {
// 心跳请求单位s
keepalive: 30,
clientId: app.openId,
protocolId: 'MQTT',
protocolVersion: 4,
username: 'admin',
password: 'admin',
reconnectPeriod: 5000,
});
app.client.on('message', function(topic, message, packet) {
console.log(message.toString());
if (app.that) app.that.onMessage(topic, JSON.parse(message.toString()));
});
app.client.on('connect', function(err) {
//console.log("监听连接状态", err);
});
app.client.on('close', function(res) {
// console.log("监听连接关闭");
if (!app.client._activeEndFlag) {
// 重新建立连接
app.client.reconnect()
}
});
app.client.on('error', function(err) {
console.log("连接失败", err);
});
}
/**
* mqtt 重新连接
*/
var mqttConnect = function() {
if (app.client == null) {
mqttClient();
}
app.client.reconnect()
}
/**
* mqtt 断开连接
*/
var mqttClose = function() {
if (app.client == null) {
mqttClient();
}
app.client.end(true, function(err) {
console.log("断开连接", err);
});
}
/**
* 订阅主题
*/
var mqttSubscribe = function(top, page) {
if (app.client == null) {
mqttClient();
}
if (top == 1 && app.appTop1) {
mqttunSubscribe(app.appTop1);
}
if (top == 2 && app.appTop2) {
mqttunSubscribe(app.appTop2);
}
console.log("开始订阅信息" + top, mqttTop(top));
app.client.subscribe(mqttTop(top), function(err) {
console.log("订阅主题" + mqttTop(top), err);
})
}
/**
* 订阅主题
*/
var mqttSubscribe1 = function(top) {
app.client.subscribe(top, function(err) {
console.log("订阅主题", err);
})
}
/**
* 取消订阅
*/
var mqttunSubscribe = function(top) {
app.client.unsubscribe(top, function(err) {
console.log("取消订阅", err,top);
});
}
/**
* 发布订阅
*/
var mqttPubish = function(top) {
if (app.client == null) {
mqttClient();
}
app.client.publish(top, "hello");
}
/**
* 接收消息
*/
var mqttOn = function(that) {
if (app.client == null) {
mqttClient();
}
app.that = that;
}
/**
* 订单主题
*/
var mqttTop = function(type) {
var store = wx.getStorageSync("store");
var tenantId = app.globalData.tenantId;
var storeId = store.storeId;
if (type == 1) { // 接单主题
return app.appTop1 = app.baseTopic + "/" + app.openId;
}
if (type == 2) { // 售罄主题
return app.appTop2 = app.baseTopic2 + "/" + tenantId + "/" + storeId;
}
if (type == 3) {
return app.getPayCode();
}
if (type == 4) {
return app.getCouponConsumeCode(app.globalData.oldCode);
}
if (type == 5) {
return app.appTop3 = app.baseTopic3+ "/" + tenantId + "/" + storeId;
}
if(type==6) { //骑手接单
var orderNo=app.globalData.orderNo;
return app.baseTopic4 + "/" + tenantId + "/" + orderNo;
}
}
module.exports = {
mqttunSubscribe: mqttunSubscribe,
mqttSubscribe: mqttSubscribe,
mqttPubish: mqttPubish,
mqttOn: mqttOn,
mqttTop: mqttTop
}