index.js 25 KB

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