app.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import mqtt from './utils/mqtt';
  2. import update from './utils/update';
  3. import strings from './utils/strings';
  4. //连接的服务器域名
  5. // const host = 'wxs://mqtt.test.radio1964.com';
  6. const host = 'wxs://mqtt.ssl.keepradioon.net:8884'
  7. // const host = 'wxs://mqtt.test.radio1964.com:8884';
  8. // const host = 'wxs://mqtt.ssl.keepradioon.net:8884'
  9. App({
  10. globalData: {
  11. statusBarHeight: 0,
  12. navBarHeight: 0,
  13. MenuButtonheight: 0,
  14. MenuButtonTop: 0,
  15. scopeBluetooth: true,
  16. ssid: "",
  17. pwdData: "",
  18. userData: null,
  19. userInfo: null,
  20. is_debug: 2, // 1 测试环境 // 2正式环境
  21. client: null,
  22. oneInitBluetooth: true,
  23. mDeviceList: [],
  24. // 当前设备
  25. // device: {},
  26. // 设备列表,跟app同步字段
  27. classifyProducts: [],
  28. //MQTT连接的配置
  29. options: {
  30. clientId: "wx_" + parseInt(Math.random() * 100 + 800, 10),
  31. reconnectPeriod: 1000, //1000毫秒,两次重新连接之间的间隔
  32. connectTimeout: 30 * 1000, //1000毫秒,两次重新连接之间的间隔
  33. resubscribe: true, //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true)
  34. keepalive: 3, //每3秒发送一次心跳
  35. },
  36. },
  37. ///全局初始化
  38. onLaunch() {
  39. var that = this;
  40. var userInfo = wx.getStorageSync("userInfo") || "";
  41. if (!strings.isEmpty(userInfo)) {
  42. that.globalData.userInfo = userInfo;
  43. }
  44. ///显示界面
  45. wx.onAppShow(() => {
  46. if (that.globalData.userInfo !== null) {
  47. if (that.globalData.client) {
  48. that.globalData.client.end(true);
  49. }
  50. console.log("重连");
  51. that.connect();
  52. };
  53. });
  54. ///界面后台化
  55. wx.onAppHide(() => {
  56. if (that.globalData.client && that.globalData.client.connected) {
  57. that.globalData.client.end(true);
  58. };
  59. });
  60. // that.getBluetoothStatus();
  61. //自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
  62. wx.getSystemInfo({
  63. success: (res) => {
  64. let custom = wx.getMenuButtonBoundingClientRect();
  65. that.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
  66. that.globalData.MenuButtonheight = custom.height;
  67. that.globalData.MenuButtonTop = custom.top;
  68. // var platform = res.platform;
  69. // that.globalData.isIOS = platform == "ios";
  70. // that.globalData.isAndroid = platform == "android";
  71. }
  72. });
  73. },
  74. onShow: function () {
  75. update.update();
  76. },
  77. onHide: function () {},
  78. ///连接设备
  79. connect() {
  80. var that = this;
  81. that.globalData.client = mqtt.connect(host, that.globalData.options);
  82. // 设备连接
  83. that.globalData.client.on('connect', () => {
  84. var page = getCurrentPages()[getCurrentPages().length - 1];
  85. if (page.mqttCallback) {
  86. page.mqttCallback("connect")
  87. };
  88. });
  89. // 消息回调 wx.hideLoading();
  90. that.globalData.client.on("message", function (topic, payload) {
  91. var page = getCurrentPages()[getCurrentPages().length - 1];
  92. if (topic.indexOf("status/onoffline") !== -1) {
  93. ///当前界面回调
  94. if (page.mqttCallback) {
  95. page.mqttCallback("message_onoffline", {
  96. topic,
  97. payload
  98. });
  99. }
  100. ///多个界面回调首页
  101. if (getCurrentPages().length != 1) {
  102. getCurrentPages()[0].mqttCallback("message_onoffline", {
  103. topic,
  104. payload
  105. });
  106. }
  107. } else if (topic.indexOf("user/pub_response") !== -1) {
  108. page.mqttCallback("message", {
  109. topic,
  110. payload
  111. });
  112. }
  113. });
  114. // 重连
  115. that.globalData.client.on("reconnect", function (errr) {
  116. console.log(" 服务器 reconnect的回调", errr);
  117. var page = getCurrentPages()[getCurrentPages().length - 1];
  118. if (page.mqttCallback) {
  119. page.mqttCallback("reconnect")
  120. };
  121. });
  122. // 离线回调
  123. that.globalData.client.on("offline", function (errr) {
  124. console.log(" 服务器 offline的回调", errr)
  125. var page = getCurrentPages()[getCurrentPages().length - 1];
  126. if (page.mqttCallback) {
  127. page.mqttCallback("offline")
  128. };
  129. });
  130. // 错误回调
  131. that.globalData.client.on("error", function (error) {
  132. console.log(" 服务器 error 的回调" + error)
  133. var page = getCurrentPages()[getCurrentPages().length - 1];
  134. if (page.mqttCallback) {
  135. page.mqttCallback("error", {})
  136. };
  137. });
  138. },
  139. // 订阅主题
  140. subscribe: function (topic, callback) {
  141. var that = this;
  142. console.log("gasdfqwerqweqr===00==" + topic);
  143. if (that.globalData.client && that.globalData.client.connected) {
  144. //订阅主题
  145. that.globalData.client.subscribe(topic, function (err, granted) {
  146. if (!err) {
  147. if (callback) {
  148. callback();
  149. }
  150. console.log("订阅成功");
  151. } else {
  152. console.log('订阅主题失败');
  153. }
  154. })
  155. } else {
  156. console.log("服务器已断开");
  157. }
  158. },
  159. // 取消订阅
  160. unsubscribe: function (topic) {
  161. var that = this;
  162. if (that.globalData.client && that.globalData.client.connected) {
  163. that.globalData.client.unsubscribe(topic);
  164. } else {
  165. console.log('请先连接服务器');
  166. }
  167. },
  168. /**
  169. * 发布信息
  170. * @param {*} data
  171. * DstDeviceName:目标设备
  172. * type: 指令
  173. * other:other
  174. * .......................
  175. * AIrSMArT_: 设备前缀
  176. */
  177. PubMsg(option, callback) {
  178. var that = this;
  179. if (strings.isEmpty(that.globalData.userInfo)) {
  180. return;
  181. }
  182. console.log("gasdfqwerqweqr===11==" + JSON.stringify(option));
  183. console.log("gasdfqwerqweqr===22==" + option.type);
  184. console.log("gasdfqwerqweqr===33==" + option.DstDeviceName);
  185. console.log("gasdfqwerqweqr===44==" + `ALY_${that.globalData.userInfo.userId}`);
  186. if (that.globalData.client && that.globalData.client.connected) {
  187. const data = {
  188. "type": option.type,
  189. "DstDeviceName": option.DstDeviceName,
  190. "SrcDeviceName": `ALY_${that.globalData.userInfo.userId}`,
  191. };
  192. if (option.other) {
  193. data.other = option.other;
  194. };
  195. that.globalData.client.publish(`/${option.DstDeviceName}/user/sub_control`, `${JSON.stringify(data)}`, (err) => {
  196. if (err) {
  197. console.log("发布消息失败");
  198. }
  199. });
  200. } else {
  201. console.log("服务器已断开");
  202. }
  203. },
  204. // 获取蓝牙权限
  205. getBluetoothStatus() {
  206. var that = this;
  207. wx.getSetting({
  208. success(res) {
  209. if (res.authSetting["scope.bluetooth"]) {
  210. that.globalData.scopeBluetooth = true;
  211. } else if (res.authSetting["scope.bluetooth"] === undefined) {
  212. that.globalData.scopeBluetooth = false;
  213. wx.authorize({
  214. scope: "scope.bluetooth",
  215. complete() {
  216. that.getBluetoothStatus();
  217. }
  218. });
  219. } else {
  220. that.globalData.scopeBluetooth = false;
  221. wx.showModal({
  222. title: '请打开系统蓝牙进行配网',
  223. content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
  224. success(res) {
  225. if (res.confirm) {
  226. console.log('用户点击确定')
  227. wx.openSetting({
  228. complete() {
  229. // that.getBluetoothStatus();
  230. }
  231. })
  232. } else if (res.cancel) {
  233. console.log('用户点击取消')
  234. }
  235. }
  236. })
  237. };
  238. if (getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck) {
  239. getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(that.globalData.scopeBluetooth);
  240. }
  241. // res.authSetting = {
  242. // "scope.userInfo": true,
  243. // "scope.userLocation": true
  244. // }
  245. }
  246. })
  247. },
  248. })
  249. // "tabBar": {
  250. // "borderStyle": "black",
  251. // "list": [{
  252. // "pagePath": "pages/index/index",
  253. // "text": "首页",
  254. // "iconPath": "./img/index_unaction.png",
  255. // "selectedIconPath": "./img/index_action.png"
  256. // },
  257. // {
  258. // "pagePath": "pages/me/me",
  259. // "text": "我的",
  260. // "iconPath": "./img/me_unaction.png",
  261. // "selectedIconPath": "./img/me_action.png"
  262. // }
  263. // ]
  264. // },