index.js 18 KB

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