index.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. // {"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. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(device))
  480. } else if (device.state === "offline") {
  481. var deviceList = that.getDeviceList();
  482. var deviceListSelect = that.getDeviceListSelect();
  483. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  484. that.setData({
  485. deviceListSelect: null,
  486. });
  487. var currentDevice = deviceList[deviceListSelect];
  488. await BtHelper.getInstance().disconnect(currentDevice);
  489. // await time_util.delayMills(500);
  490. }
  491. device.name = device.devName;
  492. BtHelper.getInstance().connect(device, function (isConnected, d) {
  493. if (isConnected) {
  494. device.state = "online";
  495. that.addBlueDevice(device);
  496. }
  497. });
  498. } else {
  499. var deviceList = that.getDeviceList();
  500. var deviceListSelect = that.getDeviceListSelect();
  501. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  502. that.setData({
  503. deviceListSelect: null,
  504. });
  505. var currentDevice = deviceList[deviceListSelect];
  506. await BtHelper.getInstance().disconnect(currentDevice);
  507. // await time_util.delayMills(500);
  508. }
  509. device.name = device.devName;
  510. BtHelper.getInstance().connect(device, function (isConnected, d) {
  511. if (isConnected) {
  512. device.state = "online";
  513. that.addBlueDevice(device);
  514. }
  515. });
  516. }
  517. },
  518. // 去唤醒界面
  519. goWake() {
  520. var that = this;
  521. var deviceList = that.getDeviceList();
  522. var deviceListSelect = that.getDeviceListSelect();
  523. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  524. return
  525. };
  526. var device = deviceList[deviceListSelect];
  527. wx.navigateTo({
  528. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  529. });
  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. // }],