index.js 30 KB

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