app.js 10 KB

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