index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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.homeLogin();
  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.getHomeDeviceData();
  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. homeLogin() {
  143. wx.login({
  144. success: res => {
  145. let phone = undefined;
  146. try {
  147. var value = wx.getStorageSync('userInfo')
  148. if (value) {
  149. phone = value.phone || undefined;
  150. };
  151. } catch (e) {
  152. // Do something when catch error
  153. };
  154. // 请求登录
  155. login({
  156. code: res.code,
  157. phone
  158. }).then((res) => {
  159. app.globalData.userInfo = res;
  160. wx.setStorage({
  161. key: "userInfo",
  162. data: res
  163. });
  164. // 需要用授权登录
  165. if ((res.isNewUser && res.isNewUser === true) || !phone) {
  166. _this.setData({
  167. showLogin: true,
  168. });
  169. return;
  170. };
  171. // 获取设备本地数据
  172. this.getHomeDeviceData();
  173. _this.setData({
  174. showLogin: false,
  175. });
  176. wx.setStorage({
  177. key: "token",
  178. data: JSON.stringify({
  179. id: res.userId,
  180. token: res.accessToken,
  181. }),
  182. });
  183. });
  184. },
  185. })
  186. },
  187. getHomeDeviceData() {
  188. // 获取设备数据
  189. wx.getStorage({
  190. key: 'devicelist',
  191. success(res) {
  192. wx.hideLoading();
  193. if (res.data) {
  194. const resData = JSON.parse(res.data);
  195. _this.setData({
  196. deviceList: resData
  197. });
  198. // 连接mqtt
  199. if (app.globalData.client === null) {
  200. app.connect();
  201. } else if (app.globalData.newDeviceId) {
  202. _this.setData({
  203. actionIndex: null,
  204. deviceListIndex: null,
  205. });
  206. // 监听
  207. let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  208. app.subscribe(topic);
  209. const Timeout = setTimeout(() => {
  210. clearTimeout(Timeout);
  211. _this.actionDevice(0);
  212. }, 1000);
  213. }
  214. // // 有新设备
  215. // if(app.globalData.newDeviceId) {
  216. // _this.setData({
  217. // actionIndex: null,
  218. // deviceListIndex: null,
  219. // });
  220. // let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  221. // app.subscribe(topic);
  222. // }
  223. }
  224. },
  225. fail(e) {
  226. wx.hideLoading();
  227. }
  228. });
  229. },
  230. // 回调
  231. mqttCallback(type, option) {
  232. let payloads = null;
  233. if (option) {
  234. payloads = JSON.parse(option.payload);
  235. };
  236. switch (type) {
  237. case "connect":
  238. _this.connectSuccess();
  239. break;
  240. case "message_onoffline":
  241. _this.online(payloads);
  242. break;
  243. case "message":
  244. // 接收设备播放信息
  245. if (payloads.type === "get_position" && payloads.other) {
  246. let actionIndex = null;
  247. _this.data.channelData.map((v, index) => {
  248. if (v.channelNum === payloads.other.channel) {
  249. actionIndex = index;
  250. }
  251. });
  252. _this.setData({
  253. actionIndex,
  254. });
  255. } else if (payloads.type === "play" || payloads.type === "play_state") {
  256. if (_this.data.deviceListIndex === null) {
  257. return;
  258. }
  259. // 接收设备当前播放状态
  260. const obj = {
  261. DstDeviceName: _this.getThisDeviceID()
  262. }
  263. app.PubMsg({
  264. type: "get_position",
  265. ...obj
  266. });
  267. } else if (payloads.type === "get_dev_info") {
  268. // 接收设备当前信息
  269. this.getchannelData(payloads.other.ProdModel);
  270. // 电量
  271. _this.setData({
  272. battery: _this._battery(payloads.other.Power),
  273. })
  274. // 当前设备木有设置定时
  275. _this.setData({
  276. isSetWake: false,
  277. });
  278. payloads.other.alarm.map((v) => {
  279. if (v.enable === "1") {
  280. _this.setData({
  281. isSetWake: true
  282. });
  283. }
  284. });
  285. // 更新信息
  286. _this.data.deviceList.map((v, index) => {
  287. if (payloads.SrcDeviceName && payloads.SrcDeviceName.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  288. _this.data.deviceList[index].ProdModel = payloads.other.ProdModel;
  289. _this.data.deviceList[index].devName = payloads.other.devName;
  290. }
  291. });
  292. // 更新缓存
  293. wx.setStorage({
  294. key: "devicelist",
  295. data: JSON.stringify(_this.data.deviceList),
  296. success() {
  297. _this.setData({
  298. deviceList: _this.data.deviceList
  299. })
  300. }
  301. });
  302. } else if (payloads.type === "battery" && payloads.other) {
  303. _this.setData({
  304. battery: _this._battery(payloads.other.battery),
  305. })
  306. }
  307. break;
  308. default:
  309. }
  310. },
  311. // 格式化电量
  312. _battery(battery) {
  313. let _battery = 0;
  314. if (battery < 20) {
  315. _battery = 0
  316. } else if (20 <= battery && battery < 40) {
  317. _battery = 1
  318. } else if (40 <= battery && battery < 60) {
  319. _battery = 2
  320. } else if (60 <= battery && battery < 80) {
  321. _battery = 3
  322. } else if (80 <= battery && battery <= 100) {
  323. _battery = 4
  324. } else if (battery > 100) {
  325. _battery = 5
  326. };
  327. return _battery
  328. },
  329. connectSuccess() {
  330. // 订阅设备在线信息
  331. _this.subscribeDevicesStatus();
  332. },
  333. online(payloads) {
  334. // 设置在线状态
  335. _this.data.deviceList.map((v, index) => {
  336. if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  337. _this.data.deviceList[index].state = payloads.state;
  338. }
  339. });
  340. _this.setData({
  341. deviceList: _this.data.deviceList,
  342. });
  343. // 更新缓存
  344. wx.setStorage({
  345. key: "devicelist",
  346. data: JSON.stringify(_this.data.deviceList)
  347. });
  348. // 如没有选中,选中最新的
  349. (() => {
  350. if (_this.data.deviceListIndex === null) {
  351. let itue = false;
  352. _this.data.deviceList.map((v, index) => {
  353. if (v.state === "online" && !itue) {
  354. itue = true;
  355. _this.actionDevice(index);
  356. }
  357. });
  358. }
  359. // else {
  360. // _this.actionDevice(_this.data.deviceListIndex);
  361. // };
  362. })();
  363. // 当前播放设备离线
  364. if (_this.data.deviceListIndex !== null && _this.data.deviceList[_this.data.deviceListIndex].state !== "online") {
  365. _this.setData({
  366. actionIndex: null,
  367. deviceListIndex: null,
  368. });
  369. };
  370. },
  371. // 订阅设备在线状态
  372. subscribeDevicesStatus() {
  373. this.data.deviceList.forEach((value) => {
  374. let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
  375. app.subscribe(topic);
  376. })
  377. },
  378. subscribeCurrDevice() {
  379. if (!(app.globalData.client && app.globalData.client.connected)) {
  380. console.log("未连接MQTT服务器");
  381. const str = setInterval(() => {
  382. clearInterval(str);
  383. _this.subscribeCurrDevice();
  384. }, 500);
  385. return;
  386. };
  387. if (this.data.deviceList.length === 0 || this.data.deviceListIndex === null) {
  388. return
  389. };
  390. let topic = `/AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
  391. app.subscribe(topic);
  392. const obj = {
  393. DstDeviceName: _this.getThisDeviceID()
  394. };
  395. app.PubMsg({
  396. type: "get_dev_info",
  397. ...obj
  398. });
  399. },
  400. isShowDevicelistFun() {
  401. this.setData({
  402. isShowDevicelist: !_this.data.isShowDevicelist
  403. })
  404. },
  405. actionMusic(e) {
  406. if (e.currentTarget.dataset.index === this.data.actionIndex) {
  407. return;
  408. };
  409. if (this.data.deviceListIndex === null) {
  410. wx.showToast({
  411. title: '请选择设备',
  412. icon: "none"
  413. })
  414. return;
  415. };
  416. this.setData({
  417. actionIndex: e.currentTarget.dataset.index
  418. });
  419. const other = {
  420. "url": "",
  421. "media_data": "",
  422. "user_id": `${app.globalData.userInfo.deviceUid}`,
  423. "timestamp": `${Math.round(new Date() / 1000)}`,
  424. "channel_id": `${this.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  425. "order": "",
  426. "resource_from": "",
  427. "songAlbumID": "",
  428. "version": 3,
  429. "is_debug": app.globalData.is_debug
  430. };
  431. app.PubMsg({
  432. type: "play",
  433. DstDeviceName: _this.getThisDeviceID(),
  434. other
  435. });
  436. },
  437. actionDeviceIndex(e) {
  438. if (e.currentTarget.dataset.index === this.data.deviceListIndex) {
  439. return
  440. };
  441. this.actionDevice(e.currentTarget.dataset.index)
  442. },
  443. actionDevice(index) {
  444. const device = this.data.deviceList[index];
  445. if (device.state !== "online") {
  446. return;
  447. };
  448. // 取消订阅
  449. if (this.data.thisDeviceMac !== null) {
  450. app.unsubscribe(`/${this.data.thisDeviceMac}/user/pub_response`);
  451. };
  452. this.setData({
  453. deviceListIndex: index,
  454. thisDeviceMac: `AIrSMArT_${this.data.deviceList[index].name.split("BLUFI_")[1]}`
  455. });
  456. // app.PubMsg({
  457. // type: "get_dev_info",
  458. // DstDeviceName: _this.getThisDeviceID()
  459. // });
  460. this.subscribeCurrDevice();
  461. },
  462. getThisDeviceID() {
  463. return `AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}`
  464. },
  465. goLogin() {
  466. wx.navigateTo({
  467. url: './../login/login',
  468. });
  469. },
  470. goDeviceConnect() {
  471. routeUtil.jump(route_constant.deviceList)
  472. return;
  473. wx.navigateTo({
  474. url: './../deviceConnect0/deviceConnect0',
  475. });
  476. this.setData({
  477. isShowDevicelist: false
  478. })
  479. },
  480. getchannelData(clientType) {
  481. let _this = this;
  482. if (this.data.deviceListIndex === null) {
  483. return;
  484. };
  485. listByDevice({
  486. clientType
  487. }).then((res) => {
  488. _this.setData({
  489. channelData: res
  490. });
  491. // 接收设备当前播放状态
  492. const obj = {
  493. DstDeviceName: _this.getThisDeviceID()
  494. }
  495. app.PubMsg({
  496. type: "get_position",
  497. ...obj
  498. });
  499. wx.setStorage({
  500. key: "channelData",
  501. data: res
  502. });
  503. // 有新设备
  504. if (app.globalData.newDeviceId) {
  505. app.globalData.newDeviceId = null;
  506. return;
  507. // 去掉此功能,先留着吧
  508. // const other= {
  509. // "url": "",
  510. // "media_data": "",
  511. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  512. // "timestamp": `${Math.round(new Date() / 1000)}`,
  513. // "channel_id": `${res[1].channelNum}`,
  514. // "order": "",
  515. // "resource_from": "",
  516. // "songAlbumID":"",
  517. // "version":3,
  518. // "is_debug": app.globalData.is_debug
  519. // };
  520. // app.PubMsg({
  521. // type: "play",
  522. // DstDeviceName: _this.getThisDeviceID(),
  523. // other
  524. // });
  525. }
  526. })
  527. },
  528. showDelete() {
  529. // wx.navigateTo({
  530. // url: './../deviceRoter3/deviceRoter3',
  531. // })
  532. this.setData({
  533. showDelete: !this.data.showDelete
  534. });
  535. },
  536. deleteDevice(e) {
  537. const id = this.data.deviceList[e.currentTarget.dataset.index].deviceId;
  538. let name = _this.data.deviceListIndex !== null ? this.data.deviceList[_this.data.deviceListIndex].name : null;
  539. const deviceList = this.data.deviceList.filter((v, i) => {
  540. return id !== v.deviceId
  541. });
  542. // 取消订阅
  543. app.unsubscribe(`/AIrSMArT_${_this.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  544. wx.setStorage({
  545. key: "devicelist",
  546. data: JSON.stringify(deviceList),
  547. success() {
  548. _this.setData({
  549. deviceList,
  550. thisDeviceMac: null
  551. });
  552. // 当前没有设备
  553. if (deviceList.length === 0) {
  554. _this.setData({
  555. showDelete: false,
  556. actionIndex: null,
  557. deviceListIndex: null,
  558. isShowDevicelist: false,
  559. });
  560. }
  561. if (_this.data.deviceListIndex === null) {
  562. return
  563. };
  564. if (e.currentTarget.dataset.index === _this.data.deviceListIndex) {
  565. let index_ = null;
  566. deviceList.map((v, index) => {
  567. if (v.state === "online" && index_ === null) {
  568. index_ = index;
  569. }
  570. });
  571. if (index_ !== null) {
  572. _this.actionDevice(index_);
  573. } else {
  574. _this.setData({
  575. actionIndex: null,
  576. deviceListIndex: null,
  577. });
  578. };
  579. } else {
  580. deviceList.map((v, index) => {
  581. if (v.name === name) {
  582. _this.setData({
  583. deviceListIndex: index,
  584. });
  585. }
  586. });
  587. }
  588. }
  589. })
  590. },
  591. goWake() {
  592. if (this.data.deviceListIndex === null) {
  593. return;
  594. };
  595. wx.navigateTo({
  596. url: './../deviceWake/deviceWake?deviceId=' + this.data.deviceList[this.data.deviceListIndex].deviceId + "&clientType=" + this.data.deviceList[this.data.deviceListIndex].ProdModel,
  597. });
  598. },
  599. goChnnel() {
  600. wx.setStorage({
  601. key: "channelDeta",
  602. data: this.data.channelData[this.data.actionIndex],
  603. success() {
  604. wx.navigateTo({
  605. url: './../channelDetails/channelDetails'
  606. })
  607. }
  608. })
  609. },
  610. updata() {
  611. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  612. wx.getUpdateManager().applyUpdate()
  613. },
  614. updataClone() {
  615. this.setData({
  616. newVersion: false
  617. })
  618. },
  619. onUnload() {
  620. if (app.globalData.client === null) {
  621. return;
  622. };
  623. app.globalData.client.end(true);
  624. app.globalData.client.end(true);
  625. app.globalData.client = null;
  626. },
  627. onTapIndex(e) {
  628. var that = this;
  629. var index = e.currentTarget.dataset.index;
  630. var indexPage = that.data.indexPage;
  631. if (indexPage != index) {
  632. that.setData({
  633. indexPage: index,
  634. });
  635. }
  636. },
  637. goMeAbout() {
  638. wx.navigateTo({
  639. url: './../about/about',
  640. })
  641. },
  642. goMeLogin() {
  643. const _this = this;
  644. if (this.data.islogin) {
  645. // 退出登录
  646. wx.removeStorage({
  647. key: 'userInfo',
  648. success(res) {
  649. _this.setData({
  650. islogin: false,
  651. userName: "未登录",
  652. userPic: "./../../img/head_pic.png",
  653. });
  654. wx.switchTab({
  655. url: `./../index/index`
  656. });
  657. }
  658. });
  659. wx.removeStorage({
  660. key: 'token',
  661. })
  662. } else {
  663. // 登录
  664. wx.navigateTo({
  665. url: './../login/login',
  666. });
  667. }
  668. },
  669. })