index.js 18 KB

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