index.js 16 KB

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