indexCopy.js 16 KB

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