index.js 25 KB

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