app.js 10 KB

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