app.js 8.6 KB

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