index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // index.ts
  2. // 获取应用实例
  3. const app = getApp();
  4. const { login, listByDevice } = require('./../../utils/api.js');
  5. const { isCN } = require('./../../utils/util.js');
  6. const { selectIndex,eventOn } = require('./../../custom-tab-bar/tabbar.js');
  7. let _this = null;
  8. const interval = null;
  9. Page({
  10. data: {
  11. navBarHeight: app.globalData.navBarHeight,
  12. MenuButtonheight: app.globalData.MenuButtonheight,
  13. MenuButtonTop: app.globalData.MenuButtonTop,
  14. actionIndex: null,
  15. isShowDevicelist: false,
  16. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  17. channelData: [],
  18. deviceList: [],
  19. deviceListIndex: null,
  20. showLogin: false,
  21. isOneLoading: true,
  22. uid: null,
  23. showDelete: false,
  24. isSetWake: false,
  25. thisDeviceMac: null,
  26. battery: 4, // 0≤电量<20,0格
  27. newVersion: false,
  28. rightId: wx.getStorageSync('rightId') || 0
  29. },
  30. onShow() {
  31. selectIndex(1);
  32. // if (typeof this.getTabBar === 'function' &&
  33. // this.getTabBar()) {
  34. // this.getTabBar().setData({
  35. // selected: 1
  36. // })
  37. // }
  38. // app.eventBus.on('rightChange', data => {
  39. // if (data !== this.data.rightId) {
  40. // this.setData({
  41. // rightId: data
  42. // })
  43. // }
  44. // })
  45. var _this = this;
  46. if ((!this.data.isOneLoading)) {
  47. wx.getStorage({
  48. key: 'userInfo',
  49. fail(res) {
  50. // 取消订阅
  51. if (_this.data.thisDeviceMac !== null) {
  52. app.unsubscribe(`/${_this.data.thisDeviceMac}/user/pub_response`);
  53. };
  54. _this.setData({
  55. showLogin: true,
  56. });
  57. },
  58. success() {
  59. if (app.globalData.newDeviceId) {
  60. // 获取设备本地数据
  61. _this.getDeviceData();
  62. } else if (_this.data.deviceListIndex !== null) {
  63. // 更新
  64. _this.actionDevice(_this.data.deviceListIndex);
  65. }
  66. }
  67. });
  68. };
  69. },
  70. changeRole() {
  71. app.changeUserRight()
  72. },
  73. onLoad() {
  74. var _this = this;
  75. eventOn(_this);
  76. wx.showLoading({
  77. title: '加载中',
  78. });
  79. const str = setTimeout(() => {
  80. clearTimeout(str);
  81. if (_this.data.isOneLoading) {
  82. _this.setData({
  83. isOneLoading: false,
  84. });
  85. }
  86. }, 500);
  87. // 获取缓存的频道数据
  88. wx.getStorage({
  89. key: "channelData",
  90. success(res) {
  91. _this.setData({
  92. channelData: res.data
  93. })
  94. }
  95. });
  96. // 获取设备本地数据
  97. _this.getDeviceData();
  98. },
  99. login() {
  100. wx.login({
  101. success: res => {
  102. let phone = undefined;
  103. try {
  104. var value = wx.getStorageSync('userInfo')
  105. if (value) {
  106. phone = value.phone || undefined;
  107. };
  108. } catch (e) {
  109. // Do something when catch error
  110. };
  111. // 请求登录
  112. login({ code: res.code, phone }).then((res) => {
  113. app.globalData.userInfo = res;
  114. wx.setStorage({
  115. key: "userInfo",
  116. data: res
  117. });
  118. // 需要用授权登录
  119. if ((res.isNewUser && res.isNewUser === true) || !phone) {
  120. _this.setData({
  121. showLogin: true,
  122. });
  123. return;
  124. };
  125. // 获取设备本地数据
  126. this.getDeviceData();
  127. _this.setData({
  128. showLogin: false,
  129. });
  130. wx.setStorage({
  131. key: "token",
  132. data: JSON.stringify({
  133. id: res.userId,
  134. token: res.accessToken,
  135. }),
  136. });
  137. });
  138. },
  139. })
  140. },
  141. getDeviceData() {
  142. // 获取设备数据
  143. wx.getStorage({
  144. key: 'devicelist',
  145. success(res) {
  146. wx.hideLoading();
  147. if (res.data) {
  148. const resData = JSON.parse(res.data);
  149. _this.setData({
  150. deviceList: resData
  151. });
  152. // 连接mqtt
  153. if (app.globalData.client === null) {
  154. app.connect();
  155. } else if (app.globalData.newDeviceId) {
  156. _this.setData({
  157. actionIndex: null,
  158. deviceListIndex: null,
  159. });
  160. // 监听
  161. let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  162. app.subscribe(topic);
  163. const Timeout = setTimeout(() => {
  164. clearTimeout(Timeout);
  165. _this.actionDevice(0);
  166. }, 1000);
  167. }
  168. // // 有新设备
  169. // if(app.globalData.newDeviceId) {
  170. // _this.setData({
  171. // actionIndex: null,
  172. // deviceListIndex: null,
  173. // });
  174. // let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  175. // app.subscribe(topic);
  176. // }
  177. }
  178. },
  179. fail(e) {
  180. wx.hideLoading();
  181. }
  182. });
  183. },
  184. // 回调
  185. mqttCallback(type, option) {
  186. let payloads = null;
  187. if (option) {
  188. payloads = JSON.parse(option.payload);
  189. };
  190. switch (type) {
  191. case "connect":
  192. _this.connectSuccess();
  193. break;
  194. case "message_onoffline":
  195. _this.online(payloads);
  196. break;
  197. case "message":
  198. // 接收设备播放信息
  199. if (payloads.type === "get_position" && payloads.other) {
  200. let actionIndex = null;
  201. _this.data.channelData.map((v, index) => {
  202. if (v.channelNum === payloads.other.channel) {
  203. actionIndex = index;
  204. }
  205. });
  206. _this.setData({
  207. actionIndex,
  208. });
  209. } else if (payloads.type === "play" || payloads.type === "play_state") {
  210. if (_this.data.deviceListIndex === null) {
  211. return;
  212. }
  213. // 接收设备当前播放状态
  214. const obj = {
  215. DstDeviceName: _this.getThisDeviceID()
  216. }
  217. app.PubMsg({ type: "get_position", ...obj });
  218. } else if (payloads.type === "get_dev_info") {
  219. // 接收设备当前信息
  220. this.getchannelData(payloads.other.ProdModel);
  221. // 电量
  222. _this.setData({
  223. battery: _this._battery(payloads.other.Power),
  224. })
  225. // 当前设备木有设置定时
  226. _this.setData({
  227. isSetWake: false,
  228. });
  229. payloads.other.alarm.map((v) => {
  230. if (v.enable === "1") {
  231. _this.setData({
  232. isSetWake: true
  233. });
  234. }
  235. });
  236. // 更新信息
  237. _this.data.deviceList.map((v, index) => {
  238. if (payloads.SrcDeviceName && payloads.SrcDeviceName.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  239. _this.data.deviceList[index].ProdModel = payloads.other.ProdModel;
  240. _this.data.deviceList[index].devName = payloads.other.devName;
  241. }
  242. });
  243. // 更新缓存
  244. wx.setStorage({
  245. key: "devicelist",
  246. data: JSON.stringify(_this.data.deviceList),
  247. success() {
  248. _this.setData({
  249. deviceList: _this.data.deviceList
  250. })
  251. }
  252. });
  253. } else if (payloads.type === "battery" && payloads.other) {
  254. _this.setData({
  255. battery: _this._battery(payloads.other.battery),
  256. })
  257. }
  258. break;
  259. default:
  260. }
  261. },
  262. // 格式化电量
  263. _battery(battery) {
  264. let _battery = 0;
  265. if (battery < 20) {
  266. _battery = 0
  267. } else if (20 <= battery && battery < 40) {
  268. _battery = 1
  269. } else if (40 <= battery && battery < 60) {
  270. _battery = 2
  271. } else if (60 <= battery && battery < 80) {
  272. _battery = 3
  273. } else if (80 <= battery && battery <= 100) {
  274. _battery = 4
  275. } else if (battery > 100) {
  276. _battery = 5
  277. };
  278. return _battery
  279. },
  280. connectSuccess() {
  281. // 订阅设备在线信息
  282. _this.subscribeDevicesStatus();
  283. },
  284. online(payloads) {
  285. // 设置在线状态
  286. _this.data.deviceList.map((v, index) => {
  287. if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  288. _this.data.deviceList[index].state = payloads.state;
  289. }
  290. });
  291. _this.setData({
  292. deviceList: _this.data.deviceList,
  293. });
  294. // 更新缓存
  295. wx.setStorage({
  296. key: "devicelist",
  297. data: JSON.stringify(_this.data.deviceList)
  298. });
  299. // 如没有选中,选中最新的
  300. (() => {
  301. if (_this.data.deviceListIndex === null) {
  302. let itue = false;
  303. _this.data.deviceList.map((v, index) => {
  304. if (v.state === "online" && !itue) {
  305. itue = true;
  306. _this.actionDevice(index);
  307. }
  308. });
  309. }
  310. // else {
  311. // _this.actionDevice(_this.data.deviceListIndex);
  312. // };
  313. })();
  314. // 当前播放设备离线
  315. if (_this.data.deviceListIndex !== null && _this.data.deviceList[_this.data.deviceListIndex].state !== "online") {
  316. _this.setData({
  317. actionIndex: null,
  318. deviceListIndex: null,
  319. });
  320. };
  321. },
  322. // 订阅设备在线状态
  323. subscribeDevicesStatus() {
  324. this.data.deviceList.forEach((value) => {
  325. let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
  326. app.subscribe(topic);
  327. })
  328. },
  329. // 订阅消息
  330. subscribeCurrDevice() {
  331. if (!(app.globalData.client && app.globalData.client.connected)) {
  332. console.log("未连接MQTT服务器");
  333. const str = setInterval(() => {
  334. clearInterval(str);
  335. _this.subscribeCurrDevice();
  336. }, 500);
  337. return;
  338. };
  339. if (this.data.deviceList.length === 0 || this.data.deviceListIndex === null) {
  340. return
  341. };
  342. let topic = `/AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
  343. app.subscribe(topic);
  344. const obj = {
  345. DstDeviceName: _this.getThisDeviceID()
  346. };
  347. app.PubMsg({ type: "get_dev_info", ...obj });
  348. },
  349. isShowDevicelistFun() {
  350. this.setData({
  351. isShowDevicelist: !_this.data.isShowDevicelist
  352. })
  353. },
  354. actionMusic(e) {
  355. if (e.currentTarget.dataset.index === this.data.actionIndex) {
  356. return;
  357. };
  358. if (this.data.deviceListIndex === null) {
  359. wx.showToast({
  360. title: '请选择设备',
  361. icon: "none"
  362. })
  363. return;
  364. };
  365. this.setData({
  366. actionIndex: e.currentTarget.dataset.index
  367. });
  368. const other = {
  369. "url": "",
  370. "media_data": "",
  371. "user_id": `${app.globalData.userInfo.deviceUid}`,
  372. "timestamp": `${Math.round(new Date() / 1000)}`,
  373. "channel_id": `${this.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  374. "order": "",
  375. "resource_from": "",
  376. "songAlbumID": "",
  377. "version": 3,
  378. "is_debug": app.globalData.is_debug
  379. };
  380. app.PubMsg({
  381. type: "play",
  382. DstDeviceName: _this.getThisDeviceID(),
  383. other
  384. });
  385. },
  386. actionDeviceIndex(e) {
  387. if (e.currentTarget.dataset.index === this.data.deviceListIndex) {
  388. return
  389. };
  390. this.actionDevice(e.currentTarget.dataset.index)
  391. },
  392. actionDevice(index) {
  393. const device = this.data.deviceList[index];
  394. if (device.state !== "online") {
  395. return;
  396. };
  397. // 取消订阅
  398. if (this.data.thisDeviceMac !== null) {
  399. app.unsubscribe(`/${this.data.thisDeviceMac}/user/pub_response`);
  400. };
  401. this.setData({
  402. deviceListIndex: index,
  403. thisDeviceMac: `AIrSMArT_${this.data.deviceList[index].name.split("BLUFI_")[1]}`
  404. });
  405. // app.PubMsg({
  406. // type: "get_dev_info",
  407. // DstDeviceName: _this.getThisDeviceID()
  408. // });
  409. this.subscribeCurrDevice();
  410. },
  411. getThisDeviceID() {
  412. return `AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}`
  413. },
  414. goLogin() {
  415. wx.navigateTo({
  416. url: './../login/login',
  417. });
  418. },
  419. goDeviceConnect() {
  420. wx.navigateTo({
  421. url: './../deviceConnect0/deviceConnect0',
  422. });
  423. this.setData({
  424. isShowDevicelist: false
  425. })
  426. },
  427. getchannelData(clientType) {
  428. let _this = this;
  429. if (this.data.deviceListIndex === null) {
  430. return;
  431. };
  432. listByDevice({ clientType }).then((res) => {
  433. _this.setData({
  434. channelData: res
  435. });
  436. // 接收设备当前播放状态
  437. const obj = {
  438. DstDeviceName: _this.getThisDeviceID()
  439. }
  440. app.PubMsg({ type: "get_position", ...obj });
  441. wx.setStorage({
  442. key: "channelData",
  443. data: res
  444. });
  445. // 有新设备
  446. if (app.globalData.newDeviceId) {
  447. app.globalData.newDeviceId = null;
  448. return;
  449. // 去掉此功能,先留着吧
  450. // const other= {
  451. // "url": "",
  452. // "media_data": "",
  453. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  454. // "timestamp": `${Math.round(new Date() / 1000)}`,
  455. // "channel_id": `${res[1].channelNum}`,
  456. // "order": "",
  457. // "resource_from": "",
  458. // "songAlbumID":"",
  459. // "version":3,
  460. // "is_debug": app.globalData.is_debug
  461. // };
  462. // app.PubMsg({
  463. // type: "play",
  464. // DstDeviceName: _this.getThisDeviceID(),
  465. // other
  466. // });
  467. }
  468. })
  469. },
  470. showDelete() {
  471. // wx.navigateTo({
  472. // url: './../deviceRoter3/deviceRoter3',
  473. // })
  474. this.setData({
  475. showDelete: !this.data.showDelete
  476. });
  477. },
  478. deleteDevice(e) {
  479. const id = this.data.deviceList[e.currentTarget.dataset.index].deviceId;
  480. let name = _this.data.deviceListIndex !== null ? this.data.deviceList[_this.data.deviceListIndex].name : null;
  481. const deviceList = this.data.deviceList.filter((v, i) => {
  482. return id !== v.deviceId
  483. });
  484. // 取消订阅
  485. app.unsubscribe(`/AIrSMArT_${_this.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  486. wx.setStorage({
  487. key: "devicelist",
  488. data: JSON.stringify(deviceList),
  489. success() {
  490. _this.setData({
  491. deviceList,
  492. thisDeviceMac: null
  493. });
  494. // 当前没有设备
  495. if (deviceList.length === 0) {
  496. _this.setData({
  497. showDelete: false,
  498. actionIndex: null,
  499. deviceListIndex: null,
  500. isShowDevicelist: false,
  501. });
  502. }
  503. if (_this.data.deviceListIndex === null) {
  504. return
  505. };
  506. if (e.currentTarget.dataset.index === _this.data.deviceListIndex) {
  507. let index_ = null;
  508. deviceList.map((v, index) => {
  509. if (v.state === "online" && index_ === null) {
  510. index_ = index;
  511. }
  512. });
  513. if (index_ !== null) {
  514. _this.actionDevice(index_);
  515. } else {
  516. _this.setData({
  517. actionIndex: null,
  518. deviceListIndex: null,
  519. });
  520. };
  521. } else {
  522. deviceList.map((v, index) => {
  523. if (v.name === name) {
  524. _this.setData({
  525. deviceListIndex: index,
  526. });
  527. }
  528. });
  529. }
  530. }
  531. })
  532. },
  533. goWake() {
  534. if (this.data.deviceListIndex === null) {
  535. return;
  536. };
  537. wx.navigateTo({
  538. url: './../deviceWake/deviceWake?deviceId=' + this.data.deviceList[this.data.deviceListIndex].deviceId + "&clientType=" + this.data.deviceList[this.data.deviceListIndex].ProdModel,
  539. });
  540. },
  541. goChnnel() {
  542. wx.setStorage({
  543. key: "channelDeta",
  544. data: this.data.channelData[this.data.actionIndex],
  545. success() {
  546. wx.navigateTo({
  547. url: './../channelDetails/channelDetails'
  548. })
  549. }
  550. })
  551. },
  552. updata() {
  553. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  554. wx.getUpdateManager().applyUpdate()
  555. },
  556. updataClone() {
  557. this.setData({
  558. newVersion: false
  559. })
  560. },
  561. onUnload() {
  562. if (app.globalData.client === null) {
  563. return;
  564. };
  565. app.globalData.client.end(true);
  566. app.globalData.client.end(true);
  567. app.globalData.client = null;
  568. },
  569. })