index.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. if (!strings.isEmpty(devicelist)) {
  75. var list = JSON.parse(devicelist);
  76. that.updateDeviceList(list, true);
  77. }
  78. },
  79. addNewDeviceId() {
  80. // 连接mqtt
  81. var that = this;
  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 (deviceList[i].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. })
  368. },
  369. ///去频道详情
  370. onTapToChannel() {
  371. var that = this;
  372. if (that.data.channelData.length > that.data.actionIndex) {
  373. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  374. wx.navigateTo({
  375. url: './../channelDetails/channelDetails'
  376. });
  377. }
  378. },
  379. onUnload() {
  380. if (app.globalData.client === null) {
  381. return;
  382. };
  383. app.globalData.client.end(true);
  384. app.globalData.client.end(true);
  385. app.globalData.client = null;
  386. },
  387. onTapIndex(e) {
  388. var that = this;
  389. var index = e.currentTarget.dataset.index;
  390. var indexPage = that.data.indexPage;
  391. if (indexPage != index) {
  392. that.setData({
  393. indexPage: index,
  394. });
  395. }
  396. },
  397. goMeAbout() {
  398. wx.navigateTo({
  399. url: './../about/about',
  400. })
  401. },
  402. onTapLogin() {
  403. var that = this;
  404. ///退出登录
  405. if (that.data.isLogin) {
  406. that.logOut();
  407. }
  408. // 登录
  409. else {
  410. wx.navigateTo({
  411. url: './../login/login',
  412. });
  413. }
  414. },
  415. ///退出登录
  416. logOut() {
  417. var that = this;
  418. ///有设备在线被选中,则让它不被选择
  419. var deviceListIndex = that.data.deviceListIndex;
  420. if (deviceListIndex != null) {
  421. var list = that.data.deviceList;
  422. if (list.length > deviceListIndex) {
  423. if (list[deviceListIndex].connectType == 3) {
  424. if (that.data.thisDeviceMac !== null) {
  425. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  426. that.setData({
  427. actionIndex: null,
  428. deviceListIndex: null,
  429. });
  430. };
  431. }
  432. }
  433. }
  434. wx.removeStorageSync('userInfo');
  435. wx.removeStorageSync('token');
  436. var nickName = "未登录";
  437. var greeting = that.getGreetBuNickName(nickName);
  438. that.setData({
  439. isLogin: false,
  440. greeting: greeting,
  441. nickName: nickName,
  442. userPic: "./../../img/head_pic.png",
  443. });
  444. },
  445. addDevice() {
  446. var that = this;
  447. if (!that.data.isLogin) {
  448. wx.navigateTo({
  449. url: './../login/login'
  450. });
  451. return;
  452. }
  453. that.goDeviceConnect();
  454. },
  455. goDeviceConnect() {
  456. wx.navigateTo({
  457. url: './../deviceList/deviceList',
  458. });
  459. },
  460. ///点击item
  461. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  462. onTapItem(e) {
  463. if (!that.data.isLogin) {
  464. wx.navigateTo({
  465. url: './../login/login'
  466. });
  467. return;
  468. }
  469. var that = this;
  470. var item = e.currentTarget.dataset.item;
  471. var connectType = item.connectType;
  472. console.log("gadfadfqwwerq===00===" + connectType);
  473. if (connectType == 3) {
  474. console.log("gadfadfqwwerq===11===" + connectType);
  475. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  476. console.log("gadfadfqwwerq===22===" + connectType);
  477. that.goWake();
  478. } else {
  479. console.log("gadfadfqwwerq===33===" + connectType);
  480. that.actionDevice(e.currentTarget.dataset.index)
  481. }
  482. return;
  483. }
  484. ///去蓝牙连接处理
  485. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  486. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  487. } else {
  488. that.addConnectBlueDevice({
  489. deviceId: item.deviceId,
  490. name: item.devName,
  491. state: item.state,
  492. clientType: item.ProdModel,
  493. mac: item.mac,
  494. });
  495. }
  496. },
  497. actionDevice(index) {
  498. var that = this;
  499. if (that.data.deviceList.length <= index) {
  500. return;
  501. };
  502. const device = that.data.deviceList[index];
  503. if (device.state !== "online") {
  504. return;
  505. };
  506. // 取消订阅
  507. if (that.data.thisDeviceMac !== null) {
  508. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  509. };
  510. that.setData({
  511. deviceListIndex: index,
  512. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
  513. });
  514. // app.PubMsg({
  515. // type: "get_dev_info",
  516. // DstDeviceName: that.getThisDeviceID()
  517. // });
  518. that.subscribeCurrDevice();
  519. },
  520. goWake() {
  521. var that = this;
  522. if (that.data.deviceListIndex === null) {
  523. return;
  524. };
  525. wx.navigateTo({
  526. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  527. });
  528. },
  529. ///点击banner事件
  530. onTapBanner() {
  531. var that = this;
  532. var item = e.currentTarget.dataset.item;
  533. },
  534. deleteDevice(e) {
  535. var that = this;
  536. wx.showModal({
  537. title: '确定删除?',
  538. success: function (res) {
  539. if (res.confirm) {
  540. if (that.data.deviceList[e.currentTarget.dataset.index].connectType != 3) {
  541. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  542. const deviceList = that.data.deviceList.filter((item, i) => {
  543. return id !== item.deviceId
  544. });
  545. wx.setStorageSync("devicelist", JSON.stringify(deviceList));
  546. that.setData({
  547. deviceList,
  548. });
  549. // 当前没有设备
  550. if (deviceList.length === 0) {
  551. that.setData({
  552. deviceListIndex: null,
  553. });
  554. }
  555. return;
  556. }
  557. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  558. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  559. const deviceList = that.data.deviceList.filter((item, i) => {
  560. return id !== item.deviceId
  561. });
  562. // 取消订阅
  563. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  564. wx.setStorageSync("devicelist", JSON.stringify(deviceList));
  565. that.setData({
  566. deviceList,
  567. thisDeviceMac: null
  568. });
  569. // 当前没有设备
  570. if (deviceList.length === 0) {
  571. that.setData({
  572. actionIndex: null,
  573. deviceListIndex: null,
  574. });
  575. }
  576. if (that.data.deviceListIndex === null) {
  577. return
  578. };
  579. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  580. let index_ = null;
  581. deviceList.map((v, index) => {
  582. if (v.state === "online" && index_ === null) {
  583. index_ = index;
  584. }
  585. });
  586. if (index_ !== null) {
  587. that.actionDevice(index_);
  588. } else {
  589. that.setData({
  590. actionIndex: null,
  591. deviceListIndex: null,
  592. });
  593. };
  594. } else {
  595. deviceList.map((v, index) => {
  596. if (v.name === name) {
  597. that.setData({
  598. deviceListIndex: index,
  599. });
  600. }
  601. });
  602. }
  603. }
  604. }
  605. });
  606. },
  607. // {"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",
  608. /// deviceList
  609. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  610. /// clientType
  611. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  612. addConnectBlueDevice(newDevice) {
  613. var that = this;
  614. // 同一个设备
  615. var deviceList = that.data.deviceList;
  616. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  617. if (tempList && tempList.length > 0) {
  618. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  619. };
  620. deviceList.unshift({
  621. /// 蓝牙ble连接
  622. connectType: 1,
  623. deviceId: newDevice.deviceId,
  624. name: newDevice.deviceId,
  625. state: "online",
  626. ProdModel: newDevice.clientType,
  627. devName: newDevice.name,
  628. mac: newDevice.mac,
  629. });
  630. ///在线排序前面,wifi设备排序前面
  631. that.updateDeviceList(deviceList, false);
  632. var indexPage = that.data.indexPage;
  633. if (indexPage != 0) {
  634. that.setData({
  635. indexPage: 0,
  636. deviceListIndex: 0,
  637. });
  638. } else {
  639. that.setData({
  640. deviceListIndex: 0,
  641. });
  642. }
  643. },
  644. addConnectWifiDevice(deviceList) {
  645. var that = this;
  646. ///在线排序前面,wifi设备排序前面
  647. that.updateDeviceList(deviceList, false);
  648. var indexPage = that.data.indexPage;
  649. if (indexPage != 0) {
  650. that.setData({
  651. indexPage: 0,
  652. deviceListIndex: 0,
  653. });
  654. } else {
  655. that.setData({
  656. deviceListIndex: 0,
  657. });
  658. }
  659. },
  660. ///更新列表排序
  661. updateDeviceList(deviceList, isInit) {
  662. if (deviceList.length == 0) return [];
  663. var that = this;
  664. var finalList = [];
  665. finalList.push(deviceList[0]);
  666. var onLineList = [];
  667. var onNoLineList = [];
  668. ///区分在线和离线
  669. for (var i = 0; i < deviceList.length; i++) {
  670. if (isInit) {
  671. deviceList[i].state = "offline";
  672. } else {
  673. if (i > 0) {
  674. if (deviceList[i].state == "online") {
  675. onLineList.push(deviceList[i])
  676. } else {
  677. onNoLineList.push(deviceList[i])
  678. }
  679. }
  680. }
  681. }
  682. // 区分在线wifi和蓝牙 wifi在前 离线在后
  683. var onLineWifiList = [];
  684. var onLineNoWifiList = [];
  685. onLineList.forEach(element => {
  686. if (element.connectType == 3) {
  687. onLineWifiList.push(element)
  688. } else {
  689. onLineNoWifiList.push(element)
  690. }
  691. });
  692. finalList = finalList.concat(onLineWifiList);
  693. finalList = finalList.concat(onLineNoWifiList);
  694. ///区分离线wifi和蓝牙 wifi在前 离线在后
  695. var onNoLineWifiList = [];
  696. var onNoLineNoWifiList = [];
  697. onNoLineList.forEach(element => {
  698. if (element.connectType == 3) {
  699. onNoLineWifiList.push(element)
  700. } else {
  701. onNoLineNoWifiList.push(element)
  702. }
  703. });
  704. finalList = finalList.concat(onNoLineWifiList);
  705. finalList = finalList.concat(onNoLineNoWifiList);
  706. // 更新缓存
  707. wx.setStorageSync("devicelist", JSON.stringify(finalList));
  708. that.setData({
  709. deviceList: finalList
  710. });
  711. },
  712. ///******************************* 可折叠 ********************************************///
  713. onUserInfoLoad() {
  714. var that = this;
  715. var userInfo = wx.getStorageSync("userInfo") || "";
  716. if (!strings.isEmpty(userInfo)) {
  717. var phone = userInfo.phone;
  718. if (!phone) {
  719. return;
  720. };
  721. var nickName = userInfo.nickname || "";
  722. var greeting = that.getGreetBuNickName(nickName);
  723. // 获取缓存的频道数据
  724. var channelData = wx.getStorageSync("channelData") || "";
  725. if (!strings.isEmpty(channelData)) {
  726. that.setData({
  727. channelData: channelData,
  728. greeting: greeting,
  729. nickName: nickName,
  730. userPic: userInfo.headUrl || "",
  731. isLogin: true,
  732. })
  733. } else {
  734. that.setData({
  735. greeting: greeting,
  736. nickName: nickName,
  737. userPic: userInfo.headUrl || "",
  738. isLogin: true,
  739. });
  740. }
  741. } else {
  742. var greeting = that.getGreetBuNickName("");
  743. that.setData({
  744. greeting: greeting,
  745. })
  746. }
  747. },
  748. getGreetBuNickName(nickName) {
  749. var greeting = timeUtil.getGreet();
  750. if (nickName != "" && nickName != "未登录") {
  751. greeting = greeting + ',' + nickName;;
  752. }
  753. return greeting;
  754. },
  755. onBannerLoad() {
  756. var that = this;
  757. var bannerList = wx.getStorageSync("homeBanner") || [];
  758. if (that.data.bannerList.length == 0) {
  759. that.setData({
  760. bannerList: bannerList
  761. });
  762. }
  763. },
  764. onBannerShow() {
  765. var that = this;
  766. getBanner({}).then((res) => {
  767. that.setData({
  768. bannerList: res
  769. });
  770. wx.setStorageSync("homeBanner", res);
  771. })
  772. },
  773. })
  774. // 去掉此功能,先留着吧
  775. // const other= {
  776. // "url": "",
  777. // "media_data": "",
  778. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  779. // "timestamp": `${Math.round(new Date() / 1000)}`,
  780. // "channel_id": `${res[1].channelNum}`,
  781. // "order": "",
  782. // "resource_from": "",
  783. // "songAlbumID":"",
  784. // "version":3,
  785. // "is_debug": app.globalData.is_debug
  786. // };
  787. // app.PubMsg({
  788. // type: "play",
  789. // DstDeviceName: that.getThisDeviceID(),
  790. // other
  791. // });