const app = getApp(); const { Client, Message } = require('paho-mqtt.js') var page = app.page; // 初始化订阅信息 var init = function() { if (undefined == app.wid || app.wid.length == 0) { console.error("mqtt wid: 值等于undefined或者长度等于0"); return false; } if (undefined == app.userId || app.userId.length == 0) { console.error("mqtt userId: 值等于undefined或者长度等于0 "); return false; } var store = wx.getStorageSync("store"); app.topic = []; var tenantId = store.tenantId; var storeId = store.id; var topic1 = app.baseTopic + "/" + app.wid + "/" + app.userId; app.topic.push(topic1); var topic2 = app.baseTopic2 + "/" + tenantId + "/" + storeId; app.topic.push(topic2); return true; }; var randomString = function(len) { len = len || 32; var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; var maxPos = $chars.length; var pwd = ''; for (let i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; }; // 订阅 var subscribe = function(filter, subscribeOptions) { var client = app.client; if (client && client.isConnected()) { for (var i = 0; i < filter.length; i++) { var topic = filter[i]; client.subscribe(topic, subscribeOptions); } return; } }; // 发布 var publish = function(topic, message, qos = 2, retained = false) { var client = app.client; if (client && client.isConnected()) { var message = new Message(message); message.destinationName = topic; message.qos = qos; message.retained = retained; console.log("发布成功"); return client.send(message); } console.log("发布失败"); }; var setOnMessageArrived = function(onMessageArrived) { if (typeof onMessageArrived === 'function') { this.data.onMessageArrived = onMessageArrived } }; var setOnConnectionLost = function(onConnectionLost) { if (typeof onConnectionLost === 'function') { this.data.onConnectionLost = onConnectionLost } }; var doSubscribe = function() { this.subscribe(app.topic, { qos: 2 }) }; var doPublish = function() { var topic = app.topic[1]; var content = { "type": 1, //(1售罄2开卖) "isAll": false, "productIds": "644721258512125952", //,逗号分隔 "date": "2018-08-16 00:00:00", "storeId": "" } this.publish(topic, JSON.stringify(content), 1, false) }; /** * 链接 */ var doConnect = function() { var that = this; if (app.client && app.client.isConnected()) { // 取消连接 // app.client.disconnect(); console.log("mqtt 已处于连接中"); return; } var initFlag = that.init(); if (initFlag) { var client = app.client; if (!client) { client = new Client('wss://iotv2.ffcygl.com/mqtt', app.openId); } client.connect({ useSSL: true, cleanSession: true, keepAliveInterval: 30, onSuccess: function() { app.client = client; console.log("mqtt 连接成功 ++++++++++ 发布订阅消息"); // console.log(app.topic); // 订阅消息 that.doSubscribe(); // 接收到消息 client.onMessageArrived = function(msg) { console.log("++++++++++++++++++++++++++ mqtt 消息通知"); var result = JSON.parse(msg.payloadString); var topic = msg.topic; var reg = RegExp(/wxdc\/sell/); if (topic.match(reg)) { if (app.page) { app.page.doHandleMqttMessage(result); } } else { console.log("订单") that.doSomeThing(result); } }; // 链接丢失 client.onConnectionLost = function(responseObject) { console.error("mqtt 链接丢失"); console.log(responseObject); }; // setInterval(function() { // that.doPublish(); // }, 5000); } }); } else { console.error("mqtt 检测到userId或者wid异常 暂不连接"); } }; var reaportConnect = function() { if (null != app.client && app.client && app.client.isConnected()) { //this.doSubscribe(); } else { app.client = null; this.doConnect(); } } /** * 取消订阅主题 */ var unsubscribe = function() { console.log("============取消订阅主题"); console.log(app.topic); var client = app.client; if (!client) { // 取消连接 return; } // 取消订阅 if (client && client.isConnected()) { var topic1 = app.topic[1]; client.unsubscribe(topic1, 2); // 重新发布订阅消息 var initFlag = this.init(); var topic2 = app.topic[1]; // console.log("重新发布订阅消息============" + JSON.stringify(topic)); client.subscribe(topic2, { qos: 2 }); } } /** * 收到通知后业务处理 */ var doSomeThing = function(result) { console.log("----------", result); // 如果当前是在订单详情页面 重新加载订单数据 var pages = getCurrentPages() var currentPage = pages[pages.length - 1] var url = currentPage.route if (url.indexOf("detail/detail") > -1) { currentPage.loadOrderInfo(); return; } if (undefined != result) { var orderNo = result.orderNo; orderNo = orderNo.substring(orderNo.length - 6, orderNo.length); var msg = ""; switch (result.type) { case 3: msg = "抱歉,商家取消了订单 订单尾号:" + orderNo; break; case 4: msg = "商家已接单啦 取餐码:" + result.takeNo; break; case 5: msg = "商家已退单 订单尾号:" + orderNo; break; case 6: msg = "抱歉,商家拒绝了您的退款请求 订单尾号:" + orderNo; break; default: } wx.showModal({ title: "提示", content: msg, showCancel: false, success: function(res) { if (res.confirm) { app.page.doCancelOrder(); } } }); } } module.exports = { init: init, randomString: randomString, subscribe: subscribe, unsubscribe: unsubscribe, publish: publish, setOnMessageArrived: setOnMessageArrived, setOnConnectionLost: setOnConnectionLost, doSubscribe: doSubscribe, doPublish: doPublish, doConnect: doConnect, reaportConnect: reaportConnect, doSomeThing: doSomeThing }