index.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. const app = getApp();
  2. const {
  3. login,
  4. listByDevice
  5. } = require('../../../utils/api.js');
  6. import timeUtil from '../../../utils/time_util.js';
  7. import strings from '../../../utils/strings.js';
  8. import store from '../../../utils/store.js';
  9. import route_constant from '../../../utils/route_constant.js';
  10. import route_util from '../../../utils/route_util.js';
  11. /// 乐鑫相关
  12. import lexin_util from '../../../utils/lexin/util.js';
  13. import lexin_jump from '../../../utils/lexin/jump.js';
  14. import lexin_add from '../../../utils/lexin/add.js';
  15. import lexin_scan from '../../../utils/lexin/scan.js';
  16. import lexin_subscribe from '../../../utils/lexin/subscribe.js';
  17. import lexin_connect from '../../../utils/lexin/connect.js';
  18. import lexin_message from '../../../utils/lexin/message.js';
  19. import {
  20. BtHelper
  21. } from '../../../devices/bt_helper.js';
  22. import event_bus from '../../../utils/event_bus.js';
  23. import {
  24. EnumCmdEvent,
  25. CmdEvent
  26. } from '../../../devices/cmd_key_event';
  27. Page({
  28. data: {
  29. navBarHeight: app.globalData.navBarHeight,
  30. safeBottomHeight: app.globalData.safeBottomHeight,
  31. nvabarData: {
  32. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  33. title: 'OhPlay', //导航栏 中间的标题
  34. },
  35. ///下午好
  36. isLogin: false,
  37. greeting: "",
  38. bannerList: [{
  39. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  40. },
  41. {
  42. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  43. }
  44. ],
  45. autoplay: true,
  46. interval: 3000, // 切换时间间隔
  47. duration: 500, // 滑动动画时长
  48. circular: true, // 衔接滑动
  49. indexPage: 0,
  50. ///是否展示频道
  51. showChannel: false,
  52. ////首页
  53. navBarHeight: app.globalData.navBarHeight,
  54. MenuButtonHeight: app.globalData.MenuButtonHeight,
  55. MenuButtonTop: app.globalData.MenuButtonTop,
  56. actionIndex: null,
  57. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  58. channelData: [],
  59. isOneLoading: true,
  60. uid: null,
  61. isSetWake: false,
  62. battery: 4, // 0≤电量<20,0格
  63. ////我的界面
  64. loginStatus: true,
  65. nickName: "未登录",
  66. userPic: '../../../img/head_pic.png',
  67. intervalId: null,
  68. /// 是否第一次自动连接过
  69. autoConnected: false,
  70. intervalId1: null,
  71. intervalId2: null,
  72. deviceMacId: null,
  73. deviceListSelect: null,
  74. deviceList: [],
  75. _willConnectBle: true,
  76. connectDeviceIding: "",
  77. },
  78. onLoad(options) {
  79. var that = this;
  80. that.onLoginLoad();
  81. that.onDeviceLoad();
  82. that.onUserInfoLoad();
  83. that.onBannerLoad();
  84. },
  85. onShow() {
  86. var that = this;
  87. that.onBannerShow();
  88. },
  89. onLoginLoad() {
  90. var that = this;
  91. var userInfo = wx.getStorageSync("userInfo") || "";
  92. if (!strings.isEmpty(userInfo)) {
  93. var phone = userInfo.phone;
  94. if (!strings.isEmpty(phone)) {
  95. that.loginReset(phone, null, null, null);
  96. };
  97. }
  98. },
  99. ///重新登录
  100. loginReset(phone, succeedCall1, succeedCall2, failCall) {
  101. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
  102. var that = this;
  103. wx.login({
  104. success: res0 => {
  105. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","unionid":null,"userId":933625,"deviceUid":1,"accessToken":"kaKdNAb9eO0iUGKsaLP18UE0wRwrvoCr4opjNC3AieaigH4Fu1IspQKi2zTPgLNG","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥","isNewUser":false}
  106. login({
  107. code: res0.code,
  108. phone: phone,
  109. }).then((res1) => {
  110. app.globalData.userInfo = res1;
  111. store.setStore("userInfo", res1);
  112. store.setStore("token", JSON.stringify({
  113. id: res1.userId,
  114. token: res1.accessToken,
  115. }));
  116. that.onUserInfoLoad();
  117. if (succeedCall1 != null) {
  118. succeedCall1();
  119. }
  120. if (succeedCall2 != null) {
  121. succeedCall2();
  122. }
  123. });
  124. },
  125. fail(fail4) {
  126. if (failCall != null) {
  127. failCall();
  128. }
  129. },
  130. });
  131. },
  132. testData() {
  133. return {
  134. "deviceId": "xxxxx",
  135. "deviceName": "xxxxxx",
  136. "deviceType": "xxxx",
  137. "type": 1,
  138. "deviceIcon": "xxxx",
  139. "deviceMacId": "xxxx",
  140. "deviceBattery": 4,
  141. "state": "online",
  142. "connectType": 1,
  143. "clientType": "猫王妙播黑胶音箱(BLE)",
  144. "deviceConnectStatus": 1,
  145. "deviceConnectStatusText": "xxxx",
  146. }
  147. },
  148. onDeviceLoad() {
  149. var that = this;
  150. var deviceList = store.getStore("deviceList");
  151. if (!strings.isEmpty(deviceList)) {
  152. // deviceList = deviceList.concat(deviceList);
  153. that.updateDeviceList(deviceList, true, false);
  154. }
  155. // that.addBlueDevice(that.testData())
  156. let hasBle = that.data.deviceList.find(item => {
  157. return item.connectType == 1;
  158. })
  159. if (hasBle) {
  160. ///监听蓝牙设备
  161. BtHelper.getInstance().initBluetoothAdapter();
  162. BtHelper.getInstance().getBluetoothDevices();
  163. var isFirst = true;
  164. console.log("开始监听蓝牙设备");
  165. ///再秒再对比一次
  166. that.stopIntervalId1();
  167. let count = 0;
  168. that.data.intervalId1 = setInterval(async function () {
  169. isFirst = false;
  170. that.compareList();
  171. if (count > 3) {
  172. that.stopIntervalId1();
  173. } else {
  174. count++;
  175. }
  176. }, isFirst ? 6 * 1000 : 12 * 1000);
  177. }
  178. },
  179. /// 对比蓝牙数据
  180. compareList() {
  181. var that = this;
  182. if (that.data.deviceList.length == 1) {
  183. let device = that.data.deviceList[0];
  184. if (device.state === 'online') {
  185. return;
  186. }
  187. getApp().getBluetoothStatus();
  188. that.data._willConnectBle = true;
  189. return;
  190. }
  191. lexin_scan.compareList(function (list) {
  192. that.updateDeviceList(list, false, false);
  193. }, function (item) {
  194. that.addBlueDevice(item);
  195. });
  196. },
  197. getBluetoothStatusCallck(v) {
  198. var that = this;
  199. if (!that.data._willConnectBle) {
  200. // true 走下面
  201. return;
  202. }
  203. if (v) {
  204. var device = that.data.deviceList[0];
  205. console.log("开始连接设备0", device);
  206. if (device.state === 'online' || device.connectType != 1) {
  207. return;
  208. }
  209. // });
  210. BtHelper.getInstance().connect(device, function (isConnected, d) {
  211. if (isConnected) {
  212. device.state = 'online';
  213. that.addBlueDevice(device);
  214. } else {
  215. device.state = 'offline';
  216. }
  217. });
  218. } else {
  219. // wx.showModal({
  220. // title: '温馨提示',
  221. // content: '请检查手机蓝牙是否打开',
  222. // showCancel: false,
  223. // success: function (res) { }
  224. // });
  225. }
  226. that.data._willConnectBle = false;
  227. },
  228. // 回调
  229. mqttCallback(type, option) {
  230. // console.log("gadsfadsfadsfa==888===" + type);
  231. var that = this;
  232. let payloads = null;
  233. if (option && option.payload) {
  234. payloads = JSON.parse(option.payload);
  235. };
  236. switch (type) {
  237. ///先走订阅
  238. case "connect":
  239. lexin_subscribe.subscribeAllDevice();
  240. break;
  241. ///再走有在线
  242. case "message_onoffline":
  243. lexin_connect.searchOnlineDevice(payloads, function (list) {
  244. that.updateDeviceList(list, false, false);
  245. }, function () {
  246. var indexPage = that.data.indexPage;
  247. that.setData({
  248. actionIndex: null,
  249. deviceListSelect: null,
  250. indexPage: indexPage == 1 ? 0 : indexPage,
  251. });
  252. }, function () {
  253. var indexPage = that.data.indexPage;
  254. that.setData({
  255. actionIndex: null,
  256. deviceListSelect: null,
  257. indexPage: indexPage == 1 ? 0 : indexPage,
  258. });
  259. });
  260. break;
  261. case "message":
  262. if (payloads) {
  263. var payloadType = payloads.type;
  264. var other = payloads.other;
  265. ///获取频道位置
  266. if (payloadType === "get_position" && other) {
  267. lexin_message.getPosition(other, function (actionIndex) {
  268. that.setData({
  269. actionIndex: actionIndex,
  270. });
  271. });
  272. }
  273. // 获取歌曲信息
  274. else if (payloadType === "get_dev_info" && other) {
  275. that.setDeviceInfor(other);
  276. lexin_message.getDeviceInfo(payloads);
  277. }
  278. ///获取电量
  279. else if (payloadType === "battery" && other) {
  280. if (other.battery) {
  281. var battery = lexin_message.getBattery(other.battery);
  282. that.setData({
  283. battery: battery,
  284. });
  285. }
  286. }
  287. ///获取播放状态
  288. else if (payloadType === "play" || payloadType === "play_state") {
  289. lexin_message.getPlay();
  290. }
  291. }
  292. break;
  293. default:
  294. }
  295. },
  296. ///去连接设备数据
  297. actionDevice(device) {
  298. var that = this;
  299. // 取消订阅
  300. var deviceMacId = that.data.deviceMacId;
  301. if (!strings.isEmpty(deviceMacId)) {
  302. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  303. app.unsubscribe(pubResponse);
  304. };
  305. that.subscribeCurrDevice(device);
  306. },
  307. ///数据处理
  308. subscribeCurrDevice(device) {
  309. var that = this;
  310. var isOK = app.globalData.client && app.globalData.client.connected;
  311. if (!isOK) {
  312. app.connect();
  313. setTimeout(() => {
  314. that.subscribeCurrDevice(device);
  315. }, 500);
  316. return;
  317. };
  318. var deviceId = device.deviceId;
  319. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  320. that.setData({
  321. deviceMacId: deviceMacId,
  322. });
  323. /// 订阅设备
  324. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  325. app.subscribe(pubResponse);
  326. /// 获取设备信息
  327. const obj = {
  328. DstDeviceName: deviceMacId
  329. };
  330. app.PubMsg({
  331. type: "get_dev_info",
  332. ...obj
  333. });
  334. },
  335. ///选择对应的频道
  336. onTapChannelIndex(e) {
  337. var that = this;
  338. var index = e.currentTarget.dataset.index;
  339. lexin_jump.toChannelIndex(index, function (i) {
  340. that.setData({
  341. actionIndex: i,
  342. });
  343. });
  344. },
  345. ///去频道详情
  346. onTapToChannel() {
  347. var that = this;
  348. var actionIndex = that.data.actionIndex;
  349. if (actionIndex == null) {
  350. return;
  351. }
  352. var channelData = that.getChannelData();
  353. if (channelData.length > actionIndex) {
  354. store.setStore("channelDeta", channelData[actionIndex]);
  355. wx.navigateTo({
  356. url: './../channelDetails/channelDetails'
  357. });
  358. }
  359. },
  360. ///选择对应的频道
  361. getchannelData(clientType) {
  362. var that = this;
  363. listByDevice({
  364. clientType: clientType,
  365. unShowLoad: true,
  366. }).then((res) => {
  367. store.setStore("channelData", res);
  368. that.setData({
  369. channelData: res,
  370. });
  371. // 接收设备当前播放状态
  372. var deviceList = that.getDeviceList();
  373. var deviceListSelect = that.getDeviceListSelect();
  374. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  375. return;
  376. };
  377. var deviceId = deviceList[deviceListSelect].deviceId;
  378. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  379. const obj = {
  380. DstDeviceName: deviceMacId
  381. }
  382. app.PubMsg({
  383. type: "get_position",
  384. ...obj
  385. });
  386. })
  387. },
  388. onTapIndex(e) {
  389. var that = this;
  390. var index = e.currentTarget.dataset.index;
  391. var indexPage = that.data.indexPage;
  392. if (indexPage != index) {
  393. that.setData({
  394. indexPage: index,
  395. });
  396. }
  397. },
  398. onTapLogin() {
  399. var that = this;
  400. ///退出登录
  401. var isLogin = that.getIsLogin();
  402. if (isLogin) {
  403. that.logOut();
  404. return;
  405. }
  406. route_util.jump(route_constant.login);
  407. },
  408. ///退出登录
  409. logOut() {
  410. var that = this;
  411. wx.removeStorageSync('userInfo');
  412. wx.removeStorageSync('token');
  413. var nickName = "未登录";
  414. var greeting = that.getGreetBuNickName(nickName);
  415. that.setData({
  416. isLogin: false,
  417. greeting: greeting,
  418. nickName: nickName,
  419. userPic: "../../../img/head_pic.png",
  420. });
  421. var deviceList = that.getDeviceList();
  422. var deviceListSelect = that.getDeviceListSelect();
  423. if (deviceList.length > 1 && (deviceListSelect === null || deviceList.length <= deviceListSelect)) {
  424. // 2个设备才判断,1个直接删除
  425. return;
  426. };
  427. deviceListSelect = deviceListSelect ?? 0
  428. ///有设备在线被选中,则让它不被选择
  429. if (deviceList[deviceListSelect].connectType == 3) {
  430. var deviceMacId = that.data.deviceMacId;
  431. if (!strings.isEmpty(deviceMacId)) {
  432. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  433. app.unsubscribe(pubResponse);
  434. var indexPage = that.data.indexPage;
  435. that.setData({
  436. deviceMacId: null,
  437. actionIndex: null,
  438. deviceListSelect: null,
  439. indexPage: indexPage == 1 ? 0 : indexPage,
  440. });
  441. };
  442. } else {
  443. // console.log("退出登录,断开连接", deviceList);
  444. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  445. // var indexPage = that.data.indexPage;
  446. // that.setData({
  447. // deviceMacId: null,
  448. // actionIndex: null,
  449. // deviceListSelect: null,
  450. // indexPage: indexPage == 1 ? 0 : indexPage,
  451. // });
  452. }
  453. },
  454. ///点击item
  455. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  456. onTapItem(e) {
  457. var that = this;
  458. var isLogin = that.getIsLogin();
  459. if (!isLogin) {
  460. route_util.jump(route_constant.login);
  461. return;
  462. }
  463. var index = e.currentTarget.dataset.index;
  464. var device = e.currentTarget.dataset.item;
  465. var connectType = device.connectType;
  466. var deviceListSelect = that.getDeviceListSelect();
  467. // wifi只支持在线点击
  468. if (connectType == 3 && device.state === "online") {
  469. if (index === deviceListSelect) {
  470. that.goWake();
  471. } else {
  472. that.addWifiDevice(device);
  473. }
  474. return;
  475. }
  476. ///去蓝牙连接处理
  477. if (index === deviceListSelect) {
  478. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(device))
  479. } else if (device.state === "offline") {
  480. console.log("去连接蓝牙", device)
  481. device.name = device.devName;
  482. BtHelper.getInstance().connect(device, function (isConnected, d) {
  483. if (isConnected) {
  484. device.state = "online";
  485. that.addBlueDevice(device);
  486. }
  487. });
  488. } else {
  489. // {"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 妙播收音机"}
  490. }
  491. },
  492. // 去唤醒界面
  493. goWake() {
  494. var that = this;
  495. var deviceList = that.getDeviceList();
  496. var deviceListSelect = that.getDeviceListSelect();
  497. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  498. return
  499. };
  500. var device = deviceList[deviceListSelect];
  501. wx.navigateTo({
  502. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  503. });
  504. },
  505. ///删除当前设备
  506. deleteDevice(e) {
  507. var that = this;
  508. var index = e.currentTarget.dataset.index;
  509. wx.showModal({
  510. title: '确定删除?',
  511. success: function (res) {
  512. if (res.confirm) {
  513. var deviceList = that.getDeviceList();
  514. if (deviceList[index].connectType == 3) {
  515. that.cancelWifi(index, false);
  516. } else {
  517. that.cancelBlue(index, false);
  518. }
  519. }
  520. }
  521. });
  522. },
  523. ///是否是同一个蓝牙
  524. isTheSameBlue(device) {
  525. var that = this;
  526. var deviceList = that.getDeviceList();
  527. var deviceListSelect = that.getDeviceListSelect();
  528. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  529. return false;
  530. };
  531. if (deviceList[deviceListSelect].connectType == 3) {
  532. return false;
  533. } else {
  534. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  535. return true;
  536. } else {
  537. return false;
  538. }
  539. }
  540. },
  541. ///断开当前的
  542. cancelCurrent() {
  543. var that = this;
  544. var deviceList = that.getDeviceList();
  545. var deviceListSelect = that.getDeviceListSelect();
  546. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  547. return;
  548. };
  549. if (deviceList[deviceListSelect].connectType == 3) {
  550. that.cancelWifi(deviceListSelect, true);
  551. } else {
  552. that.cancelBlue(deviceListSelect, true);
  553. }
  554. },
  555. /// 断开蓝牙连接
  556. cancelBlue(index, onlyCancel) {
  557. var that = this;
  558. if (onlyCancel) {
  559. return;
  560. }
  561. var deviceList = that.getDeviceList();
  562. var deviceListSelect = that.getDeviceListSelect();
  563. if (deviceList.length > 1 && (deviceListSelect === null || deviceList.length <= deviceListSelect)) {
  564. // 2个设备才判断,1个直接删除
  565. return;
  566. };
  567. ///是否删除的当前播放这个
  568. var isCurrentIndex = false;
  569. deviceListSelect = deviceListSelect ?? 0;
  570. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  571. isCurrentIndex = (deviceListSelect == index);
  572. };
  573. ///删除当前设备
  574. var deviceId = deviceList[index].deviceId;
  575. deviceList = deviceList.filter((item, i) => {
  576. return deviceId !== item.deviceId;
  577. });
  578. console.log("断开蓝牙连接", deviceId)
  579. BtHelper.getInstance().disconnect({
  580. "deviceId": deviceId
  581. });
  582. store.setStore("deviceList", deviceList);
  583. var indexPage = that.data.indexPage;
  584. that.setData({
  585. deviceList: deviceList,
  586. indexPage: indexPage == 1 ? 0 : indexPage,
  587. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  588. });
  589. },
  590. /// 断开连接wifi
  591. cancelWifi(index, onlyCancel) {
  592. var that = this;
  593. var deviceList = that.getDeviceList();
  594. if (index === null || deviceList.length <= index) {
  595. return;
  596. };
  597. var deviceId = deviceList[index].deviceId;
  598. // 取消订阅
  599. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  600. app.unsubscribe(pubResponse);
  601. if (onlyCancel) {
  602. return;
  603. }
  604. ///是否删除的当前播放这个
  605. var isCurrentIndex = false;
  606. var deviceListSelect = that.getDeviceListSelect();
  607. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  608. isCurrentIndex = (deviceListSelect == index);
  609. };
  610. deviceList = deviceList.filter((item, i) => {
  611. return deviceId !== item.deviceId;
  612. });
  613. store.setStore("deviceList", deviceList);
  614. var indexPage = that.data.indexPage;
  615. var deviceMacId = that.data.deviceMacId;
  616. var actionIndex = that.data.actionIndex;
  617. that.setData({
  618. deviceList: deviceList,
  619. deviceMacId: isCurrentIndex ? null : deviceMacId,
  620. actionIndex: isCurrentIndex ? null : actionIndex,
  621. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  622. indexPage: indexPage == 1 ? 0 : indexPage,
  623. });
  624. },
  625. /// 新添加蓝牙设备
  626. addBlueDevice(device) {
  627. var that = this;
  628. var addDeviceList = lexin_add.addBlueDevice(device);
  629. that.updateDeviceList(addDeviceList, false, false);
  630. that.setData({
  631. deviceListSelect: 0,
  632. autoConnected: true,
  633. });
  634. event_bus.removeNotification(CmdEvent.eventName, that);
  635. event_bus.addNotification(CmdEvent.eventName, function (event) {
  636. if (event.cmdEvent == EnumCmdEvent.onoffline) {
  637. if (tmp.connected != false) {
  638. return
  639. }
  640. let disDevice = that.data.deviceList.find(item => {
  641. return item.deviceId == event.deviceId
  642. })
  643. // that.data.deviceList.forEach(element => {
  644. // if (item.deviceId == tmp.deviceId) {
  645. // }
  646. // });
  647. disDevice.state = event.commonValue;
  648. console.log("断开连接", disDevice)
  649. that.updateDeviceList(that.data.deviceList, false, false);
  650. }
  651. });
  652. },
  653. // 新添加Wifi设备
  654. addWifiDevice(device) {
  655. var that = this;
  656. var isOK = app.globalData.client && app.globalData.client.connected;
  657. if (!isOK) {
  658. app.connect();
  659. setTimeout(() => {
  660. that.addWifiDevice(device);
  661. }, 500);
  662. return;
  663. };
  664. var deviceId = device.deviceId;
  665. var addDeviceList = lexin_add.addWifiDevice(device);
  666. that.updateDeviceList(addDeviceList, false, false);
  667. var indexPage = that.data.indexPage;
  668. that.setData({
  669. deviceListSelect: null,
  670. connectDeviceIding: deviceId,
  671. indexPage: indexPage == 1 ? 0 : indexPage,
  672. });
  673. lexin_subscribe.subscribeSingleDevice(device);
  674. },
  675. refreshCurrentDevice(device) {
  676. var that = this;
  677. var addDeviceList = lexin_add.addWifiDevice(device);
  678. that.updateDeviceList(addDeviceList, false, false);
  679. that.setData({
  680. autoConnected: true,
  681. deviceListSelect: 0,
  682. connectDeviceIding: "",
  683. });
  684. },
  685. /// 手机关闭蓝牙,所有蓝牙设备离线
  686. closeBlueResetOffline(isInit, closeAllBlue) {
  687. var that = this;
  688. var deviceList = that.getDeviceList();
  689. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  690. },
  691. /// 更新列表排序
  692. updateDeviceList(deviceList, isInit, closeAllBlue) {
  693. var that = this;
  694. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  695. if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
  696. store.setStore("deviceList", finalList);
  697. // todo 测试代码
  698. // finalList.forEach(element => {
  699. // element.state = "online";
  700. // });
  701. that.setData({
  702. deviceList: finalList,
  703. });
  704. }
  705. },
  706. ///点击banner事件
  707. onTapBanner() {
  708. var that = this;
  709. var item = e.currentTarget.dataset.item;
  710. },
  711. ///添加设备
  712. jumpToAddDevice() {
  713. var that = this;
  714. var isLogin = that.getIsLogin();
  715. if (!isLogin) {
  716. route_util.jump(route_constant.login);
  717. return;
  718. }
  719. route_util.jump(route_constant.deviceList);
  720. },
  721. // 关于我们
  722. jumpToAboutUs() {
  723. lexin_jump.toAboutUs();
  724. },
  725. /// 是否已登录
  726. getIsLogin() {
  727. var that = this;
  728. var isLogin = that.data.isLogin;
  729. return isLogin;
  730. },
  731. /// 获取列表数据
  732. getDeviceList() {
  733. var that = this;
  734. var deviceList = that.data.deviceList;
  735. return deviceList;
  736. },
  737. ///获取当前选中的
  738. getDeviceListSelect() {
  739. var that = this;
  740. var deviceListSelect = that.data.deviceListSelect;
  741. return deviceListSelect;
  742. },
  743. ///获取是否已经第一次连接过
  744. getAutoConnected() {
  745. var that = this;
  746. var autoConnected = that.data.autoConnected;
  747. return autoConnected;
  748. },
  749. ///当前正在连接的设备id
  750. getConnectDeviceIding() {
  751. var that = this;
  752. var connectDeviceIding = that.data.connectDeviceIding
  753. return connectDeviceIding;
  754. },
  755. ///获取频道数据
  756. getChannelData() {
  757. var that = this;
  758. var channelData = that.data.channelData;
  759. return channelData;
  760. },
  761. /// 当前播放频道
  762. getActionIndex() {
  763. var that = this;
  764. var actionIndex = that.data.actionIndex;
  765. return actionIndex;
  766. },
  767. /// 获取当前的deviceId
  768. getCurrentDeviceId() {
  769. var that = this;
  770. var deviceList = that.getDeviceList();
  771. var deviceListSelect = that.getDeviceListSelect();
  772. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  773. return "";
  774. };
  775. var deviceId = deviceList[deviceListSelect].deviceId;
  776. return deviceId;
  777. },
  778. ///设置设备相关信息
  779. setDeviceInfor(other) {
  780. var that = this;
  781. // 接收设备当前信息
  782. that.getchannelData(other.ProdModel);
  783. // 电量
  784. if (other.Power) {
  785. var battery = lexin_message.getBattery(other.Power);
  786. that.setData({
  787. battery: battery,
  788. });
  789. }
  790. // 当前设备木有设置定时
  791. that.setData({
  792. isSetWake: false,
  793. });
  794. other.alarm.map((v) => {
  795. if (v.enable === "1") {
  796. that.setData({
  797. isSetWake: true
  798. });
  799. }
  800. });
  801. },
  802. ///******************************* 可折叠 ********************************************///
  803. onUserInfoLoad() {
  804. var that = this;
  805. var userInfo = wx.getStorageSync("userInfo") || "";
  806. if (!strings.isEmpty(userInfo)) {
  807. var phone = userInfo.phone;
  808. if (!phone) {
  809. return;
  810. };
  811. var nickName = userInfo.nickname || "";
  812. var greeting = that.getGreetBuNickName(nickName);
  813. // 获取缓存的频道数据
  814. var channelData = wx.getStorageSync("channelData") || "";
  815. if (!strings.isEmpty(channelData)) {
  816. that.setData({
  817. channelData: channelData,
  818. greeting: greeting,
  819. nickName: nickName,
  820. userPic: userInfo.headUrl || "",
  821. isLogin: true,
  822. })
  823. } else {
  824. that.setData({
  825. greeting: greeting,
  826. nickName: nickName,
  827. userPic: userInfo.headUrl || "",
  828. isLogin: true,
  829. });
  830. }
  831. } else {
  832. var greeting = that.getGreetBuNickName("");
  833. that.setData({
  834. greeting: greeting,
  835. })
  836. }
  837. },
  838. getGreetBuNickName(nickName) {
  839. var greeting = timeUtil.getGreet();
  840. if (nickName != "" && nickName != "未登录") {
  841. greeting = greeting + ',' + nickName;;
  842. }
  843. return greeting;
  844. },
  845. onBannerLoad() {
  846. var that = this;
  847. // var bannerList = wx.getStorageSync("homeBanner") || [];
  848. // if (that.data.bannerList.length > 0) {
  849. // that.setData({
  850. // bannerList: bannerList
  851. // });
  852. // }
  853. },
  854. onBannerShow() {
  855. var that = this;
  856. // getBanner({}).then((res) => {
  857. // that.setData({
  858. // bannerList: res
  859. // });
  860. // store.setStore("homeBanner", res);
  861. // })
  862. },
  863. stopIntervalId1: function () {
  864. var that = this;
  865. if (!strings.isEmpty(that.data.intervalId1)) {
  866. clearInterval(that.data.intervalId1);
  867. that.data.intervalId1 = null;
  868. }
  869. },
  870. stopIntervalId2: function () {
  871. var that = this;
  872. if (!strings.isEmpty(that.data.intervalId2)) {
  873. clearInterval(that.data.intervalId2);
  874. that.data.intervalId2 = null;
  875. }
  876. },
  877. ///销毁蓝牙
  878. onUnload() {
  879. var that = this;
  880. event_bus.removeNotification(CmdEvent.eventName, that)
  881. that.stopIntervalId1();
  882. that.stopIntervalId2();
  883. if (!strings.isEmpty(that.data.intervalId)) {
  884. clearInterval(that.data.intervalId);
  885. that.data.intervalId = null;
  886. }
  887. BtHelper.getInstance().disconnect();
  888. if (app.globalData.client === null) {
  889. return;
  890. };
  891. app.globalData.client.end(true);
  892. app.globalData.client.end(true);
  893. app.globalData.client = null;
  894. },
  895. })
  896. // deviceList: [{
  897. // "connectType": 3,
  898. // "devName": "猫王小王子OTR-X",
  899. // "state": "online",
  900. // "name": "BLUFI_7cdfa1fcbb24",
  901. // "deviceId": "BLUFI_7cdfa1fcbb24",
  902. // "mac": "BLUFI_7cdfa1fcbb24",
  903. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  904. // "ProdModel": "MW-2AX(WIFI-N)",
  905. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  906. // }, {
  907. // "connectType": 3,
  908. // "devName": "猫王小王子OTR-X",
  909. // "state": "online",
  910. // "name": "BLUFI_7cdfa1fcbb24",
  911. // "deviceId": "BLUFI_7cdfa1fcbb24",
  912. // "mac": "BLUFI_7cdfa1fcbb24",
  913. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  914. // "ProdModel": "MW-2AX(WIFI-N)",
  915. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  916. // }, {
  917. // "connectType": 3,
  918. // "devName": "猫王小王子OTR-X",
  919. // "state": "online",
  920. // "name": "BLUFI_7cdfa1fcbb24",
  921. // "deviceId": "BLUFI_7cdfa1fcbb24",
  922. // "mac": "BLUFI_7cdfa1fcbb24",
  923. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  924. // "ProdModel": "MW-2AX(WIFI-N)",
  925. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  926. // }, {
  927. // "connectType": 3,
  928. // "devName": "猫王小王子OTR-X",
  929. // "state": "online",
  930. // "name": "BLUFI_7cdfa1fcbb24",
  931. // "deviceId": "BLUFI_7cdfa1fcbb24",
  932. // "mac": "BLUFI_7cdfa1fcbb24",
  933. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  934. // "ProdModel": "MW-2AX(WIFI-N)",
  935. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  936. // }, {
  937. // "connectType": 3,
  938. // "devName": "猫王小王子OTR-X",
  939. // "state": "online",
  940. // "name": "BLUFI_7cdfa1fcbb24",
  941. // "deviceId": "BLUFI_7cdfa1fcbb24",
  942. // "mac": "BLUFI_7cdfa1fcbb24",
  943. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  944. // "ProdModel": "MW-2AX(WIFI-N)",
  945. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  946. // }, {
  947. // "connectType": 3,
  948. // "devName": "猫王小王子OTR-X",
  949. // "state": "online",
  950. // "name": "BLUFI_7cdfa1fcbb24",
  951. // "deviceId": "BLUFI_7cdfa1fcbb24",
  952. // "mac": "BLUFI_7cdfa1fcbb24",
  953. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  954. // "ProdModel": "MW-2AX(WIFI-N)",
  955. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  956. // }],