index.js 25 KB

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