index.js 29 KB

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