index.js 27 KB

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