index.js 18 KB

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