index.js 18 KB

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