index.js 16 KB

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