index.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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_util from '../../utils/route_util.js';
  10. import route_constant from '../../utils/route_constant.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. route_util.jump(route_constant.channelDetail);
  356. }
  357. },
  358. ///选择对应的频道
  359. getchannelData(clientType) {
  360. var that = this;
  361. listByDevice({
  362. clientType: clientType,
  363. unShowLoad: true,
  364. }).then((res) => {
  365. store.setStore("channelData", res);
  366. that.setData({
  367. channelData: res,
  368. });
  369. // 接收设备当前播放状态
  370. var deviceList = that.getDeviceList();
  371. var deviceListSelect = that.getDeviceListSelect();
  372. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  373. return;
  374. };
  375. var deviceId = deviceList[deviceListSelect].deviceId;
  376. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  377. const obj = {
  378. DstDeviceName: deviceMacId
  379. }
  380. app.PubMsg({
  381. type: "get_position",
  382. ...obj
  383. });
  384. })
  385. },
  386. onTapIndex(e) {
  387. var that = this;
  388. var index = e.currentTarget.dataset.index;
  389. var indexPage = that.data.indexPage;
  390. if (indexPage != index) {
  391. that.setData({
  392. indexPage: index,
  393. });
  394. }
  395. },
  396. onTapLogin() {
  397. var that = this;
  398. ///退出登录
  399. var isLogin = that.getIsLogin();
  400. if (isLogin) {
  401. that.logOut();
  402. return;
  403. }
  404. route_util.jump(route_constant.login);
  405. },
  406. ///退出登录
  407. logOut() {
  408. var that = this;
  409. wx.removeStorageSync('userInfo');
  410. wx.removeStorageSync('token');
  411. var nickName = "未登录";
  412. var greeting = that.getGreetBuNickName(nickName);
  413. that.setData({
  414. isLogin: false,
  415. greeting: greeting,
  416. nickName: nickName,
  417. userPic: "../../../img/head_pic.png",
  418. });
  419. var deviceList = that.getDeviceList();
  420. var deviceListSelect = that.getDeviceListSelect();
  421. if (deviceList.length > 1 && (deviceListSelect === null || deviceList.length <= deviceListSelect)) {
  422. // 2个设备才判断,1个直接删除
  423. return;
  424. };
  425. deviceListSelect = deviceListSelect ?? 0
  426. ///有设备在线被选中,则让它不被选择
  427. if (deviceList[deviceListSelect].connectType == 3) {
  428. var deviceMacId = that.data.deviceMacId;
  429. if (!strings.isEmpty(deviceMacId)) {
  430. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  431. app.unsubscribe(pubResponse);
  432. var indexPage = that.data.indexPage;
  433. that.setData({
  434. deviceMacId: null,
  435. actionIndex: null,
  436. deviceListSelect: null,
  437. indexPage: indexPage == 1 ? 0 : indexPage,
  438. });
  439. };
  440. } else {
  441. // console.log("退出登录,断开连接", deviceList);
  442. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  443. // var indexPage = that.data.indexPage;
  444. // that.setData({
  445. // deviceMacId: null,
  446. // actionIndex: null,
  447. // deviceListSelect: null,
  448. // indexPage: indexPage == 1 ? 0 : indexPage,
  449. // });
  450. }
  451. },
  452. ///点击item
  453. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  454. async onTapItem(e) {
  455. var that = this;
  456. var isLogin = that.getIsLogin();
  457. if (!isLogin) {
  458. route_util.jump(route_constant.login);
  459. return;
  460. }
  461. var index = e.currentTarget.dataset.index;
  462. var device = e.currentTarget.dataset.item;
  463. var connectType = device.connectType;
  464. var deviceListSelect = that.getDeviceListSelect();
  465. // wifi只支持在线点击
  466. if (connectType == 3 && device.state === "online") {
  467. if (index === deviceListSelect) {
  468. that.goWake();
  469. } else {
  470. that.addWifiDevice(device);
  471. }
  472. return;
  473. }
  474. ///去蓝牙连接处理
  475. // {"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 妙播收音机"}
  476. if (index === deviceListSelect && device.state === "online") {
  477. var param = '?param=' + JSON.stringify(device);
  478. route_util.jumpParam(route_constant.deviceDetail, param);
  479. } else if (device.state === "offline") {
  480. var deviceList = that.getDeviceList();
  481. var deviceListSelect = that.getDeviceListSelect();
  482. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  483. that.setData({
  484. deviceListSelect: null,
  485. });
  486. var currentDevice = deviceList[deviceListSelect];
  487. await BtHelper.getInstance().disconnect(currentDevice);
  488. // await time_util.delayMills(500);
  489. }
  490. device.name = device.devName;
  491. BtHelper.getInstance().connect(device, function (isConnected, d) {
  492. if (isConnected) {
  493. device.state = "online";
  494. that.addBlueDevice(device);
  495. }
  496. });
  497. } else {
  498. var deviceList = that.getDeviceList();
  499. var deviceListSelect = that.getDeviceListSelect();
  500. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  501. that.setData({
  502. deviceListSelect: null,
  503. });
  504. var currentDevice = deviceList[deviceListSelect];
  505. await BtHelper.getInstance().disconnect(currentDevice);
  506. // await time_util.delayMills(500);
  507. }
  508. device.name = device.devName;
  509. BtHelper.getInstance().connect(device, function (isConnected, d) {
  510. if (isConnected) {
  511. device.state = "online";
  512. that.addBlueDevice(device);
  513. }
  514. });
  515. }
  516. },
  517. // 去唤醒界面
  518. goWake() {
  519. var that = this;
  520. var deviceList = that.getDeviceList();
  521. var deviceListSelect = that.getDeviceListSelect();
  522. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  523. return
  524. };
  525. var device = deviceList[deviceListSelect];
  526. var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel;
  527. route_util.jumpParam(route_constant.deviceWake, param);
  528. },
  529. ///删除当前设备
  530. deleteDevice(e) {
  531. var that = this;
  532. var index = e.currentTarget.dataset.index;
  533. wx.showModal({
  534. title: '确定删除?',
  535. success: function (res) {
  536. if (res.confirm) {
  537. var deviceList = that.getDeviceList();
  538. if (deviceList[index].connectType == 3) {
  539. that.cancelWifi(index, false);
  540. } else {
  541. that.cancelBlue(index, false);
  542. }
  543. }
  544. }
  545. });
  546. },
  547. ///是否是同一个蓝牙
  548. isTheSameBlue(device) {
  549. var that = this;
  550. var deviceList = that.getDeviceList();
  551. var deviceListSelect = that.getDeviceListSelect();
  552. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  553. return false;
  554. };
  555. if (deviceList[deviceListSelect].connectType == 3) {
  556. return false;
  557. } else {
  558. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  559. return true;
  560. } else {
  561. return false;
  562. }
  563. }
  564. },
  565. ///断开当前的
  566. cancelCurrent() {
  567. var that = this;
  568. var deviceList = that.getDeviceList();
  569. var deviceListSelect = that.getDeviceListSelect();
  570. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  571. return;
  572. };
  573. if (deviceList[deviceListSelect].connectType == 3) {
  574. that.cancelWifi(deviceListSelect, true);
  575. } else {
  576. that.cancelBlue(deviceListSelect, true);
  577. }
  578. },
  579. /// 断开蓝牙连接
  580. cancelBlue(index, onlyCancel) {
  581. var that = this;
  582. if (onlyCancel) {
  583. return;
  584. }
  585. var deviceList = that.getDeviceList();
  586. var deviceListSelect = that.getDeviceListSelect();
  587. ///是否删除的当前播放这个
  588. var isCurrentIndex = false;
  589. deviceListSelect = deviceListSelect ?? 0;
  590. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  591. isCurrentIndex = (deviceListSelect == index);
  592. };
  593. ///删除当前设备
  594. var deviceId = deviceList[index].deviceId;
  595. deviceList = deviceList.filter((item, i) => {
  596. return deviceId !== item.deviceId;
  597. });
  598. store.setStore("deviceList", deviceList);
  599. var indexPage = that.data.indexPage;
  600. that.setData({
  601. deviceList: deviceList,
  602. indexPage: indexPage == 1 ? 0 : indexPage,
  603. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  604. });
  605. BtHelper.getInstance().disconnect({
  606. "deviceId": deviceId
  607. });
  608. },
  609. /// 断开连接wifi
  610. cancelWifi(index, onlyCancel) {
  611. var that = this;
  612. var deviceList = that.getDeviceList();
  613. if (index === null || deviceList.length <= index) {
  614. return;
  615. };
  616. var deviceId = deviceList[index].deviceId;
  617. // 取消订阅
  618. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  619. app.unsubscribe(pubResponse);
  620. if (onlyCancel) {
  621. return;
  622. }
  623. ///是否删除的当前播放这个
  624. var isCurrentIndex = false;
  625. var deviceListSelect = that.getDeviceListSelect();
  626. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  627. isCurrentIndex = (deviceListSelect == index);
  628. };
  629. deviceList = deviceList.filter((item, i) => {
  630. return deviceId !== item.deviceId;
  631. });
  632. store.setStore("deviceList", deviceList);
  633. var indexPage = that.data.indexPage;
  634. var deviceMacId = that.data.deviceMacId;
  635. var actionIndex = that.data.actionIndex;
  636. that.setData({
  637. deviceList: deviceList,
  638. deviceMacId: isCurrentIndex ? null : deviceMacId,
  639. actionIndex: isCurrentIndex ? null : actionIndex,
  640. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  641. indexPage: indexPage == 1 ? 0 : indexPage,
  642. });
  643. },
  644. /// 新添加蓝牙设备
  645. addBlueDevice(device) {
  646. var that = this;
  647. var addDeviceList = lexin_add.addBlueDevice(device);
  648. that.updateDeviceList(addDeviceList, false, false);
  649. that.setData({
  650. deviceListSelect: 0,
  651. autoConnected: true,
  652. });
  653. event_bus.removeNotification(CmdEvent.eventName, that);
  654. event_bus.addNotification(CmdEvent.eventName, function (event) {
  655. if (event.cmdEvent == EnumCmdEvent.onoffline) {
  656. if (tmp.connected != false) {
  657. return
  658. }
  659. let disDevice = that.data.deviceList.find(item => {
  660. return item.deviceId == event.deviceId
  661. })
  662. // that.data.deviceList.forEach(element => {
  663. // if (item.deviceId == tmp.deviceId) {
  664. // }
  665. // });
  666. disDevice.state = event.commonValue;
  667. console.log("断开连接", disDevice)
  668. that.updateDeviceList(that.data.deviceList, false, false);
  669. }
  670. });
  671. },
  672. // 新添加Wifi设备
  673. addWifiDevice(device) {
  674. var that = this;
  675. var isOK = app.globalData.client && app.globalData.client.connected;
  676. if (!isOK) {
  677. app.connect();
  678. setTimeout(() => {
  679. that.addWifiDevice(device);
  680. }, 500);
  681. return;
  682. };
  683. var deviceId = device.deviceId;
  684. var addDeviceList = lexin_add.addWifiDevice(device);
  685. that.updateDeviceList(addDeviceList, false, false);
  686. var indexPage = that.data.indexPage;
  687. that.setData({
  688. deviceListSelect: null,
  689. connectDeviceIding: deviceId,
  690. indexPage: indexPage == 1 ? 0 : indexPage,
  691. });
  692. lexin_subscribe.subscribeSingleDevice(device);
  693. },
  694. refreshCurrentDevice(device) {
  695. var that = this;
  696. var addDeviceList = lexin_add.addWifiDevice(device);
  697. that.updateDeviceList(addDeviceList, false, false);
  698. that.setData({
  699. autoConnected: true,
  700. deviceListSelect: 0,
  701. connectDeviceIding: "",
  702. });
  703. },
  704. /// 手机关闭蓝牙,所有蓝牙设备离线
  705. closeBlueResetOffline(isInit, closeAllBlue) {
  706. var that = this;
  707. var deviceList = that.getDeviceList();
  708. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  709. },
  710. /// 更新列表排序
  711. updateDeviceList(deviceList, isInit, closeAllBlue) {
  712. var that = this;
  713. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  714. if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
  715. store.setStore("deviceList", finalList);
  716. // todo 测试代码
  717. // finalList.forEach(element => {
  718. // element.state = "online";
  719. // });
  720. that.setData({
  721. deviceList: finalList,
  722. });
  723. }
  724. },
  725. ///点击banner事件
  726. onTapBanner() {
  727. var that = this;
  728. var item = e.currentTarget.dataset.item;
  729. },
  730. ///添加设备
  731. jumpToAddDevice() {
  732. var that = this;
  733. var isLogin = that.getIsLogin();
  734. if (!isLogin) {
  735. route_util.jump(route_constant.login);
  736. return;
  737. }
  738. route_util.jump(route_constant.deviceList);
  739. },
  740. // 关于我们
  741. jumpToAboutUs() {
  742. lexin_jump.toAboutUs();
  743. },
  744. /// 是否已登录
  745. getIsLogin() {
  746. var that = this;
  747. var isLogin = that.data.isLogin;
  748. return isLogin;
  749. },
  750. /// 获取列表数据
  751. getDeviceList() {
  752. var that = this;
  753. var deviceList = that.data.deviceList;
  754. return deviceList;
  755. },
  756. ///获取当前选中的
  757. getDeviceListSelect() {
  758. var that = this;
  759. var deviceListSelect = that.data.deviceListSelect;
  760. return deviceListSelect;
  761. },
  762. ///获取是否已经第一次连接过
  763. getAutoConnected() {
  764. var that = this;
  765. var autoConnected = that.data.autoConnected;
  766. return autoConnected;
  767. },
  768. ///当前正在连接的设备id
  769. getConnectDeviceIding() {
  770. var that = this;
  771. var connectDeviceIding = that.data.connectDeviceIding
  772. return connectDeviceIding;
  773. },
  774. ///获取频道数据
  775. getChannelData() {
  776. var that = this;
  777. var channelData = that.data.channelData;
  778. return channelData;
  779. },
  780. /// 当前播放频道
  781. getActionIndex() {
  782. var that = this;
  783. var actionIndex = that.data.actionIndex;
  784. return actionIndex;
  785. },
  786. /// 获取当前的deviceId
  787. getCurrentDeviceId() {
  788. var that = this;
  789. var deviceList = that.getDeviceList();
  790. var deviceListSelect = that.getDeviceListSelect();
  791. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  792. return "";
  793. };
  794. var deviceId = deviceList[deviceListSelect].deviceId;
  795. return deviceId;
  796. },
  797. ///设置设备相关信息
  798. setDeviceInfor(other) {
  799. var that = this;
  800. // 接收设备当前信息
  801. that.getchannelData(other.ProdModel);
  802. // 电量
  803. if (other.Power) {
  804. var battery = lexin_message.getBattery(other.Power);
  805. that.setData({
  806. battery: battery,
  807. });
  808. }
  809. // 当前设备木有设置定时
  810. that.setData({
  811. isSetWake: false,
  812. });
  813. other.alarm.map((v) => {
  814. if (v.enable === "1") {
  815. that.setData({
  816. isSetWake: true
  817. });
  818. }
  819. });
  820. },
  821. ///******************************* 可折叠 ********************************************///
  822. onUserInfoLoad() {
  823. var that = this;
  824. var userInfo = wx.getStorageSync("userInfo") || "";
  825. if (!strings.isEmpty(userInfo)) {
  826. var phone = userInfo.phone;
  827. if (!phone) {
  828. return;
  829. };
  830. var nickName = userInfo.nickname || "";
  831. var greeting = that.getGreetBuNickName(nickName);
  832. // 获取缓存的频道数据
  833. var channelData = wx.getStorageSync("channelData") || "";
  834. if (!strings.isEmpty(channelData)) {
  835. that.setData({
  836. channelData: channelData,
  837. greeting: greeting,
  838. nickName: nickName,
  839. userPic: userInfo.headUrl || "",
  840. isLogin: true,
  841. })
  842. } else {
  843. that.setData({
  844. greeting: greeting,
  845. nickName: nickName,
  846. userPic: userInfo.headUrl || "",
  847. isLogin: true,
  848. });
  849. }
  850. } else {
  851. var greeting = that.getGreetBuNickName("");
  852. that.setData({
  853. greeting: greeting,
  854. })
  855. }
  856. },
  857. getGreetBuNickName(nickName) {
  858. var greeting = timeUtil.getGreet();
  859. if (nickName != "" && nickName != "未登录") {
  860. greeting = greeting + ',' + nickName;;
  861. }
  862. return greeting;
  863. },
  864. onBannerLoad() {
  865. var that = this;
  866. // var bannerList = wx.getStorageSync("homeBanner") || [];
  867. // if (that.data.bannerList.length > 0) {
  868. // that.setData({
  869. // bannerList: bannerList
  870. // });
  871. // }
  872. },
  873. onBannerShow() {
  874. var that = this;
  875. // getBanner({}).then((res) => {
  876. // that.setData({
  877. // bannerList: res
  878. // });
  879. // store.setStore("homeBanner", res);
  880. // })
  881. },
  882. stopIntervalId1: function () {
  883. var that = this;
  884. if (!strings.isEmpty(that.data.intervalId1)) {
  885. clearInterval(that.data.intervalId1);
  886. that.data.intervalId1 = null;
  887. }
  888. },
  889. stopIntervalId2: function () {
  890. var that = this;
  891. if (!strings.isEmpty(that.data.intervalId2)) {
  892. clearInterval(that.data.intervalId2);
  893. that.data.intervalId2 = null;
  894. }
  895. },
  896. ///销毁蓝牙
  897. onUnload() {
  898. var that = this;
  899. event_bus.removeNotification(CmdEvent.eventName, that)
  900. that.stopIntervalId1();
  901. that.stopIntervalId2();
  902. if (!strings.isEmpty(that.data.intervalId)) {
  903. clearInterval(that.data.intervalId);
  904. that.data.intervalId = null;
  905. }
  906. BtHelper.getInstance().disconnect();
  907. if (app.globalData.client === null) {
  908. return;
  909. };
  910. app.globalData.client.end(true);
  911. app.globalData.client.end(true);
  912. app.globalData.client = null;
  913. },
  914. })
  915. // deviceList: [{
  916. // "connectType": 3,
  917. // "devName": "猫王小王子OTR-X",
  918. // "state": "online",
  919. // "name": "BLUFI_7cdfa1fcbb24",
  920. // "deviceId": "BLUFI_7cdfa1fcbb24",
  921. // "mac": "BLUFI_7cdfa1fcbb24",
  922. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  923. // "ProdModel": "MW-2AX(WIFI-N)",
  924. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  925. // }, {
  926. // "connectType": 3,
  927. // "devName": "猫王小王子OTR-X",
  928. // "state": "online",
  929. // "name": "BLUFI_7cdfa1fcbb24",
  930. // "deviceId": "BLUFI_7cdfa1fcbb24",
  931. // "mac": "BLUFI_7cdfa1fcbb24",
  932. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  933. // "ProdModel": "MW-2AX(WIFI-N)",
  934. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  935. // }, {
  936. // "connectType": 3,
  937. // "devName": "猫王小王子OTR-X",
  938. // "state": "online",
  939. // "name": "BLUFI_7cdfa1fcbb24",
  940. // "deviceId": "BLUFI_7cdfa1fcbb24",
  941. // "mac": "BLUFI_7cdfa1fcbb24",
  942. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  943. // "ProdModel": "MW-2AX(WIFI-N)",
  944. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  945. // }, {
  946. // "connectType": 3,
  947. // "devName": "猫王小王子OTR-X",
  948. // "state": "online",
  949. // "name": "BLUFI_7cdfa1fcbb24",
  950. // "deviceId": "BLUFI_7cdfa1fcbb24",
  951. // "mac": "BLUFI_7cdfa1fcbb24",
  952. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  953. // "ProdModel": "MW-2AX(WIFI-N)",
  954. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  955. // }, {
  956. // "connectType": 3,
  957. // "devName": "猫王小王子OTR-X",
  958. // "state": "online",
  959. // "name": "BLUFI_7cdfa1fcbb24",
  960. // "deviceId": "BLUFI_7cdfa1fcbb24",
  961. // "mac": "BLUFI_7cdfa1fcbb24",
  962. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  963. // "ProdModel": "MW-2AX(WIFI-N)",
  964. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  965. // }, {
  966. // "connectType": 3,
  967. // "devName": "猫王小王子OTR-X",
  968. // "state": "online",
  969. // "name": "BLUFI_7cdfa1fcbb24",
  970. // "deviceId": "BLUFI_7cdfa1fcbb24",
  971. // "mac": "BLUFI_7cdfa1fcbb24",
  972. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  973. // "ProdModel": "MW-2AX(WIFI-N)",
  974. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  975. // }],