index.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. const constant = require('../../../utils/constant.js');
  2. import {
  3. BtHelper
  4. } from '../../../devices/bt_helper.js';
  5. import {
  6. EnumCmdEvent,
  7. CmdEvent
  8. } from '../../../devices/cmd_key_event';
  9. Page({
  10. data: {
  11. navBarHeight: constant.app.globalData.navBarHeight,
  12. safeBottomHeight: constant.app.globalData.safeBottomHeight,
  13. bannerList: constant.app.globalData.bannerList,
  14. nvabarData: {
  15. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  16. title: 'OhPlay', //导航栏 中间的标题
  17. },
  18. ///下午好
  19. isLogin: false,
  20. greeting: "",
  21. // bannerList: [{
  22. // "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  23. // },
  24. // {
  25. // "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  26. // }
  27. // ],
  28. autoplay: true,
  29. interval: 3000, // 切换时间间隔
  30. duration: 500, // 滑动动画时长
  31. circular: true, // 衔接滑动
  32. indexPage: 0,
  33. ///是否展示频道
  34. showChannel: false,
  35. ////首页
  36. navBarHeight: constant.app.globalData.navBarHeight,
  37. MenuButtonheight: constant.app.globalData.MenuButtonheight,
  38. MenuButtonTop: constant.app.globalData.MenuButtonTop,
  39. actionIndex: null,
  40. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  41. channelData: [],
  42. isOneLoading: true,
  43. uid: null,
  44. isSetWake: false,
  45. battery: 4, // 0≤电量<20,0格
  46. ////我的界面
  47. loginStatus: true,
  48. nickName: "未登录",
  49. userPic: './../../../img/head_pic.png',
  50. intervalId: null,
  51. /// 是否第一次自动连接过
  52. autoConnected: false,
  53. intervalId1: null,
  54. intervalId2: null,
  55. deviceMacId: null,
  56. deviceListSelect: null,
  57. deviceList: [],
  58. _willConnectBle: null,
  59. _hasShowModal: null,
  60. _clickOtherDevice: false,
  61. connectDeviceIding: "",
  62. },
  63. onLoad(options) {
  64. var that = this;
  65. that.onLoginLoad();
  66. that.onDeviceLoad();
  67. that.onUserInfoLoad();
  68. that.onBannerLoad();
  69. },
  70. onShow() {},
  71. onLoginLoad() {
  72. var that = this;
  73. var userInfo = wx.getStorageSync("userInfo") || "";
  74. if (!constant.strings.isEmpty(userInfo)) {
  75. var phone = userInfo.phone;
  76. if (!constant.strings.isEmpty(phone)) {
  77. that.loginReset(phone, null, null, null);
  78. };
  79. }
  80. },
  81. ///重新登录
  82. loginReset(phone, succeedCall1, succeedCall2, failCall) {
  83. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
  84. var that = this;
  85. wx.login({
  86. success: res0 => {
  87. // {"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}
  88. constant.apiUtil.login({
  89. code: res0.code,
  90. phone: phone,
  91. }).then((res1) => {
  92. constant.app.globalData.userInfo = res1;
  93. constant.store.setStore("userInfo", res1);
  94. that.setData({
  95. isLogin: true,
  96. });
  97. constant.store.setStore("token", JSON.stringify({
  98. id: res1.userId,
  99. token: res1.accessToken,
  100. }));
  101. that.onUserInfoLoad();
  102. if (succeedCall1 != null) {
  103. succeedCall1();
  104. }
  105. if (succeedCall2 != null) {
  106. succeedCall2();
  107. }
  108. });
  109. },
  110. fail(fail4) {
  111. if (failCall != null) {
  112. failCall();
  113. }
  114. },
  115. });
  116. },
  117. testData() {
  118. return {
  119. "deviceId": "xxxxx",
  120. "deviceName": "xxxxxx",
  121. "deviceType": "xxxx",
  122. "type": 1,
  123. "deviceIcon": "xxxx",
  124. "deviceMacId": "xxxx",
  125. "deviceBattery": 4,
  126. "state": "online",
  127. "connectType": 1,
  128. "clientType": "猫王妙播黑胶音箱(BLE)",
  129. "deviceConnectStatus": 1,
  130. "deviceConnectStatusText": "xxxx",
  131. }
  132. },
  133. onDeviceLoad() {
  134. var that = this;
  135. var deviceList = constant.store.getStore("deviceList");
  136. if (!constant.strings.isEmpty(deviceList)) {
  137. // deviceList = deviceList.concat(deviceList);
  138. that.updateDeviceList(deviceList, true, false);
  139. }
  140. // that.addBlueDevice(that.testData())
  141. let hasBle = that.data.deviceList.find(item => {
  142. return item.connectType == 1;
  143. })
  144. if (hasBle) {
  145. ///监听蓝牙设备
  146. BtHelper.getInstance().initBluetoothAdapter();
  147. BtHelper.getInstance().getBluetoothDevices();
  148. BtHelper.getInstance().startScan(null, null, function (res) {});
  149. var isFirst = true;
  150. console.log("开始监听蓝牙设备");
  151. that.startBleTimer(isFirst, true);
  152. that.addNotification()
  153. }
  154. },
  155. startBleTimer(isFirst, openTimer) {
  156. let that = this;
  157. let deviceList = that.getDeviceList()
  158. let findBle = deviceList.find(item => {
  159. return (item.connectType === 1 && item.state === "online")
  160. })
  161. let findWifi = deviceList.find(item => {
  162. return (item.connectType === 3 && item.state === "online")
  163. })
  164. if (!openTimer && (findBle || findWifi)) {
  165. // 强制开启监听 && (有蓝牙在线 || 有wifi在线)就return
  166. return;
  167. }
  168. ///再秒再对比一次
  169. that.stopIntervalId1();
  170. let count = 0;
  171. that.data.intervalId1 = setInterval(async function () {
  172. console.log("开始扫描蓝牙设备");
  173. that.compareList();
  174. // if (count > 4) {
  175. if (isFirst) {
  176. that.stopIntervalId1();
  177. var newIsFirst = false
  178. that.startBleTimer(newIsFirst, true);
  179. }
  180. // } else {
  181. // count++;
  182. // }
  183. isFirst = false;
  184. }, isFirst ? 3 * 1000 : 10 * 1000);
  185. },
  186. /// 对比蓝牙数据
  187. compareList() {
  188. let that = this;
  189. constant.lexinScan.compareList2(that.data.deviceList, function (device) {
  190. if (device) {
  191. console.log("找到蓝牙了");
  192. that.data._willConnectBle = device;
  193. getApp().getBluetoothStatus();
  194. }
  195. });
  196. // constant.lexinScan.compareList(function (list) {
  197. // console.log("对比蓝牙数据", list);
  198. // that.updateDeviceList(list, false, false);
  199. // }, function (item) {
  200. // that.addBlueDevice(item);
  201. // });
  202. },
  203. getBluetoothStatusCallck(v) {
  204. var that = this;
  205. if (!that.data._willConnectBle) {
  206. // true 走下面
  207. return;
  208. }
  209. if (v) {
  210. var device = that.data._willConnectBle;
  211. console.log("开始连接设备0", device);
  212. // if (device.state === 'online' || device.connectType != 1) {
  213. // return;
  214. // }
  215. BtHelper.getInstance().connect(device, function (isConnected, d) {
  216. console.log("自动连接成功", isConnected)
  217. if (isConnected) {
  218. device.state = 'online';
  219. that.updateDeviceList(device);
  220. that.stopIntervalId1();
  221. } else {
  222. device.state = 'offline';
  223. }
  224. });
  225. } else {
  226. // wx.showModal({
  227. // title: '温馨提示',
  228. // content: '请检查手机蓝牙是否打开',
  229. // showCancel: false,
  230. // success: function (res) { }
  231. // });
  232. }
  233. that.data._willConnectBle = null;
  234. },
  235. // 回调
  236. mqttCallback(type, option) {
  237. // console.log("gadsfadsfadsfa==999===" + type + "====" + JSON.stringify(option));
  238. var that = this;
  239. let payloads = null;
  240. try {
  241. if (option && option.payload) {
  242. payloads = JSON.parse(option.payload);
  243. };
  244. switch (type) {
  245. ///先走订阅
  246. case "connect":
  247. constant.lexinSubscribe.subscribeAllDevice();
  248. break;
  249. ///再走有在线
  250. case "message_onoffline":
  251. constant.lexinConnect.searchOnlineDevice(payloads, function (list) {
  252. that.updateDeviceList(list, false, false);
  253. that.stopIntervalId1()
  254. }, function () {
  255. var indexPage = that.data.indexPage;
  256. that.setData({
  257. actionIndex: null,
  258. deviceListSelect: null,
  259. indexPage: indexPage == 1 ? 0 : indexPage,
  260. });
  261. that.startBleTimer();
  262. }, function () {
  263. var indexPage = that.data.indexPage;
  264. that.setData({
  265. actionIndex: null,
  266. deviceListSelect: null,
  267. indexPage: indexPage == 1 ? 0 : indexPage,
  268. });
  269. that.startBleTimer();
  270. });
  271. break;
  272. case "message":
  273. if (payloads) {
  274. var payloadType = payloads.type;
  275. var other = payloads.other;
  276. ///获取频道位置
  277. if (payloadType === "get_position" && other) {
  278. constant.lexinMessage.getPosition(other, function (actionIndex) {
  279. that.setData({
  280. actionIndex: actionIndex,
  281. });
  282. });
  283. }
  284. // 获取歌曲信息
  285. else if (payloadType === "get_dev_info" && other) {
  286. that.setDeviceInfor(other);
  287. constant.lexinMessage.getDeviceInfo(payloads);
  288. }
  289. ///获取电量
  290. else if (payloadType === "battery" && other) {
  291. if (other.battery) {
  292. var battery = constant.lexinMessage.getBattery(other.battery);
  293. that.setData({
  294. battery: battery,
  295. });
  296. }
  297. }
  298. ///获取播放状态
  299. else if (payloadType === "play" || payloadType === "play_state") {
  300. constant.lexinMessage.getPlay();
  301. }
  302. }
  303. break;
  304. default:
  305. }
  306. } catch (e) {}
  307. },
  308. ///去连接设备数据
  309. actionDevice(device) {
  310. var that = this;
  311. // 取消订阅
  312. var deviceMacId = that.data.deviceMacId;
  313. if (!constant.strings.isEmpty(deviceMacId)) {
  314. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  315. constant.app.unsubscribe(pubResponse);
  316. };
  317. that.subscribeCurrDevice(device);
  318. },
  319. ///数据处理
  320. subscribeCurrDevice(device) {
  321. var that = this;
  322. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  323. if (!isOK) {
  324. constant.app.connect();
  325. setTimeout(() => {
  326. that.subscribeCurrDevice(device);
  327. }, 500);
  328. return;
  329. };
  330. var deviceId = device.deviceId;
  331. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  332. that.setData({
  333. deviceMacId: deviceMacId,
  334. });
  335. /// 订阅设备
  336. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  337. constant.app.subscribe(pubResponse);
  338. /// 获取设备信息
  339. const obj = {
  340. DstDeviceName: deviceMacId
  341. };
  342. constant.app.PubMsg({
  343. type: "get_dev_info",
  344. ...obj
  345. });
  346. },
  347. ///选择对应的频道
  348. onTapChannelIndex(e) {
  349. var that = this;
  350. var index = e.currentTarget.dataset.index;
  351. constant.lexinJump.toChannelIndex(index, function (i) {
  352. that.setData({
  353. actionIndex: i,
  354. });
  355. });
  356. },
  357. ///去频道详情
  358. onTapToChannel() {
  359. var that = this;
  360. var actionIndex = that.data.actionIndex;
  361. if (actionIndex == null) {
  362. return;
  363. }
  364. var channelData = that.getChannelData();
  365. if (channelData.length > actionIndex) {
  366. constant.store.setStore("channelDeta", channelData[actionIndex]);
  367. constant.routeUtil.jump(constant.routePath.channelDetail);
  368. }
  369. },
  370. ///选择对应的频道
  371. getchannelData(clientType) {
  372. var that = this;
  373. constant.apiUtil.listByDevice({
  374. clientType: clientType,
  375. unShowLoad: true,
  376. }).then((res) => {
  377. constant.store.setStore("channelData", res);
  378. that.setData({
  379. channelData: res,
  380. });
  381. // 接收设备当前播放状态
  382. var deviceList = that.getDeviceList();
  383. var deviceListSelect = that.getDeviceListSelect();
  384. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  385. return;
  386. };
  387. var deviceId = deviceList[deviceListSelect].deviceId;
  388. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  389. const obj = {
  390. DstDeviceName: deviceMacId
  391. }
  392. constant.app.PubMsg({
  393. type: "get_position",
  394. ...obj
  395. });
  396. })
  397. },
  398. onTapIndex(e) {
  399. var that = this;
  400. var index = e.currentTarget.dataset.index;
  401. var indexPage = that.data.indexPage;
  402. if (indexPage != index) {
  403. that.setData({
  404. indexPage: index,
  405. });
  406. }
  407. },
  408. onTapLogin() {
  409. var that = this;
  410. ///退出登录
  411. var isLogin = that.getIsLogin();
  412. if (isLogin) {
  413. that.logOut();
  414. return;
  415. }
  416. constant.routeUtil.jump(constant.routePath.login);
  417. },
  418. ///退出登录
  419. logOut() {
  420. var that = this;
  421. wx.removeStorageSync('userInfo');
  422. wx.removeStorageSync('token');
  423. var nickName = "未登录";
  424. var greeting = that.getGreetBuNickName(nickName);
  425. that.setData({
  426. isLogin: false,
  427. greeting: greeting,
  428. nickName: nickName,
  429. userPic: "./../../../img/head_pic.png",
  430. });
  431. var device = that.getCurrentDevice();
  432. if (device == null) {
  433. return;
  434. }
  435. ///有设备在线被选中,则让它不被选择
  436. if (device.connectType == 3) {
  437. var deviceMacId = that.data.deviceMacId;
  438. if (!constant.strings.isEmpty(deviceMacId)) {
  439. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  440. constant.app.unsubscribe(pubResponse);
  441. var indexPage = that.data.indexPage;
  442. that.setData({
  443. deviceMacId: null,
  444. actionIndex: null,
  445. deviceListSelect: null,
  446. indexPage: indexPage == 1 ? 0 : indexPage,
  447. });
  448. };
  449. } else {
  450. // console.log("退出登录,断开连接", deviceList);
  451. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  452. // var indexPage = that.data.indexPage;
  453. // that.setData({
  454. // deviceMacId: null,
  455. // actionIndex: null,
  456. // deviceListSelect: null,
  457. // indexPage: indexPage == 1 ? 0 : indexPage,
  458. // });
  459. }
  460. },
  461. ///点击item
  462. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  463. async onTapItem(e) {
  464. var that = this;
  465. var isLogin = that.getIsLogin();
  466. if (!isLogin) {
  467. constant.routeUtil.jump(constant.routePath.login);
  468. return;
  469. }
  470. var index = e.currentTarget.dataset.index;
  471. var device = e.currentTarget.dataset.item;
  472. var connectType = device.connectType;
  473. var deviceListSelect = that.getDeviceListSelect();
  474. // wifi只支持在线点击
  475. if (connectType == 3 && device.state === "online") {
  476. if (index === deviceListSelect) {
  477. that.goWake();
  478. } else {
  479. // 现断线第一个
  480. // that.disconnectFirst()
  481. that.addWifiDevice(device);
  482. }
  483. return;
  484. }
  485. ///去蓝牙连接处理
  486. // {"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 妙播收音机"}
  487. var deviceList = that.getDeviceList();
  488. let curDevice = deviceList[index]
  489. if (index === deviceListSelect && device.state === "online") {
  490. var param = "?param=" + JSON.stringify(curDevice);
  491. constant.routeUtil.jumpParam(constant.routePath.deviceDetail, param);
  492. } else if (device.state === "offline") {
  493. var deviceListSelect = that.getDeviceListSelect();
  494. if (deviceListSelect != null) {
  495. if (deviceList.length > deviceListSelect) {
  496. var currentDevice = deviceList[deviceListSelect];
  497. console.log("断开连接0", currentDevice)
  498. // 现断线第一个
  499. // that.disconnectFirst()
  500. }
  501. that.setData({
  502. deviceListSelect: null,
  503. });
  504. }
  505. console.log("断开连接2", device)
  506. device.name = device.devName;
  507. that.data._clickOtherDevice = true;
  508. BtHelper.getInstance().connect(device, function (isConnected, d) {
  509. that.data._clickOtherDevice = false;
  510. if (isConnected) {
  511. device.state = "online";
  512. that.addBlueDevice(device);
  513. }
  514. });
  515. } else {
  516. var deviceList = that.getDeviceList();
  517. var deviceListSelect = that.getDeviceListSelect();
  518. if (deviceListSelect != null) {
  519. if (deviceList.length > deviceListSelect) {
  520. var currentDevice = deviceList[deviceListSelect];
  521. if (currentDevice.connectType === 1) {
  522. console.log("断开连接1", currentDevice)
  523. await BtHelper.getInstance().disconnect(currentDevice);
  524. }
  525. // await time_util.delayMills(500);
  526. }
  527. that.setData({
  528. deviceListSelect: null,
  529. });
  530. }
  531. that.data._clickOtherDevice = true;
  532. device.name = device.devName;
  533. BtHelper.getInstance().connect(device, function (isConnected, d) {
  534. that.data._clickOtherDevice = false;
  535. if (isConnected) {
  536. device.state = "online";
  537. that.addBlueDevice(device);
  538. }
  539. });
  540. }
  541. },
  542. disconnectFirst() {
  543. let that = this;
  544. let deviceList = that.getDeviceList()
  545. let first = deviceList[0] ?? {}
  546. if (first.state == "online") {
  547. if (first.connectType === 1) {
  548. BtHelper.getInstance().disconnect(first)
  549. first.state == "offline"
  550. } else {
  551. // 取消订阅
  552. // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
  553. //constant.app.unsubscribe(pubResponse);
  554. }
  555. }
  556. },
  557. // 去唤醒界面
  558. goWake() {
  559. var that = this;
  560. var deviceList = that.getDeviceList();
  561. var deviceListSelect = that.getDeviceListSelect();
  562. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  563. return
  564. };
  565. var device = deviceList[deviceListSelect];
  566. var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel;
  567. constant.routeUtil.jumpParam(constant.routePath.deviceWake, param);
  568. },
  569. ///删除当前设备
  570. deleteDevice(e) {
  571. var that = this;
  572. var index = e.currentTarget.dataset.index;
  573. wx.showModal({
  574. title: '确定删除?',
  575. success: function (res) {
  576. if (res.confirm) {
  577. var deviceList = that.getDeviceList();
  578. if (deviceList[index].connectType == 3) {
  579. that.cancelWifi(index, false);
  580. } else {
  581. console.log("确定删除0", index, deviceList[index].deviceId)
  582. that.cancelBlue(index, false);
  583. }
  584. }
  585. }
  586. });
  587. },
  588. ///是否是同一个蓝牙
  589. isTheSameBlue(device) {
  590. var that = this;
  591. var deviceList = that.getDeviceList();
  592. var deviceListSelect = that.getDeviceListSelect();
  593. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  594. return false;
  595. };
  596. if (deviceList[deviceListSelect].connectType == 3) {
  597. return false;
  598. } else {
  599. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  600. return true;
  601. } else {
  602. return false;
  603. }
  604. }
  605. },
  606. ///断开当前的
  607. cancelCurrent() {
  608. var that = this;
  609. var deviceList = that.getDeviceList();
  610. var deviceListSelect = that.getDeviceListSelect();
  611. if (deviceList[deviceListSelect].connectType == 3) {
  612. that.cancelWifi(deviceListSelect, true);
  613. } else {
  614. that.cancelBlue(deviceListSelect, true);
  615. }
  616. },
  617. /// 断开蓝牙连接
  618. cancelBlue(index, onlyCancel) {
  619. var that = this;
  620. if (onlyCancel) {
  621. return;
  622. }
  623. var deviceList = that.getDeviceList();
  624. var deviceListSelect = that.getDeviceListSelect();
  625. ///是否删除的当前播放这个
  626. var isCurrentIndex = false;
  627. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  628. isCurrentIndex = (deviceListSelect == index);
  629. };
  630. let device = deviceList[index];
  631. ///删除当前设备
  632. var deviceId = device.deviceId;
  633. deviceList = deviceList.filter((item, i) => {
  634. return deviceId !== item.deviceId;
  635. });
  636. constant.store.setStore("deviceList", deviceList);
  637. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  638. var indexPage = that.data.indexPage;
  639. that.setData({
  640. deviceList: deviceList,
  641. indexPage: indexPage == 1 ? 0 : indexPage,
  642. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  643. });
  644. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  645. if (device.state === "online") {
  646. BtHelper.getInstance().disconnect({
  647. "deviceId": deviceId
  648. });
  649. }
  650. },
  651. /// 断开连接wifi
  652. cancelWifi(index, onlyCancel) {
  653. var that = this;
  654. var deviceList = that.getDeviceList();
  655. if (index === null || deviceList.length <= index) {
  656. return;
  657. };
  658. var deviceId = deviceList[index].deviceId;
  659. // 取消订阅
  660. var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
  661. constant.app.unsubscribe(pubResponse);
  662. if (onlyCancel) {
  663. return;
  664. }
  665. ///是否删除的当前播放这个
  666. var isCurrentIndex = false;
  667. var deviceListSelect = that.getDeviceListSelect();
  668. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  669. isCurrentIndex = (deviceListSelect == index);
  670. };
  671. deviceList = deviceList.filter((item, i) => {
  672. return deviceId !== item.deviceId;
  673. });
  674. constant.store.setStore("deviceList", deviceList);
  675. var indexPage = that.data.indexPage;
  676. var deviceMacId = that.data.deviceMacId;
  677. var actionIndex = that.data.actionIndex;
  678. that.setData({
  679. deviceList: deviceList,
  680. deviceMacId: isCurrentIndex ? null : deviceMacId,
  681. actionIndex: isCurrentIndex ? null : actionIndex,
  682. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  683. indexPage: indexPage == 1 ? 0 : indexPage,
  684. });
  685. },
  686. /// 新添加蓝牙设备
  687. addBlueDevice(device) {
  688. var that = this;
  689. var addDeviceList = constant.lexinAdd.addBlueDevice(device);
  690. let hasOnline = 0
  691. addDeviceList.forEach(item => {
  692. // 蓝牙设备下线。wifi不动
  693. if (item.deviceId != device.deviceId && item.connectType === 1) {
  694. item.state = "offline"
  695. }
  696. })
  697. console.log("新添加蓝牙设备", addDeviceList)
  698. that.updateDeviceList(addDeviceList, false, false);
  699. that.setData({
  700. deviceListSelect: 0,
  701. autoConnected: true,
  702. });
  703. that.addNotification()
  704. },
  705. disconnectDev(event, device) {
  706. let _this = this;
  707. if (_this.data._clickOtherDevice) {
  708. _this.data._clickOtherDevice = false
  709. return;
  710. }
  711. console.log("断开连接--", event.commonValue, event.deviceId);
  712. var selectDev = _this.getCurrentDevice()
  713. var isSelect = selectDev == null || (selectDev != null) && selectDev.deviceId === event.deviceId
  714. if (isSelect && selectDev.deviceId === event.deviceId && event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  715. _this.data._hasShowModal = true
  716. wx.showModal({
  717. title: '提示',
  718. content: '设备断开连接,请重新连接设备',
  719. showCancel: false,
  720. success: function (res) {
  721. _this.data._hasShowModal = false;
  722. if (res.confirm) {
  723. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  724. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  725. }
  726. }
  727. }
  728. })
  729. }
  730. // 设备断开连接,请重新连接设备
  731. },
  732. addNotification() {
  733. let that = this;
  734. constant.eventBus.removeNotification(CmdEvent.eventName, that);
  735. constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
  736. let deviceList = that.getDeviceList()
  737. // console.log("收到监听:", JSON.stringify(event))
  738. if (event.cmdEvent == EnumCmdEvent.btMac) {
  739. let disDevice = deviceList.find(item => {
  740. return item.state == "online" && item.connectType === 1
  741. })
  742. if (disDevice) {
  743. disDevice.mac = event.btMac
  744. console.log("设备mac:", disDevice.mac)
  745. }
  746. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  747. let isFirst = false;
  748. // 有下线就开启监听扫描
  749. if (event.commonValue == "online") {
  750. console.log("有蓝牙设备上线")
  751. that.stopIntervalId1();
  752. } else {
  753. // 有wifi设备在线
  754. let wifiDevice = deviceList.find(item => {
  755. return item.state == "online" && item.connectType === 3
  756. })
  757. if (!wifiDevice) {
  758. console.log("开启蓝牙监听:", event.deviceId, event.commonValue)
  759. that.startBleTimer(isFirst, true);
  760. }
  761. }
  762. console.log("首页ble通知:", event.deviceId, event.commonValue)
  763. let hasOnleBle = deviceList.find(item => {
  764. return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
  765. })
  766. let disDevice = deviceList.find(item => {
  767. return item.deviceId == event.deviceId && item.connectType == 1
  768. })
  769. // that.data.deviceList.forEach(element => {
  770. // if (item.deviceId == tmp.deviceId) {
  771. // }
  772. // });
  773. if (!disDevice) {
  774. // console.log("")
  775. console.log("没有找到在线的对应的蓝牙设备", JSON.stringify(deviceList))
  776. return;
  777. }
  778. disDevice.state = event.commonValue;
  779. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  780. if (event.commonValue == "online") {
  781. // BtHelper.getInstance().connect()
  782. console.log("首页连接上线", disDevice)
  783. } else {
  784. if (hasOnleBle) {
  785. that.disconnectDev(event, disDevice)
  786. }
  787. console.log("首页连接断开", disDevice)
  788. that.updateDeviceList(deviceList, false, false);
  789. }
  790. }
  791. }, that);
  792. },
  793. // 新添加Wifi设备
  794. addWifiDevice(device) {
  795. var that = this;
  796. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  797. if (!isOK) {
  798. constant.app.connect();
  799. setTimeout(() => {
  800. that.addWifiDevice(device);
  801. }, 500);
  802. return;
  803. };
  804. console.log("有新的wifi设备0", device)
  805. var deviceId = device.deviceId;
  806. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  807. console.log("有新的wifi设备", addDeviceList)
  808. that.updateDeviceList(addDeviceList, false, false);
  809. var indexPage = that.data.indexPage;
  810. that.setData({
  811. deviceListSelect: null,
  812. connectDeviceIding: deviceId,
  813. indexPage: indexPage == 1 ? 0 : indexPage,
  814. });
  815. constant.lexinSubscribe.subscribeSingleDevice(device);
  816. },
  817. refreshCurrentDevice(device) {
  818. var that = this;
  819. console.log("刷新列表0:", device)
  820. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  821. console.log("刷新列表1:", addDeviceList)
  822. that.updateDeviceList(addDeviceList, false, false);
  823. that.setData({
  824. autoConnected: true,
  825. deviceListSelect: 0,
  826. connectDeviceIding: "",
  827. });
  828. },
  829. /// 手机关闭蓝牙,所有蓝牙设备离线
  830. closeBlueResetOffline(isInit, closeAllBlue) {
  831. var that = this;
  832. var deviceList = that.getDeviceList();
  833. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  834. },
  835. /// 更新列表排序
  836. updateDeviceList(deviceList, isInit, closeAllBlue) {
  837. var that = this;
  838. var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
  839. constant.store.setStore("deviceList", finalList);
  840. that.setData({
  841. deviceList: finalList,
  842. });
  843. },
  844. ///点击banner事件
  845. onTapBanner(e) {
  846. var item = e.currentTarget.dataset.item;
  847. try {
  848. if (constant.strings.isEmpty(item.forwardUrl)) {
  849. return;
  850. }
  851. // 内链
  852. if (item.forwardType == 0) {
  853. var param = "?param=" + item.forwardUrl;
  854. constant.app.webSkipUrl = item.forwardUrl;
  855. constant.routeUtil.jumpParam(constant.routePath.webview, param);
  856. }
  857. // 外链
  858. else if (item.forwardType == 1) {
  859. } else {
  860. }
  861. } catch (err) {
  862. console.log("跳转失败", err)
  863. }
  864. },
  865. ///去充值内容会员
  866. toRecharge(e) {
  867. var device = e.currentTarget.dataset.item;
  868. },
  869. ///添加设备
  870. jumpToAddDevice() {
  871. var that = this;
  872. var isLogin = that.getIsLogin();
  873. if (!isLogin) {
  874. var param = "?toDeviceList=" + true;
  875. constant.routeUtil.jumpParam(constant.routePath.login, param);
  876. return;
  877. }
  878. constant.routeUtil.jump(constant.routePath.deviceList);
  879. },
  880. // 关于我们
  881. jumpToAboutUs() {
  882. constant.lexinJump.toAboutUs();
  883. },
  884. /// 是否已登录
  885. getIsLogin() {
  886. var that = this;
  887. var isLogin = that.data.isLogin;
  888. return isLogin;
  889. },
  890. /// 获取列表数据
  891. getDeviceList() {
  892. var that = this;
  893. var deviceList = that.data.deviceList;
  894. return deviceList;
  895. },
  896. ///获取当前选中的
  897. getDeviceListSelect() {
  898. var that = this;
  899. var deviceListSelect = that.data.deviceListSelect;
  900. return deviceListSelect;
  901. },
  902. ///获取是否已经第一次连接过
  903. getAutoConnected() {
  904. var that = this;
  905. var autoConnected = that.data.autoConnected;
  906. return autoConnected;
  907. },
  908. ///当前正在连接的设备id
  909. getConnectDeviceIding() {
  910. var that = this;
  911. var connectDeviceIding = that.data.connectDeviceIding
  912. return connectDeviceIding;
  913. },
  914. ///获取频道数据
  915. getChannelData() {
  916. var that = this;
  917. var channelData = that.data.channelData;
  918. return channelData;
  919. },
  920. /// 当前播放频道
  921. getActionIndex() {
  922. var that = this;
  923. var actionIndex = that.data.actionIndex;
  924. return actionIndex;
  925. },
  926. /// 获取当前的device
  927. getCurrentDevice() {
  928. var that = this;
  929. var deviceList = that.getDeviceList();
  930. var deviceListSelect = that.getDeviceListSelect();
  931. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  932. return null;
  933. };
  934. var device = deviceList[deviceListSelect];
  935. return device;
  936. },
  937. /// 获取当前的deviceId
  938. getCurrentDeviceId() {
  939. var that = this;
  940. var deviceList = that.getDeviceList();
  941. var deviceListSelect = that.getDeviceListSelect();
  942. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  943. return "";
  944. };
  945. var deviceId = deviceList[deviceListSelect].deviceId;
  946. return deviceId;
  947. },
  948. ///设置设备相关信息
  949. setDeviceInfor(other) {
  950. var that = this;
  951. // 接收设备当前信息
  952. that.getchannelData(other.ProdModel);
  953. // 电量
  954. if (other.Power) {
  955. var battery = constant.lexinMessage.getBattery(other.Power);
  956. that.setData({
  957. battery: battery,
  958. });
  959. }
  960. // 当前设备木有设置定时
  961. that.setData({
  962. isSetWake: false,
  963. });
  964. other.alarm.map((v) => {
  965. if (v.enable === "1") {
  966. that.setData({
  967. isSetWake: true
  968. });
  969. }
  970. });
  971. },
  972. ///******************************* 可折叠 ********************************************///
  973. onUserInfoLoad() {
  974. var that = this;
  975. var userInfo = wx.getStorageSync("userInfo") || "";
  976. if (!constant.strings.isEmpty(userInfo)) {
  977. var phone = userInfo.phone;
  978. if (!phone) {
  979. return;
  980. };
  981. var nickName = userInfo.nickname || "";
  982. var greeting = that.getGreetBuNickName(nickName);
  983. // 获取缓存的频道数据
  984. var channelData = wx.getStorageSync("channelData") || "";
  985. if (!constant.strings.isEmpty(channelData)) {
  986. that.setData({
  987. channelData: channelData,
  988. greeting: greeting,
  989. nickName: nickName,
  990. userPic: userInfo.headUrl || "",
  991. isLogin: true,
  992. })
  993. } else {
  994. that.setData({
  995. greeting: greeting,
  996. nickName: nickName,
  997. userPic: userInfo.headUrl || "",
  998. isLogin: true,
  999. });
  1000. }
  1001. } else {
  1002. var greeting = that.getGreetBuNickName("");
  1003. that.setData({
  1004. greeting: greeting,
  1005. })
  1006. }
  1007. },
  1008. getGreetBuNickName(nickName) {
  1009. var greeting = constant.timeUtil.getGreet();
  1010. if (nickName != "" && nickName != "未登录") {
  1011. greeting = greeting + ',' + nickName;;
  1012. }
  1013. return greeting;
  1014. },
  1015. onBannerLoad() {
  1016. var that = this;
  1017. constant.apiUtil.getBanner({}).then((res) => {
  1018. that.setData({
  1019. bannerList: res,
  1020. });
  1021. constant.store.setStore("homeBanner", res);
  1022. });
  1023. },
  1024. stopIntervalId1: function () {
  1025. var that = this;
  1026. if (!constant.strings.isEmpty(that.data.intervalId1)) {
  1027. clearInterval(that.data.intervalId1);
  1028. that.data.intervalId1 = null;
  1029. }
  1030. },
  1031. stopIntervalId2: function () {
  1032. var that = this;
  1033. if (!constant.strings.isEmpty(that.data.intervalId2)) {
  1034. clearInterval(that.data.intervalId2);
  1035. that.data.intervalId2 = null;
  1036. }
  1037. },
  1038. ///销毁蓝牙
  1039. onUnload() {
  1040. var that = this;
  1041. constant.eventBus.removeNotification(CmdEvent.eventName, that)
  1042. console.log("界面onUnload")
  1043. that.stopIntervalId1();
  1044. that.stopIntervalId2();
  1045. if (!constant.strings.isEmpty(that.data.intervalId)) {
  1046. clearInterval(that.data.intervalId);
  1047. that.data.intervalId = null;
  1048. }
  1049. BtHelper.getInstance().disconnect();
  1050. if (constant.app.globalData.client === null) {
  1051. return;
  1052. };
  1053. constant.app.globalData.client.end(true);
  1054. constant.app.globalData.client = null;
  1055. },
  1056. })