index.js 27 KB

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