index.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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("本地设备列表", 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. that.data.deviceList.map((v, index) => {
  340. if (payloads.SrcDeviceName && payloads.SrcDeviceName.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  341. that.data.deviceList[index].ProdModel = payloads.other.ProdModel;
  342. that.data.deviceList[index].devName = payloads.other.devName;
  343. }
  344. });
  345. // 更新缓存
  346. wx.setStorage({
  347. key: "devicelist",
  348. data: JSON.stringify(that.data.deviceList),
  349. success() {
  350. that.setData({
  351. deviceList: that.data.deviceList
  352. })
  353. }
  354. });
  355. } else if (payloads.type === "battery" && payloads.other) {
  356. that.setData({
  357. battery: that._battery(payloads.other.battery),
  358. })
  359. }
  360. break;
  361. default:
  362. }
  363. },
  364. // 格式化电量
  365. _battery(battery) {
  366. let _battery = 0;
  367. if (battery < 20) {
  368. _battery = 0
  369. } else if (20 <= battery && battery < 40) {
  370. _battery = 1
  371. } else if (40 <= battery && battery < 60) {
  372. _battery = 2
  373. } else if (60 <= battery && battery < 80) {
  374. _battery = 3
  375. } else if (80 <= battery && battery <= 100) {
  376. _battery = 4
  377. } else if (battery > 100) {
  378. _battery = 5
  379. };
  380. return _battery
  381. },
  382. connectSuccess() {
  383. // 订阅设备在线信息
  384. var that = this;
  385. that.subscribeDevicesStatus();
  386. },
  387. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  388. online(payloads) {
  389. // 设置在线状态
  390. var that = this;
  391. that.data.deviceList.map((v, index) => {
  392. if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
  393. that.data.deviceList[index].state = payloads.state;
  394. }
  395. });
  396. that.setData({
  397. deviceList: that.data.deviceList,
  398. });
  399. // 更新缓存
  400. wx.setStorage({
  401. key: "devicelist",
  402. data: JSON.stringify(that.data.deviceList)
  403. });
  404. // 如没有选中,选中最新的
  405. (() => {
  406. if (that.data.deviceListIndex === null) {
  407. let itue = false;
  408. that.data.deviceList.map((v, index) => {
  409. if (v.state === "online" && !itue) {
  410. itue = true;
  411. that.actionDevice(index);
  412. }
  413. });
  414. }
  415. // else {
  416. // that.actionDevice(that.data.deviceListIndex);
  417. // };
  418. })();
  419. // 当前播放设备离线
  420. if (that.data.deviceListIndex !== null && that.data.deviceList[that.data.deviceListIndex].state !== "online") {
  421. that.setData({
  422. actionIndex: null,
  423. deviceListIndex: null,
  424. });
  425. };
  426. },
  427. // 订阅设备在线状态
  428. subscribeDevicesStatus() {
  429. var that = this;
  430. that.data.deviceList.forEach((value) => {
  431. let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
  432. app.subscribe(topic);
  433. })
  434. },
  435. subscribeCurrDevice() {
  436. var that = this;
  437. if (!(app.globalData.client && app.globalData.client.connected)) {
  438. console.log("未连接MQTT服务器");
  439. const str = setInterval(() => {
  440. clearInterval(str);
  441. that.subscribeCurrDevice();
  442. }, 500);
  443. return;
  444. };
  445. if (that.data.deviceList.length === 0 || that.data.deviceListIndex === null) {
  446. return
  447. };
  448. let topic = `/AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
  449. app.subscribe(topic);
  450. const obj = {
  451. DstDeviceName: that.getThisDeviceID()
  452. };
  453. app.PubMsg({
  454. type: "get_dev_info",
  455. ...obj
  456. });
  457. },
  458. actionMusic(e) {
  459. var that = this;
  460. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  461. return;
  462. };
  463. if (that.data.deviceListIndex === null) {
  464. wx.showToast({
  465. title: '请选择设备',
  466. icon: "none"
  467. })
  468. return;
  469. };
  470. that.setData({
  471. actionIndex: e.currentTarget.dataset.index
  472. });
  473. const other = {
  474. "url": "",
  475. "media_data": "",
  476. "user_id": `${app.globalData.userInfo.deviceUid}`,
  477. "timestamp": `${Math.round(new Date() / 1000)}`,
  478. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  479. "order": "",
  480. "resource_from": "",
  481. "songAlbumID": "",
  482. "version": 3,
  483. "is_debug": app.globalData.is_debug
  484. };
  485. app.PubMsg({
  486. type: "play",
  487. DstDeviceName: that.getThisDeviceID(),
  488. other
  489. });
  490. },
  491. getThisDeviceID() {
  492. var that = this;
  493. return `AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}`
  494. },
  495. getchannelData(clientType) {
  496. var that = this;
  497. if (that.data.deviceListIndex === null) {
  498. return;
  499. };
  500. listByDevice({
  501. clientType
  502. }).then((res) => {
  503. that.setData({
  504. channelData: res
  505. });
  506. // 接收设备当前播放状态
  507. const obj = {
  508. DstDeviceName: that.getThisDeviceID()
  509. }
  510. app.PubMsg({
  511. type: "get_position",
  512. ...obj
  513. });
  514. wx.setStorage({
  515. key: "channelData",
  516. data: res
  517. });
  518. // 有新设备
  519. if (app.globalData.newDeviceId) {
  520. app.globalData.newDeviceId = null;
  521. return;
  522. // 去掉此功能,先留着吧
  523. // const other= {
  524. // "url": "",
  525. // "media_data": "",
  526. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  527. // "timestamp": `${Math.round(new Date() / 1000)}`,
  528. // "channel_id": `${res[1].channelNum}`,
  529. // "order": "",
  530. // "resource_from": "",
  531. // "songAlbumID":"",
  532. // "version":3,
  533. // "is_debug": app.globalData.is_debug
  534. // };
  535. // app.PubMsg({
  536. // type: "play",
  537. // DstDeviceName: that.getThisDeviceID(),
  538. // other
  539. // });
  540. }
  541. })
  542. },
  543. goChnnel() {
  544. var that = this;
  545. wx.setStorage({
  546. key: "channelDeta",
  547. data: that.data.channelData[that.data.actionIndex],
  548. success() {
  549. wx.navigateTo({
  550. url: './../channelDetails/channelDetails'
  551. })
  552. }
  553. })
  554. },
  555. updata() {
  556. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  557. wx.getUpdateManager().applyUpdate()
  558. },
  559. updataClone() {
  560. var that = this;
  561. that.setData({
  562. newVersion: false
  563. })
  564. },
  565. onUnload() {
  566. if (app.globalData.client === null) {
  567. return;
  568. };
  569. app.globalData.client.end(true);
  570. app.globalData.client.end(true);
  571. app.globalData.client = null;
  572. },
  573. onTapIndex(e) {
  574. var that = this;
  575. var index = e.currentTarget.dataset.index;
  576. var indexPage = that.data.indexPage;
  577. if (indexPage != index) {
  578. that.setData({
  579. indexPage: index,
  580. });
  581. }
  582. },
  583. goMeAbout() {
  584. wx.navigateTo({
  585. url: './../about/about',
  586. })
  587. },
  588. goMeLogin() {
  589. var that = this;
  590. if (that.data.islogin) {
  591. // 退出登录
  592. wx.removeStorage({
  593. key: 'userInfo',
  594. success(res) {
  595. var nickname = "未登录";
  596. var greeting = that.updateGreeting();
  597. if (nickname != "" && nickname != "未登录") {
  598. greeting = greeting + ',' + nickname;;
  599. }
  600. that.setData({
  601. phone: "",
  602. greeting: greeting,
  603. islogin: false,
  604. nickname: nickname,
  605. userPic: "./../../img/head_pic.png",
  606. });
  607. // wx.switchTab({
  608. // url: `./../index/index`
  609. // });
  610. }
  611. });
  612. wx.removeStorage({
  613. key: 'token',
  614. })
  615. } else {
  616. // 登录
  617. wx.navigateTo({
  618. url: './../login/login',
  619. });
  620. }
  621. },
  622. addDevice() {
  623. var that = this;
  624. var phone = that.data.phone;
  625. var showLogin = that.data.showLogin;
  626. if (phone == "" || showLogin) {
  627. wx.navigateTo({
  628. url: './../login/login'
  629. });
  630. return;
  631. }
  632. that.goDeviceConnect();
  633. },
  634. goDeviceConnect() {
  635. wx.navigateTo({
  636. url: './../deviceList/deviceList',
  637. });
  638. },
  639. ///点击item
  640. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  641. onTapIem(e) {
  642. var that = this;
  643. var item = e.currentTarget.dataset.item;
  644. var ProdModel = item.ProdModel;
  645. if (ProdModel == "猫王小王子OTR-X") {
  646. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  647. that.goWake();
  648. } else {
  649. that.actionDevice(e.currentTarget.dataset.index)
  650. }
  651. return;
  652. }
  653. ///去蓝牙连接处理
  654. },
  655. actionDevice(index) {
  656. var that = this;
  657. const device = that.data.deviceList[index];
  658. if (device.state !== "online") {
  659. return;
  660. };
  661. // 取消订阅
  662. if (that.data.thisDeviceMac !== null) {
  663. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  664. };
  665. that.setData({
  666. deviceListIndex: index,
  667. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}`
  668. });
  669. // app.PubMsg({
  670. // type: "get_dev_info",
  671. // DstDeviceName: that.getThisDeviceID()
  672. // });
  673. that.subscribeCurrDevice();
  674. },
  675. goWake() {
  676. var that = this;
  677. if (that.data.deviceListIndex === null) {
  678. return;
  679. };
  680. wx.navigateTo({
  681. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  682. });
  683. },
  684. ///点击banner事件
  685. onTapBanner() {
  686. var that = this;
  687. var item = e.currentTarget.dataset.item;
  688. },
  689. deleteDevice(e) {
  690. var that = this;
  691. wx.showModal({
  692. title: '确定删除?',
  693. success: function (res) {
  694. if (res.confirm) {
  695. if (that.data.deviceList[e.currentTarget.dataset.index].ProdModel != "MW-2AX(WIFI-N)") {
  696. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  697. const deviceList = that.data.deviceList.filter((item, i) => {
  698. return id !== item.deviceId
  699. });
  700. wx.setStorage({
  701. key: "devicelist",
  702. data: JSON.stringify(deviceList),
  703. success() {
  704. that.setData({
  705. deviceList,
  706. });
  707. // 当前没有设备
  708. if (deviceList.length === 0) {
  709. that.setData({
  710. deviceListIndex: null,
  711. });
  712. }
  713. }
  714. });
  715. return;
  716. }
  717. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  718. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  719. const deviceList = that.data.deviceList.filter((item, i) => {
  720. return id !== item.deviceId
  721. });
  722. // 取消订阅
  723. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  724. wx.setStorage({
  725. key: "devicelist",
  726. data: JSON.stringify(deviceList),
  727. success() {
  728. that.setData({
  729. deviceList,
  730. thisDeviceMac: null
  731. });
  732. // 当前没有设备
  733. if (deviceList.length === 0) {
  734. that.setData({
  735. actionIndex: null,
  736. deviceListIndex: null,
  737. });
  738. }
  739. if (that.data.deviceListIndex === null) {
  740. return
  741. };
  742. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  743. let index_ = null;
  744. deviceList.map((v, index) => {
  745. if (v.state === "online" && index_ === null) {
  746. index_ = index;
  747. }
  748. });
  749. if (index_ !== null) {
  750. that.actionDevice(index_);
  751. } else {
  752. that.setData({
  753. actionIndex: null,
  754. deviceListIndex: null,
  755. });
  756. };
  757. } else {
  758. deviceList.map((v, index) => {
  759. if (v.name === name) {
  760. that.setData({
  761. deviceListIndex: index,
  762. });
  763. }
  764. });
  765. }
  766. }
  767. });
  768. }
  769. }
  770. });
  771. },
  772. // {"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",
  773. /// deviceList
  774. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  775. /// clientType
  776. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  777. addConnectDevice(connectDevice) {
  778. var that = this;
  779. var deviceList = that.data.deviceList;
  780. connectDevice.state = "online";
  781. connectDevice.ProdModel = connectDevice.clientType;
  782. connectDevice.devName = connectDevice.name;
  783. connectDevice.name = connectDevice.deviceId;
  784. // deviceList.push(connectDevice);
  785. deviceList.unshift(connectDevice);
  786. var finalList = that.updateDeviceList(deviceList);
  787. var indexPage = that.data.indexPage;
  788. if (indexPage != 0) {
  789. that.setData({
  790. indexPage: 0,
  791. deviceListIndex: 0,
  792. deviceList: finalList,
  793. });
  794. } else {
  795. that.setData({
  796. deviceListIndex: 0,
  797. deviceList: finalList,
  798. });
  799. }
  800. // 更新缓存
  801. wx.setStorage({
  802. key: "devicelist",
  803. data: JSON.stringify(finalList)
  804. });
  805. // console.log("dfadsfasdfasdfa==111==" + JSON.stringify(that.data.deviceList))
  806. // console.log("dfadsfasdfasdfa==222==" + that.data.deviceList.length)
  807. },
  808. updateDeviceList(deviceList) {
  809. if (deviceList.length == 0) return [];
  810. var finalList = [];
  811. finalList.push(deviceList[0]);
  812. var onLineList = [];
  813. var onNoLineList = [];
  814. for (var i = 0; i < deviceList.length; i++) {
  815. if (i > 0) {
  816. if (deviceList[i].state == "online") {
  817. onLineList.push(deviceList[i])
  818. } else {
  819. onNoLineList.push(deviceList[i])
  820. }
  821. }
  822. }
  823. onLineList.forEach(element => {
  824. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  825. onLineList.push(element)
  826. } else {
  827. onNoLineList.push(element)
  828. }
  829. });
  830. var onLineWifiList = [];
  831. var onLineNoWifiList = [];
  832. onNoLineList.forEach(element => {
  833. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  834. onLineWifiList.push(element)
  835. } else {
  836. onLineNoWifiList.push(element)
  837. }
  838. });
  839. finalList = finalList.concat(onLineWifiList);
  840. finalList = finalList.concat(onLineNoWifiList);
  841. var onNoLineWifiList = [];
  842. var onNoLineNoWifiList = [];
  843. onNoLineList.forEach(element => {
  844. if (element.ProdModel == "MW-2AX(WIFI-N)") {
  845. onNoLineWifiList.push(element)
  846. } else {
  847. onNoLineNoWifiList.push(element)
  848. }
  849. });
  850. finalList = finalList.concat(onNoLineWifiList);
  851. finalList = finalList.concat(onNoLineWifiList);
  852. return finalList;
  853. },
  854. })