index.js 29 KB

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