index.js 33 KB

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