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