index.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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. console.log("gasdfqwwerqewrqwerqr==22222===" + i);
  423. console.log("gasdfqwwerqewrqwerqr==3333===" + JSON.stringify(deviceList[i]));
  424. let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  425. app.subscribe(topic);
  426. }
  427. }
  428. }
  429. },
  430. subscribeCurrDevice() {
  431. var that = this;
  432. if (!(app.globalData.client && app.globalData.client.connected)) {
  433. console.log("未连接MQTT服务器");
  434. const str = setInterval(() => {
  435. clearInterval(str);
  436. that.subscribeCurrDevice();
  437. }, 500);
  438. return;
  439. };
  440. var deviceList = that.data.deviceList;
  441. var deviceListIndex = that.data.deviceListIndex;
  442. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  443. return
  444. };
  445. var tempDevice = that.data.deviceList[deviceListIndex];
  446. var tempDeviceId = tempDevice.deviceId;
  447. ///删除当前选中这个
  448. deviceList.splice(deviceListIndex, 1);
  449. ///添加到第一个去
  450. deviceList.unshift(tempDevice);
  451. that.setData({
  452. deviceListIndex: 0,
  453. autoConnected: true,
  454. deviceList: deviceList,
  455. });
  456. let topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
  457. app.subscribe(topic);
  458. const obj = {
  459. DstDeviceName: that.getThisDeviceID(tempDeviceId)
  460. };
  461. app.PubMsg({
  462. type: "get_dev_info",
  463. ...obj
  464. });
  465. },
  466. actionMusic(e) {
  467. var that = this;
  468. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  469. return;
  470. };
  471. if (that.data.deviceListIndex === null) {
  472. wx.showToast({
  473. title: '请选择设备',
  474. icon: "none"
  475. })
  476. return;
  477. };
  478. that.setData({
  479. actionIndex: e.currentTarget.dataset.index
  480. });
  481. var deviceList = that.data.deviceList;
  482. var deviceListIndex = that.data.deviceListIndex;
  483. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  484. return
  485. };
  486. var deviceId = deviceList[deviceListIndex].deviceId;
  487. const other = {
  488. "url": "",
  489. "media_data": "",
  490. "user_id": `${app.globalData.userInfo.deviceUid}`,
  491. "timestamp": `${Math.round(new Date() / 1000)}`,
  492. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  493. "order": "",
  494. "resource_from": "",
  495. "songAlbumID": "",
  496. "version": 3,
  497. "is_debug": app.globalData.is_debug
  498. };
  499. app.PubMsg({
  500. type: "play",
  501. DstDeviceName: that.getThisDeviceID(deviceId),
  502. other
  503. });
  504. },
  505. getThisDeviceID(deviceId) {
  506. return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
  507. },
  508. getchannelData(clientType) {
  509. var that = this;
  510. var deviceList = that.data.deviceList;
  511. var deviceListIndex = that.data.deviceListIndex;
  512. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  513. return;
  514. };
  515. var deviceId = deviceList[deviceListIndex].deviceId;
  516. listByDevice({
  517. clientType
  518. }).then((res) => {
  519. that.setData({
  520. channelData: res
  521. });
  522. // 接收设备当前播放状态
  523. const obj = {
  524. DstDeviceName: that.getThisDeviceID(deviceId)
  525. }
  526. app.PubMsg({
  527. type: "get_position",
  528. ...obj
  529. });
  530. wx.setStorageSync("channelData", res);
  531. // 有新设备
  532. if (app.globalData.newDeviceId) {
  533. app.globalData.newDeviceId = null;
  534. return;
  535. }
  536. })
  537. },
  538. ///去频道详情
  539. onTapToChannel() {
  540. var that = this;
  541. if (that.data.channelData.length > that.data.actionIndex) {
  542. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  543. wx.navigateTo({
  544. url: './../channelDetails/channelDetails'
  545. });
  546. }
  547. },
  548. onTapIndex(e) {
  549. var that = this;
  550. var index = e.currentTarget.dataset.index;
  551. var indexPage = that.data.indexPage;
  552. if (indexPage != index) {
  553. that.setData({
  554. indexPage: index,
  555. });
  556. }
  557. },
  558. goMeAbout() {
  559. wx.navigateTo({
  560. url: './../about/about',
  561. })
  562. },
  563. onTapLogin() {
  564. var that = this;
  565. ///退出登录
  566. if (that.data.isLogin) {
  567. that.logOut();
  568. }
  569. // 登录
  570. else {
  571. route_util.jump(route_constant.login);
  572. }
  573. },
  574. ///退出登录
  575. logOut() {
  576. var that = this;
  577. ///有设备在线被选中,则让它不被选择
  578. var deviceListIndex = that.data.deviceListIndex;
  579. if (deviceListIndex != null) {
  580. var list = that.data.deviceList;
  581. if (list.length > deviceListIndex) {
  582. if (list[deviceListIndex].connectType == 3) {
  583. if (that.data.thisDeviceMac !== null) {
  584. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  585. that.setData({
  586. actionIndex: null,
  587. deviceListIndex: null,
  588. });
  589. };
  590. }
  591. }
  592. }
  593. wx.removeStorageSync('userInfo');
  594. wx.removeStorageSync('token');
  595. var nickName = "未登录";
  596. var greeting = that.getGreetBuNickName(nickName);
  597. that.setData({
  598. isLogin: false,
  599. greeting: greeting,
  600. nickName: nickName,
  601. userPic: "./../../img/head_pic.png",
  602. });
  603. },
  604. ///添加设备
  605. addDevice() {
  606. var that = this;
  607. if (!that.data.isLogin) {
  608. route_util.jump(route_constant.login);
  609. return;
  610. }
  611. ///跳转设备列表
  612. route_util.jump(route_constant.deviceList);
  613. },
  614. ///点击item
  615. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  616. onTapItem(e) {
  617. var that = this;
  618. if (!that.data.isLogin) {
  619. route_util.jump(route_constant.login);
  620. return;
  621. }
  622. var item = e.currentTarget.dataset.item;
  623. console.log("点击item:", item)
  624. var connectType = item.connectType;
  625. // wifi只支持在线点击
  626. if (connectType == 3 && item.state === "online") {
  627. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  628. that.goWake();
  629. } else {
  630. that.actionDevice(e.currentTarget.dataset.index)
  631. }
  632. return;
  633. }
  634. if (item.connectType != "1") {
  635. return
  636. }
  637. ///去蓝牙连接处理
  638. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  639. // 蓝牙才能连接
  640. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  641. } else if (item.state === "offline") {
  642. console.log("去连接蓝牙")
  643. } else {
  644. ///item
  645. // {"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 妙播收音机"}
  646. item.name = item.devName;
  647. BtHelper.getInstance().connect(item, function (data) {
  648. if (data) {
  649. that.addConnectBlueDevice(item);
  650. }
  651. });
  652. }
  653. },
  654. actionDevice(index) {
  655. var that = this;
  656. if (!that.data.isLogin) {
  657. return;
  658. }
  659. if (that.data.deviceList.length <= index) {
  660. return;
  661. };
  662. const device = that.data.deviceList[index];
  663. if (device.state !== "online") {
  664. return;
  665. };
  666. // 取消订阅
  667. if (that.data.thisDeviceMac !== null) {
  668. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  669. };
  670. that.setData({
  671. deviceListIndex: index,
  672. autoConnected: true,
  673. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
  674. });
  675. // app.PubMsg({
  676. // type: "get_dev_info",
  677. // DstDeviceName: that.getThisDeviceID()
  678. // });
  679. that.subscribeCurrDevice();
  680. },
  681. goWake() {
  682. var that = this;
  683. if (that.data.deviceListIndex === null) {
  684. return;
  685. };
  686. wx.navigateTo({
  687. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  688. });
  689. },
  690. ///点击banner事件
  691. onTapBanner() {
  692. var that = this;
  693. var item = e.currentTarget.dataset.item;
  694. },
  695. ///删除当前设备
  696. deleteDevice(e) {
  697. var that = this;
  698. var index = e.currentTarget.dataset.index;
  699. wx.showModal({
  700. title: '确定删除?',
  701. success: function (res) {
  702. if (res.confirm) {
  703. if (that.data.deviceList[index].connectType == 3) {
  704. that.cancelWifi(index, false);
  705. } else {
  706. that.cancelBlue(index, false);
  707. }
  708. }
  709. }
  710. });
  711. },
  712. ///是否是同一个蓝牙
  713. isTheSameBlue(connectDevice) {
  714. var that = this;
  715. var deviceListIndex = that.data.deviceListIndex;
  716. if (deviceListIndex == null) {
  717. return false;
  718. }
  719. var deviceList = that.data.deviceList;
  720. if (deviceList.length > deviceListIndex) {
  721. if (deviceList[deviceListIndex].connectType == 3) {
  722. return false;
  723. } else {
  724. if (deviceList[deviceListIndex].deviceId == connectDevice.deviceId) {
  725. return true;
  726. } else {
  727. return false;
  728. }
  729. }
  730. } else {
  731. return false;
  732. }
  733. },
  734. ///断开当前的
  735. async cancelCurrent() {
  736. var that = this;
  737. var deviceListIndex = that.data.deviceListIndex;
  738. if (deviceListIndex == null) {
  739. return;
  740. }
  741. var deviceList = that.data.deviceList;
  742. if (deviceList.length > deviceListIndex) {
  743. if (deviceList[deviceListIndex].connectType == 3) {
  744. await that.cancelWifi(deviceListIndex, true);
  745. } else {
  746. await that.cancelBlue(deviceListIndex, true);
  747. }
  748. }
  749. },
  750. /// 断开蓝牙连接
  751. async cancelBlue(index, onlyCancel) {
  752. var that = this;
  753. await BtHelper.getInstance().disconnect();
  754. if (onlyCancel) {
  755. return;
  756. }
  757. const id = that.data.deviceList[index].deviceId;
  758. const deviceList = that.data.deviceList.filter((item, i) => {
  759. return id !== item.deviceId
  760. });
  761. wx.setStorageSync("deviceList", JSON.stringify(deviceList));
  762. that.setData({
  763. deviceList,
  764. });
  765. // 当前没有设备
  766. // if (deviceList.length === 0) {
  767. that.setData({
  768. deviceListIndex: null,
  769. });
  770. // }
  771. },
  772. /// 断开连接wifi
  773. async cancelWifi(index, onlyCancel) {
  774. var that = this;
  775. // 取消订阅
  776. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}/user/pub_response`);
  777. if (onlyCancel) {
  778. return;
  779. }
  780. const id = that.data.deviceList[index].deviceId;
  781. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  782. const deviceList = that.data.deviceList.filter((item, i) => {
  783. return id !== item.deviceId
  784. });
  785. wx.setStorageSync("deviceList", JSON.stringify(deviceList));
  786. that.setData({
  787. deviceList,
  788. thisDeviceMac: null
  789. });
  790. // 当前没有设备
  791. // if (deviceList.length === 0) {
  792. that.setData({
  793. actionIndex: null,
  794. deviceListIndex: null,
  795. });
  796. // }
  797. if (that.data.deviceListIndex === null) {
  798. return
  799. };
  800. if (index === that.data.deviceListIndex) {
  801. let index_ = null;
  802. deviceList.map((v, index) => {
  803. if (v.state === "online" && index_ === null) {
  804. index_ = index;
  805. }
  806. });
  807. if (index_ !== null) {
  808. that.actionDevice(index_);
  809. } else {
  810. that.setData({
  811. actionIndex: null,
  812. deviceListIndex: null,
  813. });
  814. };
  815. } else {
  816. deviceList.map((v, index) => {
  817. if (v.name === name) {
  818. that.setData({
  819. deviceListIndex: index,
  820. autoConnected: true,
  821. });
  822. }
  823. });
  824. }
  825. },
  826. // {"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",
  827. /// deviceList
  828. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  829. /// clientType
  830. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  831. ///新添加蓝牙设备
  832. addConnectBlueDevice(newDevice) {
  833. var that = this;
  834. // 同一个设备
  835. var deviceList = that.data.deviceList;
  836. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  837. if (tempList && tempList.length > 0) {
  838. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  839. };
  840. console.log("添加蓝牙设备:", newDevice);
  841. deviceList.unshift({
  842. /// 蓝牙ble连接
  843. connectType: 1,
  844. deviceId: newDevice.deviceId,
  845. name: newDevice.name,
  846. state: "online",
  847. ProdModel: newDevice.ProdModel,
  848. clientType: newDevice.ProdModel,
  849. devName: newDevice.name,
  850. mac: newDevice.mac,
  851. img: newDevice.img,
  852. });
  853. ///在线排序前面,wifi设备排序前面
  854. that.updateDeviceList(deviceList, false);
  855. var indexPage = that.data.indexPage;
  856. if (indexPage != 0) {
  857. that.setData({
  858. indexPage: 0,
  859. deviceListIndex: 0,
  860. autoConnected: true,
  861. });
  862. } else {
  863. that.setData({
  864. deviceListIndex: 0,
  865. autoConnected: true,
  866. });
  867. }
  868. },
  869. ///新添加wifi设备
  870. async addConnectWifiDevice(newDevice) {
  871. var that = this;
  872. // 同一个设备
  873. var deviceList = that.data.deviceList;
  874. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  875. if (tempList && tempList.length > 0) {
  876. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  877. };
  878. console.log("添加Wifi设备:" + JSON.stringify(newDevice));
  879. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  880. deviceList.unshift({
  881. /// Wifi连接
  882. connectType: 3,
  883. deviceId: newDevice.deviceId,
  884. name: "猫王小王子OTR-X",
  885. state: "online",
  886. ProdModel: 3,
  887. devName: "猫王小王子OTR-X",
  888. mac: newDevice.deviceId,
  889. image: "./../../img/min.png",
  890. });
  891. ///在线排序前面,wifi设备排序前面
  892. that.updateDeviceList(deviceList, false);
  893. var indexPage = that.data.indexPage;
  894. if (indexPage != 0) {
  895. that.setData({
  896. indexPage: 0,
  897. });
  898. }
  899. ///断开蓝牙连接
  900. // await BtHelper.getInstance().disconnect();
  901. that.actionDevice(0);
  902. },
  903. ///更新列表排序
  904. updateDeviceList(deviceList, isInit) {
  905. if (deviceList.length == 0) {
  906. return;
  907. }
  908. var that = this;
  909. var finalList = [];
  910. finalList.push(deviceList[0]);
  911. console.log("online", deviceList[0].deviceId, deviceList[0].state);
  912. var onLineList = [];
  913. var onNoLineList = [];
  914. ///区分在线和离线
  915. for (var i = 0; i < deviceList.length; i++) {
  916. if (isInit) {
  917. deviceList[i].state = "offline";
  918. } else {
  919. if (i > 0) {
  920. if (deviceList[i].state == "online") {
  921. onLineList.push(deviceList[i])
  922. } else {
  923. onNoLineList.push(deviceList[i])
  924. }
  925. }
  926. }
  927. }
  928. // 区分在线wifi和蓝牙 wifi在前 离线在后
  929. var onLineWifiList = [];
  930. var onLineBlueList = [];
  931. onLineList.forEach(element => {
  932. if (element.connectType == 3) {
  933. onLineWifiList.push(element)
  934. } else {
  935. onLineBlueList.push(element)
  936. }
  937. });
  938. finalList = finalList.concat(onLineWifiList);
  939. finalList = finalList.concat(onLineBlueList);
  940. let mDeviceList = []
  941. mDeviceList = mDeviceList.concat(onLineWifiList);
  942. mDeviceList = mDeviceList.concat(onLineBlueList);
  943. getApp().globalData.mDeviceList = mDeviceList;
  944. ///区分离线wifi和蓝牙 wifi在前 离线在后
  945. var onNoLineWifiList = [];
  946. var onNoLineBlueList = [];
  947. onNoLineList.forEach(element => {
  948. if (element.connectType == 3) {
  949. onNoLineWifiList.push(element)
  950. } else {
  951. onNoLineBlueList.push(element)
  952. }
  953. });
  954. finalList = finalList.concat(onNoLineWifiList);
  955. finalList = finalList.concat(onNoLineBlueList);
  956. // 更新缓存
  957. wx.setStorageSync("deviceList", JSON.stringify(finalList));
  958. that.setData({
  959. deviceList: finalList
  960. });
  961. console.log("更新设备列表:", finalList);
  962. getApp().globalData.deviceList = finalList;
  963. },
  964. ///******************************* 可折叠 ********************************************///
  965. onUserInfoLoad() {
  966. var that = this;
  967. var userInfo = wx.getStorageSync("userInfo") || "";
  968. if (!strings.isEmpty(userInfo)) {
  969. var phone = userInfo.phone;
  970. if (!phone) {
  971. return;
  972. };
  973. var nickName = userInfo.nickname || "";
  974. var greeting = that.getGreetBuNickName(nickName);
  975. // 获取缓存的频道数据
  976. var channelData = wx.getStorageSync("channelData") || "";
  977. if (!strings.isEmpty(channelData)) {
  978. that.setData({
  979. channelData: channelData,
  980. greeting: greeting,
  981. nickName: nickName,
  982. userPic: userInfo.headUrl || "",
  983. isLogin: true,
  984. })
  985. } else {
  986. that.setData({
  987. greeting: greeting,
  988. nickName: nickName,
  989. userPic: userInfo.headUrl || "",
  990. isLogin: true,
  991. });
  992. }
  993. } else {
  994. var greeting = that.getGreetBuNickName("");
  995. that.setData({
  996. greeting: greeting,
  997. })
  998. }
  999. },
  1000. getGreetBuNickName(nickName) {
  1001. var greeting = timeUtil.getGreet();
  1002. if (nickName != "" && nickName != "未登录") {
  1003. greeting = greeting + ',' + nickName;;
  1004. }
  1005. return greeting;
  1006. },
  1007. onBannerLoad() {
  1008. var that = this;
  1009. var bannerList = wx.getStorageSync("homeBanner") || [];
  1010. if (that.data.bannerList.length == 0) {
  1011. that.setData({
  1012. bannerList: bannerList
  1013. });
  1014. }
  1015. },
  1016. onBannerShow() {
  1017. var that = this;
  1018. getBanner({}).then((res) => {
  1019. that.setData({
  1020. bannerList: res
  1021. });
  1022. wx.setStorageSync("homeBanner", res);
  1023. })
  1024. },
  1025. ///销毁蓝牙
  1026. onUnload() {
  1027. var that = this;
  1028. if (!strings.isEmpty(that.data.intervalId)) {
  1029. clearInterval(that.data.intervalId);
  1030. that.data.intervalId = null;
  1031. }
  1032. BtHelper.getInstance().disconnect();
  1033. if (app.globalData.client === null) {
  1034. return;
  1035. };
  1036. app.globalData.client.end(true);
  1037. app.globalData.client.end(true);
  1038. app.globalData.client = null;
  1039. },
  1040. })
  1041. // 去掉此功能,先留着吧
  1042. // const other= {
  1043. // "url": "",
  1044. // "media_data": "",
  1045. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  1046. // "timestamp": `${Math.round(new Date() / 1000)}`,
  1047. // "channel_id": `${res[1].channelNum}`,
  1048. // "order": "",
  1049. // "resource_from": "",
  1050. // "songAlbumID":"",
  1051. // "version":3,
  1052. // "is_debug": app.globalData.is_debug
  1053. // };
  1054. // app.PubMsg({
  1055. // type: "play",
  1056. // DstDeviceName: that.getThisDeviceID(),
  1057. // other
  1058. // });