index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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_constant from '../../utils/route_constant.js';
  9. import route_util from '../../utils/route_util.js';
  10. Page({
  11. data: {
  12. nvabarData: {
  13. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  14. title: 'OhPlay', //导航栏 中间的标题
  15. },
  16. ///下午好
  17. isLogin: false,
  18. greeting: "",
  19. bannerList: [],
  20. autoplay: true,
  21. interval: 3000, // 切换时间间隔
  22. duration: 500, // 滑动动画时长
  23. circular: true, // 衔接滑动
  24. indexPage: 0,
  25. ///是否展示频道
  26. showChannel: false,
  27. ////首页
  28. navBarHeight: app.globalData.navBarHeight,
  29. MenuButtonheight: app.globalData.MenuButtonheight,
  30. MenuButtonTop: app.globalData.MenuButtonTop,
  31. actionIndex: null,
  32. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  33. channelData: [],
  34. deviceList: [],
  35. deviceListIndex: null,
  36. isOneLoading: true,
  37. uid: null,
  38. isSetWake: false,
  39. thisDeviceMac: null,
  40. battery: 4, // 0≤电量<20,0格
  41. ////我的界面
  42. loginStatus: true,
  43. nickName: "未登录",
  44. userPic: './../../img/head_pic.png'
  45. },
  46. onLoad(options) {
  47. var that = this;
  48. that.onDeviceLoad();
  49. that.onUserInfoLoad();
  50. that.onBannerLoad();
  51. },
  52. onShow() {
  53. var that = this;
  54. that.onHomeShow();
  55. that.onBannerShow();
  56. },
  57. onHomeShow() {
  58. var that = this;
  59. var userInfo = wx.getStorageSync("userInfo") || "";
  60. if (!strings.isEmpty(userInfo)) {
  61. // 获取设备本地数据
  62. if (app.globalData.newDeviceId) {
  63. that.addNewDeviceId();
  64. } else if (app.globalData.client == null) {
  65. app.connect();
  66. } else if (that.data.deviceListIndex == null && that.data.deviceList.length > 0) {
  67. that.actionDevice(0)
  68. }
  69. } else {
  70. if (app.globalData.client == null) {
  71. app.connect();
  72. }
  73. }
  74. },
  75. onDeviceLoad() {
  76. var that = this;
  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. ///获取频道数据
  141. ///锁定播放哪一个频道
  142. if (payloads.type === "get_position" && payloads.other) {
  143. let actionIndex = null;
  144. that.data.channelData.map((v, index) => {
  145. if (v.channelNum === payloads.other.channel) {
  146. actionIndex = index;
  147. }
  148. });
  149. that.setData({
  150. actionIndex,
  151. });
  152. }
  153. ///获取播放状态
  154. else if (payloads.type === "play" || payloads.type === "play_state") {
  155. var deviceList = that.data.deviceList;
  156. var deviceListIndex = that.data.deviceListIndex;
  157. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  158. return
  159. };
  160. var deviceId = deviceList[deviceListIndex].deviceId;
  161. // 接收设备当前播放状态
  162. const obj = {
  163. DstDeviceName: that.getThisDeviceID(deviceId)
  164. }
  165. app.PubMsg({
  166. type: "get_position",
  167. ...obj
  168. });
  169. }
  170. ///获取频道列表数据
  171. else if (payloads.type === "get_dev_info") {
  172. // 接收设备当前信息
  173. that.getchannelData(payloads.other.ProdModel);
  174. // 电量
  175. that.setData({
  176. battery: that._battery(payloads.other.Power),
  177. })
  178. // 当前设备木有设置定时
  179. that.setData({
  180. isSetWake: false,
  181. });
  182. payloads.other.alarm.map((v) => {
  183. if (v.enable === "1") {
  184. that.setData({
  185. isSetWake: true
  186. });
  187. }
  188. });
  189. // 更新信息
  190. ///连上就调用2次
  191. var isUpdate = false;
  192. // payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  193. var deviceList = that.data.deviceList;
  194. if (!strings.isEmpty(deviceList)) {
  195. for (var i = 0; i < deviceList.length; i++) {
  196. if (payloads.SrcDeviceName) {
  197. var deviceId = deviceList[i].deviceId;
  198. var splitDeviceId = deviceId.split("BLUFI_");
  199. if (splitDeviceId.length == 2) {
  200. var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
  201. if (index !== -1 && deviceList[i].ProdModel != payloads.other.ProdModel && deviceList[i].devName != payloads.other.devName) {
  202. isUpdate = true;
  203. deviceList[i].ProdModel = payloads.other.ProdModel;
  204. deviceList[i].devName = payloads.other.devName;
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. ///数据有更新
  212. if (isUpdate) {
  213. that.updateDeviceList(deviceList, false);
  214. }
  215. } else if (payloads.type === "battery" && payloads.other) {
  216. that.setData({
  217. battery: that._battery(payloads.other.battery),
  218. })
  219. }
  220. break;
  221. default:
  222. }
  223. },
  224. // 格式化电量
  225. _battery(battery) {
  226. let _battery = 0;
  227. if (battery < 20) {
  228. _battery = 0
  229. } else if (20 <= battery && battery < 40) {
  230. _battery = 1
  231. } else if (40 <= battery && battery < 60) {
  232. _battery = 2
  233. } else if (60 <= battery && battery < 80) {
  234. _battery = 3
  235. } else if (80 <= battery && battery <= 100) {
  236. _battery = 4
  237. } else if (battery > 100) {
  238. _battery = 5
  239. };
  240. return _battery
  241. },
  242. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  243. ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
  244. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  245. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  246. onlineDevice(payloads) {
  247. // 设置在线状态
  248. var that = this;
  249. console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  250. ///是否更新过在线离线状态
  251. var isUpdate = false;
  252. var deviceList = that.data.deviceList;
  253. if (!strings.isEmpty(deviceList)) {
  254. for (var i = 0; i < deviceList.length; i++) {
  255. if (payloads.uuid) {
  256. var deviceId = deviceList[i].deviceId;
  257. var splitDeviceId = deviceId.split("BLUFI_");
  258. if (splitDeviceId.length == 2) {
  259. var index = payloads.uuid.indexOf(splitDeviceId[1]);
  260. if (index !== -1) {
  261. if (deviceList[i].state != payloads.state) {
  262. isUpdate = true;
  263. deviceList[i].state = payloads.state;
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. ///数据有更新
  272. if (isUpdate) {
  273. that.updateDeviceList(deviceList, false);
  274. }
  275. ///当前没有连接设备,则去连接第一个wifi设备
  276. var deviceListIndex = that.data.deviceListIndex;
  277. if (deviceListIndex === null) {
  278. var list = that.data.deviceList;
  279. for (var i = 0; i < list.length; i++) {
  280. if (list[i].state === "online" && list[i].connectType == 3) {
  281. that.actionDevice(i);
  282. break;
  283. }
  284. }
  285. } else {
  286. // 当前播放设备离线
  287. if (that.data.deviceList.length > deviceListIndex && that.data.deviceList[deviceListIndex].state !== "online") {
  288. that.setData({
  289. actionIndex: null,
  290. deviceListIndex: null,
  291. });
  292. };
  293. }
  294. },
  295. // 订阅设备在线状态
  296. subscribeDevicesStatus() {
  297. var that = this;
  298. var deviceList = that.data.deviceList;
  299. if (!strings.isEmpty(deviceList)) {
  300. for (var i = 0; i < deviceList.length; i++) {
  301. if (deviceList[i].connectType == 3) {
  302. let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  303. app.subscribe(topic);
  304. }
  305. }
  306. }
  307. },
  308. subscribeCurrDevice() {
  309. var that = this;
  310. if (!(app.globalData.client && app.globalData.client.connected)) {
  311. console.log("未连接MQTT服务器");
  312. const str = setInterval(() => {
  313. clearInterval(str);
  314. that.subscribeCurrDevice();
  315. }, 500);
  316. return;
  317. };
  318. var deviceList = that.data.deviceList;
  319. var deviceListIndex = that.data.deviceListIndex;
  320. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  321. return
  322. };
  323. var tempDevice = that.data.deviceList[deviceListIndex];
  324. var tempDeviceId = tempDevice.deviceId;
  325. ///删除当前选中这个
  326. deviceList.splice(deviceListIndex, 1);
  327. ///添加到第一个去
  328. deviceList.unshift(tempDevice);
  329. that.setData({
  330. deviceListIndex: 0,
  331. deviceList: deviceList,
  332. });
  333. let topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
  334. app.subscribe(topic);
  335. const obj = {
  336. DstDeviceName: that.getThisDeviceID(tempDeviceId)
  337. };
  338. app.PubMsg({
  339. type: "get_dev_info",
  340. ...obj
  341. });
  342. },
  343. actionMusic(e) {
  344. var that = this;
  345. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  346. return;
  347. };
  348. if (that.data.deviceListIndex === null) {
  349. wx.showToast({
  350. title: '请选择设备',
  351. icon: "none"
  352. })
  353. return;
  354. };
  355. that.setData({
  356. actionIndex: e.currentTarget.dataset.index
  357. });
  358. var deviceList = that.data.deviceList;
  359. var deviceListIndex = that.data.deviceListIndex;
  360. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  361. return
  362. };
  363. var deviceId = deviceList[deviceListIndex].deviceId;
  364. const other = {
  365. "url": "",
  366. "media_data": "",
  367. "user_id": `${app.globalData.userInfo.deviceUid}`,
  368. "timestamp": `${Math.round(new Date() / 1000)}`,
  369. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  370. "order": "",
  371. "resource_from": "",
  372. "songAlbumID": "",
  373. "version": 3,
  374. "is_debug": app.globalData.is_debug
  375. };
  376. app.PubMsg({
  377. type: "play",
  378. DstDeviceName: that.getThisDeviceID(deviceId),
  379. other
  380. });
  381. },
  382. getThisDeviceID(deviceId) {
  383. return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
  384. },
  385. getchannelData(clientType) {
  386. var that = this;
  387. var deviceList = that.data.deviceList;
  388. var deviceListIndex = that.data.deviceListIndex;
  389. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  390. return;
  391. };
  392. var deviceId = deviceList[deviceListIndex].deviceId;
  393. listByDevice({
  394. clientType
  395. }).then((res) => {
  396. that.setData({
  397. channelData: res
  398. });
  399. // 接收设备当前播放状态
  400. const obj = {
  401. DstDeviceName: that.getThisDeviceID(deviceId)
  402. }
  403. app.PubMsg({
  404. type: "get_position",
  405. ...obj
  406. });
  407. wx.setStorageSync("channelData", res);
  408. // 有新设备
  409. if (app.globalData.newDeviceId) {
  410. app.globalData.newDeviceId = null;
  411. return;
  412. }
  413. })
  414. },
  415. ///去频道详情
  416. onTapToChannel() {
  417. var that = this;
  418. if (that.data.channelData.length > that.data.actionIndex) {
  419. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  420. wx.navigateTo({
  421. url: './../channelDetails/channelDetails'
  422. });
  423. }
  424. },
  425. onUnload() {
  426. if (app.globalData.client === null) {
  427. return;
  428. };
  429. app.globalData.client.end(true);
  430. app.globalData.client.end(true);
  431. app.globalData.client = null;
  432. },
  433. onTapIndex(e) {
  434. var that = this;
  435. var index = e.currentTarget.dataset.index;
  436. var indexPage = that.data.indexPage;
  437. if (indexPage != index) {
  438. that.setData({
  439. indexPage: index,
  440. });
  441. }
  442. },
  443. goMeAbout() {
  444. wx.navigateTo({
  445. url: './../about/about',
  446. })
  447. },
  448. onTapLogin() {
  449. var that = this;
  450. ///退出登录
  451. if (that.data.isLogin) {
  452. that.logOut();
  453. }
  454. // 登录
  455. else {
  456. route_util.jump(route_constant.login);
  457. }
  458. },
  459. ///退出登录
  460. logOut() {
  461. var that = this;
  462. ///有设备在线被选中,则让它不被选择
  463. var deviceListIndex = that.data.deviceListIndex;
  464. if (deviceListIndex != null) {
  465. var list = that.data.deviceList;
  466. if (list.length > deviceListIndex) {
  467. if (list[deviceListIndex].connectType == 3) {
  468. if (that.data.thisDeviceMac !== null) {
  469. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  470. that.setData({
  471. actionIndex: null,
  472. deviceListIndex: null,
  473. });
  474. };
  475. }
  476. }
  477. }
  478. wx.removeStorageSync('userInfo');
  479. wx.removeStorageSync('token');
  480. var nickName = "未登录";
  481. var greeting = that.getGreetBuNickName(nickName);
  482. that.setData({
  483. isLogin: false,
  484. greeting: greeting,
  485. nickName: nickName,
  486. userPic: "./../../img/head_pic.png",
  487. });
  488. },
  489. ///添加设备
  490. addDevice() {
  491. var that = this;
  492. if (!that.data.isLogin) {
  493. route_util.jump(route_constant.login);
  494. return;
  495. }
  496. ///跳转设备列表
  497. route_util.jump(route_constant.deviceList);
  498. },
  499. ///点击item
  500. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  501. onTapItem(e) {
  502. var that = this;
  503. if (!that.data.isLogin) {
  504. route_util.jump(route_constant.login);
  505. return;
  506. }
  507. var item = e.currentTarget.dataset.item;
  508. var connectType = item.connectType;
  509. if (connectType == 3) {
  510. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  511. that.goWake();
  512. } else {
  513. that.actionDevice(e.currentTarget.dataset.index)
  514. }
  515. return;
  516. }
  517. console.log(item)
  518. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  519. return
  520. ///去蓝牙连接处理
  521. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  522. console.log(item)
  523. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  524. } else {
  525. that.addConnectBlueDevice({
  526. deviceId: item.deviceId,
  527. name: item.devName,
  528. state: item.state,
  529. clientType: item.ProdModel,
  530. mac: item.mac,
  531. });
  532. }
  533. },
  534. actionDevice(index) {
  535. var that = this;
  536. if (!that.data.isLogin) {
  537. return;
  538. }
  539. if (that.data.deviceList.length <= index) {
  540. return;
  541. };
  542. const device = that.data.deviceList[index];
  543. if (device.state !== "online") {
  544. return;
  545. };
  546. // 取消订阅
  547. if (that.data.thisDeviceMac !== null) {
  548. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  549. };
  550. that.setData({
  551. deviceListIndex: index,
  552. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
  553. });
  554. // app.PubMsg({
  555. // type: "get_dev_info",
  556. // DstDeviceName: that.getThisDeviceID()
  557. // });
  558. that.subscribeCurrDevice();
  559. },
  560. goWake() {
  561. var that = this;
  562. if (that.data.deviceListIndex === null) {
  563. return;
  564. };
  565. wx.navigateTo({
  566. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  567. });
  568. },
  569. ///点击banner事件
  570. onTapBanner() {
  571. var that = this;
  572. var item = e.currentTarget.dataset.item;
  573. },
  574. ///删除当前设备
  575. deleteDevice(e) {
  576. var that = this;
  577. wx.showModal({
  578. title: '确定删除?',
  579. success: function (res) {
  580. if (res.confirm) {
  581. if (that.data.deviceList[e.currentTarget.dataset.index].connectType != 3) {
  582. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  583. const deviceList = that.data.deviceList.filter((item, i) => {
  584. return id !== item.deviceId
  585. });
  586. wx.setStorageSync("devicelist", JSON.stringify(deviceList));
  587. that.setData({
  588. deviceList,
  589. });
  590. // 当前没有设备
  591. if (deviceList.length === 0) {
  592. that.setData({
  593. deviceListIndex: null,
  594. });
  595. }
  596. return;
  597. }
  598. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  599. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  600. const deviceList = that.data.deviceList.filter((item, i) => {
  601. return id !== item.deviceId
  602. });
  603. // 取消订阅
  604. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  605. wx.setStorageSync("devicelist", JSON.stringify(deviceList));
  606. that.setData({
  607. deviceList,
  608. thisDeviceMac: null
  609. });
  610. // 当前没有设备
  611. if (deviceList.length === 0) {
  612. that.setData({
  613. actionIndex: null,
  614. deviceListIndex: null,
  615. });
  616. }
  617. if (that.data.deviceListIndex === null) {
  618. return
  619. };
  620. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  621. let index_ = null;
  622. deviceList.map((v, index) => {
  623. if (v.state === "online" && index_ === null) {
  624. index_ = index;
  625. }
  626. });
  627. if (index_ !== null) {
  628. that.actionDevice(index_);
  629. } else {
  630. that.setData({
  631. actionIndex: null,
  632. deviceListIndex: null,
  633. });
  634. };
  635. } else {
  636. deviceList.map((v, index) => {
  637. if (v.name === name) {
  638. that.setData({
  639. deviceListIndex: index,
  640. });
  641. }
  642. });
  643. }
  644. }
  645. }
  646. });
  647. },
  648. // {"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",
  649. /// deviceList
  650. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  651. /// clientType
  652. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  653. ///新添加蓝牙设备
  654. addConnectBlueDevice(newDevice) {
  655. var that = this;
  656. // 同一个设备
  657. var deviceList = that.data.deviceList;
  658. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  659. if (tempList && tempList.length > 0) {
  660. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  661. };
  662. console.log("添加蓝牙设备:", newDevice);
  663. deviceList.unshift({
  664. /// 蓝牙ble连接
  665. connectType: 1,
  666. deviceId: newDevice.deviceId,
  667. name: newDevice.deviceId,
  668. state: "online",
  669. ProdModel: newDevice.clientType,
  670. devName: newDevice.name,
  671. mac: newDevice.mac,
  672. });
  673. ///在线排序前面,wifi设备排序前面
  674. that.updateDeviceList(deviceList, false);
  675. var indexPage = that.data.indexPage;
  676. if (indexPage != 0) {
  677. that.setData({
  678. indexPage: 0,
  679. deviceListIndex: 0,
  680. });
  681. } else {
  682. that.setData({
  683. deviceListIndex: 0,
  684. });
  685. }
  686. },
  687. ///新添加wifi设备
  688. addConnectWifiDevice(deviceList) {
  689. var that = this;
  690. ///在线排序前面,wifi设备排序前面
  691. that.updateDeviceList(deviceList, false);
  692. var indexPage = that.data.indexPage;
  693. if (indexPage != 0) {
  694. that.setData({
  695. indexPage: 0,
  696. deviceListIndex: 0,
  697. });
  698. } else {
  699. that.setData({
  700. deviceListIndex: 0,
  701. });
  702. }
  703. },
  704. ///更新列表排序
  705. updateDeviceList(deviceList, isInit) {
  706. if (deviceList.length == 0) return [];
  707. var that = this;
  708. var finalList = [];
  709. finalList.push(deviceList[0]);
  710. var onLineList = [];
  711. var onNoLineList = [];
  712. ///区分在线和离线
  713. for (var i = 0; i < deviceList.length; i++) {
  714. if (isInit) {
  715. deviceList[i].state = "offline";
  716. } else {
  717. if (i > 0) {
  718. if (deviceList[i].state == "online") {
  719. onLineList.push(deviceList[i])
  720. } else {
  721. onNoLineList.push(deviceList[i])
  722. }
  723. }
  724. }
  725. }
  726. // 区分在线wifi和蓝牙 wifi在前 离线在后
  727. var onLineWifiList = [];
  728. var onLineNoWifiList = [];
  729. onLineList.forEach(element => {
  730. if (element.connectType == 3) {
  731. onLineWifiList.push(element)
  732. } else {
  733. onLineNoWifiList.push(element)
  734. }
  735. });
  736. finalList = finalList.concat(onLineWifiList);
  737. finalList = finalList.concat(onLineNoWifiList);
  738. ///区分离线wifi和蓝牙 wifi在前 离线在后
  739. var onNoLineWifiList = [];
  740. var onNoLineNoWifiList = [];
  741. onNoLineList.forEach(element => {
  742. if (element.connectType == 3) {
  743. onNoLineWifiList.push(element)
  744. } else {
  745. onNoLineNoWifiList.push(element)
  746. }
  747. });
  748. finalList = finalList.concat(onNoLineWifiList);
  749. finalList = finalList.concat(onNoLineNoWifiList);
  750. // 更新缓存
  751. wx.setStorageSync("devicelist", JSON.stringify(finalList));
  752. finalList.forEach(element => {
  753. if (element.connectType == 1) {
  754. element.state = "online"
  755. }
  756. });
  757. that.setData({
  758. deviceList: finalList
  759. });
  760. },
  761. ///******************************* 可折叠 ********************************************///
  762. onUserInfoLoad() {
  763. var that = this;
  764. var userInfo = wx.getStorageSync("userInfo") || "";
  765. if (!strings.isEmpty(userInfo)) {
  766. var phone = userInfo.phone;
  767. if (!phone) {
  768. return;
  769. };
  770. var nickName = userInfo.nickname || "";
  771. var greeting = that.getGreetBuNickName(nickName);
  772. // 获取缓存的频道数据
  773. var channelData = wx.getStorageSync("channelData") || "";
  774. if (!strings.isEmpty(channelData)) {
  775. that.setData({
  776. channelData: channelData,
  777. greeting: greeting,
  778. nickName: nickName,
  779. userPic: userInfo.headUrl || "",
  780. isLogin: true,
  781. })
  782. } else {
  783. that.setData({
  784. greeting: greeting,
  785. nickName: nickName,
  786. userPic: userInfo.headUrl || "",
  787. isLogin: true,
  788. });
  789. }
  790. } else {
  791. var greeting = that.getGreetBuNickName("");
  792. that.setData({
  793. greeting: greeting,
  794. })
  795. }
  796. },
  797. getGreetBuNickName(nickName) {
  798. var greeting = timeUtil.getGreet();
  799. if (nickName != "" && nickName != "未登录") {
  800. greeting = greeting + ',' + nickName;;
  801. }
  802. return greeting;
  803. },
  804. onBannerLoad() {
  805. var that = this;
  806. var bannerList = wx.getStorageSync("homeBanner") || [];
  807. if (that.data.bannerList.length == 0) {
  808. that.setData({
  809. bannerList: bannerList
  810. });
  811. }
  812. },
  813. onBannerShow() {
  814. var that = this;
  815. getBanner({}).then((res) => {
  816. that.setData({
  817. bannerList: res
  818. });
  819. wx.setStorageSync("homeBanner", res);
  820. })
  821. },
  822. })
  823. // 去掉此功能,先留着吧
  824. // const other= {
  825. // "url": "",
  826. // "media_data": "",
  827. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  828. // "timestamp": `${Math.round(new Date() / 1000)}`,
  829. // "channel_id": `${res[1].channelNum}`,
  830. // "order": "",
  831. // "resource_from": "",
  832. // "songAlbumID":"",
  833. // "version":3,
  834. // "is_debug": app.globalData.is_debug
  835. // };
  836. // app.PubMsg({
  837. // type: "play",
  838. // DstDeviceName: that.getThisDeviceID(),
  839. // other
  840. // });