index.js 29 KB

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