index.js 25 KB

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