index.js 19 KB

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