app.js 7.9 KB

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