index.js 26 KB

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