index.js 16 KB

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