index.js 24 KB

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