index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. const app = getApp();
  2. const {
  3. getBanner,
  4. login,
  5. listByDevice
  6. } = require('../../utils/api.js');
  7. import timeUtil from '../../utils/time_util.js';
  8. Page({
  9. data: {
  10. nvabarData: {
  11. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  12. title: 'OhPlay', //导航栏 中间的标题
  13. },
  14. ///下午好
  15. greeting: "",
  16. ///用来判断是否已登录
  17. phone: "",
  18. bannerList: [],
  19. autoplay: true,
  20. interval: 3000, // 切换时间间隔
  21. duration: 500, // 滑动动画时长
  22. circular: true, // 衔接滑动
  23. indexPage: 0,
  24. ///是否展示频道
  25. showChannel: false,
  26. ////首页
  27. navBarHeight: app.globalData.navBarHeight,
  28. MenuButtonheight: app.globalData.MenuButtonheight,
  29. MenuButtonTop: app.globalData.MenuButtonTop,
  30. actionIndex: null,
  31. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  32. channelData: [],
  33. deviceList: [],
  34. deviceListIndex: null,
  35. showLogin: false,
  36. isOneLoading: true,
  37. uid: null,
  38. isSetWake: false,
  39. thisDeviceMac: null,
  40. battery: 4, // 0≤电量<20,0格
  41. ////我的界面
  42. loginStatus: true,
  43. nickName: "未登录",
  44. userPic: './../../img/head_pic.png',
  45. islogin: false,
  46. },
  47. onLoad(options) {
  48. var that = this;
  49. that.onGreetLoad();
  50. that.onBannerLoad();
  51. that.onHomeLoad();
  52. that.onMeLoad();
  53. },
  54. onShow() {
  55. var that = this;
  56. that.onHomeShow();
  57. that.onMeLoad();
  58. that.onBannerShow();
  59. },
  60. onHomeLoad() {
  61. var that = this;
  62. // wx.showLoading({
  63. // title: '加载中',
  64. // });
  65. const str = setTimeout(() => {
  66. clearTimeout(str);
  67. if (that.data.isOneLoading) {
  68. that.setData({
  69. isOneLoading: false,
  70. });
  71. }
  72. }, 500);
  73. // 获取缓存的频道数据
  74. wx.getStorage({
  75. key: "channelData",
  76. success(res) {
  77. that.setData({
  78. channelData: res.data
  79. })
  80. }
  81. });
  82. // 登录
  83. that.login();
  84. },
  85. onMeLoad() {
  86. var that = this;
  87. wx.getStorage({
  88. key: "userInfo",
  89. success(res) {
  90. var phone = res.data.phone;
  91. if (!phone) {
  92. return;
  93. };
  94. var nickName = res.data.nickName || "";
  95. var greeting = that.getGreetBuNickName(nickName);
  96. that.setData({
  97. phone: phone,
  98. greeting: greeting,
  99. nickName: nickName,
  100. userPic: res.data.headUrl || "",
  101. islogin: true,
  102. });
  103. }
  104. })
  105. },
  106. onHomeShow() {
  107. var that = this;
  108. if ((!that.data.isOneLoading)) {
  109. wx.getStorage({
  110. key: 'userInfo',
  111. fail(res) {
  112. // 取消订阅
  113. if (that.data.thisDeviceMac !== null) {
  114. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  115. };
  116. that.setData({
  117. showLogin: true,
  118. });
  119. },
  120. success() {
  121. if (app.globalData.newDeviceId) {
  122. // 获取设备本地数据
  123. that.getDeviceData();
  124. } else if (that.data.deviceListIndex !== null) {
  125. // 更新
  126. that.actionDevice(that.data.deviceListIndex);
  127. }
  128. }
  129. });
  130. };
  131. },
  132. ////开始是home的
  133. login() {
  134. var that = this;
  135. wx.login({
  136. success: res => {
  137. let phone = undefined;
  138. try {
  139. var value = wx.getStorageSync('userInfo')
  140. if (value) {
  141. phone = value.phone || undefined;
  142. };
  143. } catch (e) {
  144. // Do something when catch error
  145. };
  146. // 请求登录
  147. login({
  148. code: res.code,
  149. phone
  150. }).then((res) => {
  151. app.globalData.userInfo = res;
  152. wx.setStorage({
  153. key: "userInfo",
  154. data: res
  155. });
  156. // 需要用授权登录
  157. if ((res.isNewUser && res.isNewUser === true) || !phone) {
  158. that.setData({
  159. showLogin: true,
  160. });
  161. return;
  162. };
  163. // 获取设备本地数据
  164. that.getDeviceData();
  165. that.setData({
  166. showLogin: false,
  167. });
  168. wx.setStorage({
  169. key: "token",
  170. data: JSON.stringify({
  171. id: res.userId,
  172. token: res.accessToken,
  173. }),
  174. });
  175. });
  176. },
  177. })
  178. },
  179. ///开始时home的
  180. getDeviceData() {
  181. var that = this;
  182. // 获取设备数据
  183. wx.getStorage({
  184. key: 'devicelist',
  185. success(res) {
  186. // wx.hideLoading();
  187. if (res.data) {
  188. const resData = JSON.parse(res.data);
  189. console.log("gadsfadsfadsfa==333===" + JSON.stringify(resData));
  190. that.setData({
  191. deviceList: resData
  192. });
  193. // 连接mqtt
  194. if (app.globalData.client === null) {
  195. app.connect();
  196. } else if (app.globalData.newDeviceId) {
  197. that.setData({
  198. actionIndex: null,
  199. deviceListIndex: null,
  200. });
  201. // 监听
  202. let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  203. app.subscribe(topic);
  204. const Timeout = setTimeout(() => {
  205. clearTimeout(Timeout);
  206. that.actionDevice(0);
  207. }, 1000);
  208. }
  209. // // 有新设备
  210. // if(app.globalData.newDeviceId) {
  211. // that.setData({
  212. // actionIndex: null,
  213. // deviceListIndex: null,
  214. // });
  215. // let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
  216. // app.subscribe(topic);
  217. // }
  218. }
  219. },
  220. fail(e) {
  221. // wx.hideLoading();
  222. }
  223. });
  224. },
  225. // 回调
  226. mqttCallback(type, option) {
  227. // console.log("gadsfadsfqwerq===" + type + "===" + option);
  228. var that = this;
  229. let payloads = null;
  230. if (option) {
  231. payloads = JSON.parse(option.payload);
  232. };
  233. switch (type) {
  234. case "connect":
  235. that.connectSuccess();
  236. break;
  237. case "message_onoffline":
  238. that.online(payloads);
  239. break;
  240. case "message":
  241. // 接收设备播放信息
  242. if (payloads.type === "get_position" && payloads.other) {
  243. let actionIndex = null;
  244. that.data.channelData.map((v, index) => {
  245. if (v.channelNum === payloads.other.channel) {
  246. actionIndex = index;
  247. }
  248. });
  249. that.setData({
  250. actionIndex,
  251. });
  252. } else if (payloads.type === "play" || payloads.type === "play_state") {
  253. if (that.data.deviceListIndex === null) {
  254. return;
  255. }
  256. // 接收设备当前播放状态
  257. const obj = {
  258. DstDeviceName: that.getThisDeviceID()
  259. }
  260. app.PubMsg({
  261. type: "get_position",
  262. ...obj
  263. });
  264. } else if (payloads.type === "get_dev_info") {
  265. // 接收设备当前信息
  266. that.getchannelData(payloads.other.ProdModel);
  267. // 电量
  268. that.setData({
  269. battery: that._battery(payloads.other.Power),
  270. })
  271. // 当前设备木有设置定时
  272. that.setData({
  273. isSetWake: false,
  274. });
  275. payloads.other.alarm.map((v) => {
  276. if (v.enable === "1") {
  277. that.setData({
  278. isSetWake: true
  279. });
  280. }
  281. });
  282. // 更新信息
  283. ///连上就调用2次
  284. that.data.deviceList.map((v, index) => {
  285. // console.log("gadsfadsfadsfa==444===" + payloads.SrcDeviceName);
  286. // console.log("gadsfadsfadsfa==555===" + JSON.stringify(payloads.other));
  287. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  288. /// payloads.other
  289. // {"Guid":"AIrSMArT_7cdfa1fcbb24","ProdModel":"MW-2AX(WIFI-N)","sim_iccid":"","esim_iccid":"","SoftVer":"3.0.7","Firmware":"RTOS","Power":20,"lowBatteryNtf":0,"Volume":24,"Mute":0,"pauseShutdown":600,"PlayState":4,"PlayMode":0,"devName":"猫王小王子OTR-X","deviceId":"","deviceSecret":"","is_tts_enable":1,"SignalQuality":100,"CardIndex":0,"CardSum":0,"NetModel":2,"is_netmode_auto":2,"Imei":"","mid":"","deviceDSN":"","userid":"1","user_id_white":"10000309","alarm":[{"alarm_id":"0","alarm_name":"close","current_timestamp":"2024-03-31 19:20:39","on_off_timestamp":"19:30:39","operation":"off","song_uri":"","weekly_repeat":"0","enable":"0","action":"update","week_actives":[0,0,0,0,0,0,0]},{"alarm_id":"1","alarm_name":"open","current_timestamp":"2023-11-17 12:25:10","on_off_timestamp":"13:35:00","operation":"on","song_uri":"","weekly_repeat":"1","enable":"1","action":"update","week_actives":[1,1,1,1,1,1,1]}]}
  290. ///payloads.SrcDeviceName AIrSMArT_7cdfa1fcbb24
  291. if (payloads.SrcDeviceName && payloads.SrcDeviceName.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  292. that.data.deviceList[index].ProdModel = payloads.other.ProdModel;
  293. that.data.deviceList[index].devName = payloads.other.devName;
  294. }
  295. });
  296. // 更新缓存
  297. wx.setStorage({
  298. key: "devicelist",
  299. data: JSON.stringify(that.data.deviceList),
  300. success() {
  301. that.setData({
  302. deviceList: that.data.deviceList
  303. })
  304. }
  305. });
  306. } else if (payloads.type === "battery" && payloads.other) {
  307. that.setData({
  308. battery: that._battery(payloads.other.battery),
  309. })
  310. }
  311. break;
  312. default:
  313. }
  314. },
  315. // 格式化电量
  316. _battery(battery) {
  317. let _battery = 0;
  318. if (battery < 20) {
  319. _battery = 0
  320. } else if (20 <= battery && battery < 40) {
  321. _battery = 1
  322. } else if (40 <= battery && battery < 60) {
  323. _battery = 2
  324. } else if (60 <= battery && battery < 80) {
  325. _battery = 3
  326. } else if (80 <= battery && battery <= 100) {
  327. _battery = 4
  328. } else if (battery > 100) {
  329. _battery = 5
  330. };
  331. return _battery
  332. },
  333. connectSuccess() {
  334. // 订阅设备在线信息
  335. var that = this;
  336. that.subscribeDevicesStatus();
  337. },
  338. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  339. ///连上就调用2次
  340. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  341. online(payloads) {
  342. // 设置在线状态
  343. var that = this;
  344. console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  345. that.data.deviceList.map((v, index) => {
  346. if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  347. that.data.deviceList[index].state = payloads.state;
  348. }
  349. });
  350. that.setData({
  351. deviceList: that.data.deviceList,
  352. });
  353. // 更新缓存
  354. wx.setStorage({
  355. key: "devicelist",
  356. data: JSON.stringify(that.data.deviceList)
  357. });
  358. // 如没有选中,选中最新的
  359. (() => {
  360. if (that.data.deviceListIndex === null) {
  361. let itue = false;
  362. that.data.deviceList.map((v, index) => {
  363. if (v.state === "online" && !itue) {
  364. itue = true;
  365. that.actionDevice(index);
  366. }
  367. });
  368. }
  369. // else {
  370. // that.actionDevice(that.data.deviceListIndex);
  371. // };
  372. })();
  373. // 当前播放设备离线
  374. if (that.data.deviceListIndex !== null && that.data.deviceList[that.data.deviceListIndex].state !== "online") {
  375. that.setData({
  376. actionIndex: null,
  377. deviceListIndex: null,
  378. });
  379. };
  380. },
  381. // 订阅设备在线状态
  382. subscribeDevicesStatus() {
  383. var that = this;
  384. that.data.deviceList.forEach((value) => {
  385. let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
  386. app.subscribe(topic);
  387. })
  388. },
  389. subscribeCurrDevice() {
  390. var that = this;
  391. if (!(app.globalData.client && app.globalData.client.connected)) {
  392. console.log("未连接MQTT服务器");
  393. const str = setInterval(() => {
  394. clearInterval(str);
  395. that.subscribeCurrDevice();
  396. }, 500);
  397. return;
  398. };
  399. if (that.data.deviceList.length === 0 || that.data.deviceListIndex === null) {
  400. return
  401. };
  402. let topic = `/AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
  403. app.subscribe(topic);
  404. const obj = {
  405. DstDeviceName: that.getThisDeviceID()
  406. };
  407. app.PubMsg({
  408. type: "get_dev_info",
  409. ...obj
  410. });
  411. },
  412. actionMusic(e) {
  413. var that = this;
  414. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  415. return;
  416. };
  417. if (that.data.deviceListIndex === null) {
  418. wx.showToast({
  419. title: '请选择设备',
  420. icon: "none"
  421. })
  422. return;
  423. };
  424. that.setData({
  425. actionIndex: e.currentTarget.dataset.index
  426. });
  427. const other = {
  428. "url": "",
  429. "media_data": "",
  430. "user_id": `${app.globalData.userInfo.deviceUid}`,
  431. "timestamp": `${Math.round(new Date() / 1000)}`,
  432. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  433. "order": "",
  434. "resource_from": "",
  435. "songAlbumID": "",
  436. "version": 3,
  437. "is_debug": app.globalData.is_debug
  438. };
  439. app.PubMsg({
  440. type: "play",
  441. DstDeviceName: that.getThisDeviceID(),
  442. other
  443. });
  444. },
  445. getThisDeviceID() {
  446. var that = this;
  447. return `AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}`
  448. },
  449. getchannelData(clientType) {
  450. var that = this;
  451. console.log("gadfadsfqwwerqewrqr==000=" + clientType);
  452. if (that.data.deviceListIndex === null) {
  453. return;
  454. };
  455. listByDevice({
  456. clientType
  457. }).then((res) => {
  458. that.setData({
  459. channelData: res
  460. });
  461. // 接收设备当前播放状态
  462. const obj = {
  463. DstDeviceName: that.getThisDeviceID()
  464. }
  465. app.PubMsg({
  466. type: "get_position",
  467. ...obj
  468. });
  469. wx.setStorage({
  470. key: "channelData",
  471. data: res
  472. });
  473. // 有新设备
  474. if (app.globalData.newDeviceId) {
  475. app.globalData.newDeviceId = null;
  476. return;
  477. // 去掉此功能,先留着吧
  478. // const other= {
  479. // "url": "",
  480. // "media_data": "",
  481. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  482. // "timestamp": `${Math.round(new Date() / 1000)}`,
  483. // "channel_id": `${res[1].channelNum}`,
  484. // "order": "",
  485. // "resource_from": "",
  486. // "songAlbumID":"",
  487. // "version":3,
  488. // "is_debug": app.globalData.is_debug
  489. // };
  490. // app.PubMsg({
  491. // type: "play",
  492. // DstDeviceName: that.getThisDeviceID(),
  493. // other
  494. // });
  495. }
  496. })
  497. },
  498. goChannel() {
  499. var that = this;
  500. wx.setStorage({
  501. key: "channelDeta",
  502. data: that.data.channelData[that.data.actionIndex],
  503. success() {
  504. wx.navigateTo({
  505. url: './../channelDetails/channelDetails'
  506. })
  507. }
  508. })
  509. },
  510. updata() {
  511. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  512. wx.getUpdateManager().applyUpdate()
  513. },
  514. onUnload() {
  515. if (app.globalData.client === null) {
  516. return;
  517. };
  518. app.globalData.client.end(true);
  519. app.globalData.client.end(true);
  520. app.globalData.client = null;
  521. },
  522. onTapIndex(e) {
  523. var that = this;
  524. var index = e.currentTarget.dataset.index;
  525. var indexPage = that.data.indexPage;
  526. if (indexPage != index) {
  527. that.setData({
  528. indexPage: index,
  529. });
  530. }
  531. },
  532. goMeAbout() {
  533. wx.navigateTo({
  534. url: './../about/about',
  535. })
  536. },
  537. goMeLogin() {
  538. var that = this;
  539. if (that.data.islogin) {
  540. // 退出登录
  541. wx.removeStorage({
  542. key: 'userInfo',
  543. success(res) {
  544. var nickName = "未登录";
  545. var greeting = that.getGreetBuNickName(nickName);
  546. that.setData({
  547. phone: "",
  548. greeting: greeting,
  549. islogin: false,
  550. nickName: nickName,
  551. userPic: "./../../img/head_pic.png",
  552. });
  553. // wx.switchTab({
  554. // url: `./../index/index`
  555. // });
  556. }
  557. });
  558. wx.removeStorage({
  559. key: 'token',
  560. })
  561. } else {
  562. // 登录
  563. wx.navigateTo({
  564. url: './../login/login',
  565. });
  566. }
  567. },
  568. addDevice() {
  569. var that = this;
  570. var phone = that.data.phone;
  571. var showLogin = that.data.showLogin;
  572. if (phone == "" || showLogin) {
  573. wx.navigateTo({
  574. url: './../login/login'
  575. });
  576. return;
  577. }
  578. that.goDeviceConnect();
  579. },
  580. goDeviceConnect() {
  581. wx.navigateTo({
  582. url: './../deviceList/deviceList',
  583. });
  584. },
  585. ///点击item
  586. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  587. onTapItem(e) {
  588. var that = this;
  589. var item = e.currentTarget.dataset.item;
  590. var ProdModel = item.ProdModel;
  591. if (ProdModel == "MW-2AX(WIFI-N)") {
  592. console.log("gadfadsfadsfaf===aa");
  593. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  594. console.log("gadfadsfadsfaf===bbb");
  595. that.goWake();
  596. } else {
  597. console.log("gadfadsfadsfaf===ccc");
  598. that.actionDevice(e.currentTarget.dataset.index)
  599. }
  600. return;
  601. }
  602. ///去蓝牙连接处理
  603. that.addConnectBlueDevice({
  604. deviceId: item.deviceId,
  605. name: item.devName,
  606. state: item.state,
  607. clientType: item.ProdModel,
  608. mac: item.mac,
  609. });
  610. },
  611. actionDevice(index) {
  612. var that = this;
  613. const device = that.data.deviceList[index];
  614. if (device.state !== "online") {
  615. return;
  616. };
  617. // 取消订阅
  618. if (that.data.thisDeviceMac !== null) {
  619. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  620. };
  621. that.setData({
  622. deviceListIndex: index,
  623. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}`
  624. });
  625. // app.PubMsg({
  626. // type: "get_dev_info",
  627. // DstDeviceName: that.getThisDeviceID()
  628. // });
  629. that.subscribeCurrDevice();
  630. },
  631. goWake() {
  632. var that = this;
  633. if (that.data.deviceListIndex === null) {
  634. return;
  635. };
  636. wx.navigateTo({
  637. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  638. });
  639. },
  640. ///点击banner事件
  641. onTapBanner() {
  642. var that = this;
  643. var item = e.currentTarget.dataset.item;
  644. },
  645. deleteDevice(e) {
  646. var that = this;
  647. wx.showModal({
  648. title: '确定删除?',
  649. success: function (res) {
  650. if (res.confirm) {
  651. if (that.data.deviceList[e.currentTarget.dataset.index].ProdModel != "MW-2AX(WIFI-N)") {
  652. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  653. const deviceList = that.data.deviceList.filter((item, i) => {
  654. return id !== item.deviceId
  655. });
  656. wx.setStorage({
  657. key: "devicelist",
  658. data: JSON.stringify(deviceList),
  659. success() {
  660. that.setData({
  661. deviceList,
  662. });
  663. // 当前没有设备
  664. if (deviceList.length === 0) {
  665. that.setData({
  666. deviceListIndex: null,
  667. });
  668. }
  669. }
  670. });
  671. return;
  672. }
  673. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  674. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  675. const deviceList = that.data.deviceList.filter((item, i) => {
  676. return id !== item.deviceId
  677. });
  678. // 取消订阅
  679. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  680. wx.setStorage({
  681. key: "devicelist",
  682. data: JSON.stringify(deviceList),
  683. success() {
  684. that.setData({
  685. deviceList,
  686. thisDeviceMac: null
  687. });
  688. // 当前没有设备
  689. if (deviceList.length === 0) {
  690. that.setData({
  691. actionIndex: null,
  692. deviceListIndex: null,
  693. });
  694. }
  695. if (that.data.deviceListIndex === null) {
  696. return
  697. };
  698. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  699. let index_ = null;
  700. deviceList.map((v, index) => {
  701. if (v.state === "online" && index_ === null) {
  702. index_ = index;
  703. }
  704. });
  705. if (index_ !== null) {
  706. that.actionDevice(index_);
  707. } else {
  708. that.setData({
  709. actionIndex: null,
  710. deviceListIndex: null,
  711. });
  712. };
  713. } else {
  714. deviceList.map((v, index) => {
  715. if (v.name === name) {
  716. that.setData({
  717. deviceListIndex: index,
  718. });
  719. }
  720. });
  721. }
  722. }
  723. });
  724. }
  725. }
  726. });
  727. },
  728. // {"applicationType":"[0, 1]","deviceId":"DB:45:DD:76:42:15","img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100711728016597.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100714667384264.png","connectImg":null,"name":"猫王音响·小王子 OTR-X","bluetoothName":"猫王音响·小王子 OTR-X","bluetoothNames":["猫王音响·小王子 OTR-X"],"isChannelsPlatforms":0,"platform":-1,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[1,3,6],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100644913162836.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100648938942906.png","icon":null,"guideUrl":null}},{"is5g":0,"type":2,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155903515728925.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155910706032704.png","icon":null,"guideUrl":null}}],"clientType":"MW-2AX(WIFI)","firstVersion":"0.0.1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0,"mac":"9b45dd76e2150000",
  729. /// deviceList
  730. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  731. /// clientType
  732. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  733. addConnectBlueDevice(newDevice) {
  734. var that = this;
  735. // 同一个设备
  736. var deviceList = that.data.deviceList;
  737. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  738. if (tempList && tempList.length > 0) {
  739. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  740. };
  741. deviceList.unshift({
  742. /// 蓝牙ble连接
  743. connectType: 1,
  744. deviceId: newDevice.deviceId,
  745. name: newDevice.deviceId,
  746. state: "online",
  747. ProdModel: newDevice.clientType,
  748. devName: newDevice.name,
  749. mac: newDevice.mac,
  750. });
  751. var finalList = that.updateDeviceList(deviceList);
  752. var indexPage = that.data.indexPage;
  753. if (indexPage != 0) {
  754. that.setData({
  755. indexPage: 0,
  756. deviceListIndex: 0,
  757. deviceList: finalList,
  758. });
  759. } else {
  760. that.setData({
  761. deviceListIndex: 0,
  762. deviceList: finalList,
  763. });
  764. }
  765. // 更新缓存
  766. wx.setStorage({
  767. key: "devicelist",
  768. data: JSON.stringify(finalList)
  769. });
  770. },
  771. addConnectWifiDevice(deviceList) {
  772. var that = this;
  773. var finalList = that.updateDeviceList(deviceList);
  774. var indexPage = that.data.indexPage;
  775. if (indexPage != 0) {
  776. that.setData({
  777. indexPage: 0,
  778. deviceListIndex: 0,
  779. deviceList: finalList,
  780. });
  781. } else {
  782. that.setData({
  783. deviceListIndex: 0,
  784. deviceList: finalList,
  785. });
  786. }
  787. // 更新缓存
  788. wx.setStorage({
  789. key: "devicelist",
  790. data: JSON.stringify(finalList)
  791. });
  792. },
  793. updateDeviceList(deviceList) {
  794. if (deviceList.length == 0) return [];
  795. var finalList = [];
  796. finalList.push(deviceList[0]);
  797. var onLineList = [];
  798. var onNoLineList = [];
  799. for (var i = 0; i < deviceList.length; i++) {
  800. if (i > 0) {
  801. if (deviceList[i].state == "online") {
  802. onLineList.push(deviceList[i])
  803. } else {
  804. onNoLineList.push(deviceList[i])
  805. }
  806. }
  807. }
  808. onLineList.forEach(element => {
  809. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  810. onLineList.push(element)
  811. } else {
  812. onNoLineList.push(element)
  813. }
  814. });
  815. var onLineWifiList = [];
  816. var onLineNoWifiList = [];
  817. onNoLineList.forEach(element => {
  818. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  819. onLineWifiList.push(element)
  820. } else {
  821. onLineNoWifiList.push(element)
  822. }
  823. });
  824. finalList = finalList.concat(onLineWifiList);
  825. finalList = finalList.concat(onLineNoWifiList);
  826. var onNoLineWifiList = [];
  827. var onNoLineNoWifiList = [];
  828. onNoLineList.forEach(element => {
  829. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  830. onNoLineWifiList.push(element)
  831. } else {
  832. onNoLineNoWifiList.push(element)
  833. }
  834. });
  835. finalList = finalList.concat(onNoLineWifiList);
  836. finalList = finalList.concat(onNoLineWifiList);
  837. return finalList;
  838. },
  839. ///******************************* 可折叠 ********************************************///
  840. onGreetLoad() {
  841. var that = this;
  842. var nickName = that.data.nickName;
  843. var greeting = that.getGreetBuNickName(nickName);
  844. that.setData({
  845. greeting: greeting
  846. });
  847. },
  848. getGreetBuNickName(nickName) {
  849. var greeting = timeUtil.getGreet();
  850. if (nickName != "" && nickName != "未登录") {
  851. greeting = greeting + ',' + nickName;;
  852. }
  853. return greeting;
  854. },
  855. onBannerLoad() {
  856. var that = this;
  857. var bannerList = wx.getStorageSync("homeBanner") || [];
  858. if (that.data.bannerList.length == 0) {
  859. that.setData({
  860. bannerList: bannerList
  861. });
  862. }
  863. },
  864. onBannerShow() {
  865. var that = this;
  866. getBanner({}).then((res) => {
  867. that.setData({
  868. bannerList: res
  869. });
  870. wx.setStorageSync("homeBanner", res);
  871. })
  872. },
  873. })
  874. // updataClone() {
  875. // var that = this;
  876. // that.setData({
  877. // newVersion: false
  878. // })
  879. // },