index.js 26 KB

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