index.js 19 KB

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