index.js 25 KB

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