index.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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.addBlueDevice(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. }, function () {
  254. var indexPage = that.data.indexPage;
  255. that.setData({
  256. actionIndex: null,
  257. deviceListSelect: null,
  258. indexPage: indexPage == 1 ? 0 : indexPage,
  259. });
  260. }, function () {
  261. var indexPage = that.data.indexPage;
  262. that.setData({
  263. actionIndex: null,
  264. deviceListSelect: null,
  265. indexPage: indexPage == 1 ? 0 : indexPage,
  266. });
  267. });
  268. break;
  269. case "message":
  270. if (payloads) {
  271. var payloadType = payloads.type;
  272. var other = payloads.other;
  273. ///获取频道位置
  274. if (payloadType === "get_position" && other) {
  275. constant.lexinMessage.getPosition(other, function (actionIndex) {
  276. that.setData({
  277. actionIndex: actionIndex,
  278. });
  279. });
  280. }
  281. // 获取歌曲信息
  282. else if (payloadType === "get_dev_info" && other) {
  283. that.setDeviceInfor(other);
  284. constant.lexinMessage.getDeviceInfo(payloads);
  285. }
  286. ///获取电量
  287. else if (payloadType === "battery" && other) {
  288. if (other.battery) {
  289. var battery = constant.lexinMessage.getBattery(other.battery);
  290. that.setData({
  291. battery: battery,
  292. });
  293. }
  294. }
  295. ///获取播放状态
  296. else if (payloadType === "play" || payloadType === "play_state") {
  297. constant.lexinMessage.getPlay();
  298. }
  299. }
  300. break;
  301. default:
  302. }
  303. } catch (e) {}
  304. },
  305. ///去连接设备数据
  306. actionDevice(device) {
  307. var that = this;
  308. // 取消订阅
  309. var deviceMacId = that.data.deviceMacId;
  310. if (!constant.strings.isEmpty(deviceMacId)) {
  311. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  312. constant.app.unsubscribe(pubResponse);
  313. };
  314. that.subscribeCurrDevice(device);
  315. },
  316. ///数据处理
  317. subscribeCurrDevice(device) {
  318. var that = this;
  319. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  320. if (!isOK) {
  321. constant.app.connect();
  322. setTimeout(() => {
  323. that.subscribeCurrDevice(device);
  324. }, 500);
  325. return;
  326. };
  327. var deviceId = device.deviceId;
  328. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  329. that.setData({
  330. deviceMacId: deviceMacId,
  331. });
  332. /// 订阅设备
  333. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  334. constant.app.subscribe(pubResponse);
  335. /// 获取设备信息
  336. const obj = {
  337. DstDeviceName: deviceMacId
  338. };
  339. constant.app.PubMsg({
  340. type: "get_dev_info",
  341. ...obj
  342. });
  343. },
  344. ///选择对应的频道
  345. onTapChannelIndex(e) {
  346. var that = this;
  347. var index = e.currentTarget.dataset.index;
  348. constant.lexinJump.toChannelIndex(index, function (i) {
  349. that.setData({
  350. actionIndex: i,
  351. });
  352. });
  353. },
  354. ///去频道详情
  355. onTapToChannel() {
  356. var that = this;
  357. var actionIndex = that.data.actionIndex;
  358. if (actionIndex == null) {
  359. return;
  360. }
  361. var channelData = that.getChannelData();
  362. if (channelData.length > actionIndex) {
  363. constant.store.setStore("channelDeta", channelData[actionIndex]);
  364. constant.routeUtil.jump(constant.routePath.channelDetail);
  365. }
  366. },
  367. ///选择对应的频道
  368. getchannelData(clientType) {
  369. var that = this;
  370. constant.apiUtil.listByDevice({
  371. clientType: clientType,
  372. unShowLoad: true,
  373. }).then((res) => {
  374. constant.store.setStore("channelData", res);
  375. that.setData({
  376. channelData: res,
  377. });
  378. // 接收设备当前播放状态
  379. var deviceList = that.getDeviceList();
  380. var deviceListSelect = that.getDeviceListSelect();
  381. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  382. return;
  383. };
  384. var deviceId = deviceList[deviceListSelect].deviceId;
  385. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  386. const obj = {
  387. DstDeviceName: deviceMacId
  388. }
  389. constant.app.PubMsg({
  390. type: "get_position",
  391. ...obj
  392. });
  393. })
  394. },
  395. onTapIndex(e) {
  396. var that = this;
  397. var index = e.currentTarget.dataset.index;
  398. var indexPage = that.data.indexPage;
  399. if (indexPage != index) {
  400. that.setData({
  401. indexPage: index,
  402. });
  403. }
  404. },
  405. onTapLogin() {
  406. var that = this;
  407. ///退出登录
  408. var isLogin = that.getIsLogin();
  409. if (isLogin) {
  410. that.logOut();
  411. return;
  412. }
  413. constant.routeUtil.jump(constant.routePath.login);
  414. },
  415. ///退出登录
  416. logOut() {
  417. var that = this;
  418. wx.removeStorageSync('userInfo');
  419. wx.removeStorageSync('token');
  420. var nickName = "未登录";
  421. var greeting = that.getGreetBuNickName(nickName);
  422. that.setData({
  423. isLogin: false,
  424. greeting: greeting,
  425. nickName: nickName,
  426. userPic: "./../../../img/head_pic.png",
  427. });
  428. var device = that.getCurrentDevice();
  429. if (device == null) {
  430. return;
  431. }
  432. ///有设备在线被选中,则让它不被选择
  433. if (device.connectType == 3) {
  434. var deviceMacId = that.data.deviceMacId;
  435. if (!constant.strings.isEmpty(deviceMacId)) {
  436. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  437. constant.app.unsubscribe(pubResponse);
  438. var indexPage = that.data.indexPage;
  439. that.setData({
  440. deviceMacId: null,
  441. actionIndex: null,
  442. deviceListSelect: null,
  443. indexPage: indexPage == 1 ? 0 : indexPage,
  444. });
  445. };
  446. } else {
  447. // console.log("退出登录,断开连接", deviceList);
  448. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  449. // var indexPage = that.data.indexPage;
  450. // that.setData({
  451. // deviceMacId: null,
  452. // actionIndex: null,
  453. // deviceListSelect: null,
  454. // indexPage: indexPage == 1 ? 0 : indexPage,
  455. // });
  456. }
  457. },
  458. ///点击item
  459. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  460. async onTapItem(e) {
  461. var that = this;
  462. var isLogin = that.getIsLogin();
  463. if (!isLogin) {
  464. constant.routeUtil.jump(constant.routePath.login);
  465. return;
  466. }
  467. var index = e.currentTarget.dataset.index;
  468. var device = e.currentTarget.dataset.item;
  469. var connectType = device.connectType;
  470. var deviceListSelect = that.getDeviceListSelect();
  471. // wifi只支持在线点击
  472. if (connectType == 3 && device.state === "online") {
  473. if (index === deviceListSelect) {
  474. that.goWake();
  475. } else {
  476. // 现断线第一个
  477. // that.disconnectFirst()
  478. that.addWifiDevice(device);
  479. }
  480. return;
  481. }
  482. ///去蓝牙连接处理
  483. // {"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 妙播收音机"}
  484. var deviceList = that.getDeviceList();
  485. let curDevice = deviceList[index]
  486. if (index === deviceListSelect && device.state === "online") {
  487. var param = "?param=" + JSON.stringify(curDevice);
  488. constant.routeUtil.jumpParam(constant.routePath.deviceDetail, param);
  489. } else if (device.state === "offline") {
  490. var deviceListSelect = that.getDeviceListSelect();
  491. if (deviceListSelect != null) {
  492. if (deviceList.length > deviceListSelect) {
  493. var currentDevice = deviceList[deviceListSelect];
  494. console.log("断开连接0", currentDevice)
  495. // 现断线第一个
  496. // that.disconnectFirst()
  497. }
  498. that.setData({
  499. deviceListSelect: null,
  500. });
  501. }
  502. console.log("断开连接2", device)
  503. device.name = device.devName;
  504. that.data._clickOtherDevice = true;
  505. BtHelper.getInstance().connect(device, function (isConnected, d) {
  506. that.data._clickOtherDevice = false;
  507. if (isConnected) {
  508. device.state = "online";
  509. that.addBlueDevice(device);
  510. }
  511. });
  512. } else {
  513. var deviceList = that.getDeviceList();
  514. var deviceListSelect = that.getDeviceListSelect();
  515. if (deviceListSelect != null) {
  516. if (deviceList.length > deviceListSelect) {
  517. var currentDevice = deviceList[deviceListSelect];
  518. if (currentDevice.connectType === 1) {
  519. console.log("断开连接1", currentDevice)
  520. await BtHelper.getInstance().disconnect(currentDevice);
  521. }
  522. // await time_util.delayMills(500);
  523. }
  524. that.setData({
  525. deviceListSelect: null,
  526. });
  527. }
  528. that.data._clickOtherDevice = true;
  529. device.name = device.devName;
  530. BtHelper.getInstance().connect(device, function (isConnected, d) {
  531. that.data._clickOtherDevice = false;
  532. if (isConnected) {
  533. device.state = "online";
  534. that.addBlueDevice(device);
  535. }
  536. });
  537. }
  538. },
  539. disconnectFirst() {
  540. let that = this;
  541. let deviceList = that.getDeviceList()
  542. let first = deviceList[0] ?? {}
  543. if (first.state == "online") {
  544. if (first.connectType === 1) {
  545. BtHelper.getInstance().disconnect(first)
  546. first.state == "offline"
  547. } else {
  548. // 取消订阅
  549. // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
  550. //constant.app.unsubscribe(pubResponse);
  551. }
  552. }
  553. },
  554. // 去唤醒界面
  555. goWake() {
  556. var that = this;
  557. var deviceList = that.getDeviceList();
  558. var deviceListSelect = that.getDeviceListSelect();
  559. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  560. return
  561. };
  562. var device = deviceList[deviceListSelect];
  563. var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel;
  564. constant.routeUtil.jumpParam(constant.routePath.deviceWake, param);
  565. },
  566. ///删除当前设备
  567. deleteDevice(e) {
  568. var that = this;
  569. var index = e.currentTarget.dataset.index;
  570. wx.showModal({
  571. title: '确定删除?',
  572. success: function (res) {
  573. if (res.confirm) {
  574. var deviceList = that.getDeviceList();
  575. if (deviceList[index].connectType == 3) {
  576. that.cancelWifi(index, false);
  577. } else {
  578. console.log("确定删除0", index, deviceList[index].deviceId)
  579. that.cancelBlue(index, false);
  580. }
  581. }
  582. }
  583. });
  584. },
  585. ///是否是同一个蓝牙
  586. isTheSameBlue(device) {
  587. var that = this;
  588. var deviceList = that.getDeviceList();
  589. var deviceListSelect = that.getDeviceListSelect();
  590. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  591. return false;
  592. };
  593. if (deviceList[deviceListSelect].connectType == 3) {
  594. return false;
  595. } else {
  596. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  597. return true;
  598. } else {
  599. return false;
  600. }
  601. }
  602. },
  603. ///断开当前的
  604. cancelCurrent() {
  605. var that = this;
  606. var deviceList = that.getDeviceList();
  607. var deviceListSelect = that.getDeviceListSelect();
  608. if (deviceList[deviceListSelect].connectType == 3) {
  609. that.cancelWifi(deviceListSelect, true);
  610. } else {
  611. that.cancelBlue(deviceListSelect, true);
  612. }
  613. },
  614. /// 断开蓝牙连接
  615. cancelBlue(index, onlyCancel) {
  616. var that = this;
  617. if (onlyCancel) {
  618. return;
  619. }
  620. var deviceList = that.getDeviceList();
  621. var deviceListSelect = that.getDeviceListSelect();
  622. ///是否删除的当前播放这个
  623. var isCurrentIndex = false;
  624. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  625. isCurrentIndex = (deviceListSelect == index);
  626. };
  627. let device = deviceList[index];
  628. ///删除当前设备
  629. var deviceId = device.deviceId;
  630. deviceList = deviceList.filter((item, i) => {
  631. return deviceId !== item.deviceId;
  632. });
  633. constant.store.setStore("deviceList", deviceList);
  634. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  635. var indexPage = that.data.indexPage;
  636. that.setData({
  637. deviceList: deviceList,
  638. indexPage: indexPage == 1 ? 0 : indexPage,
  639. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  640. });
  641. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  642. if (device.state === "online") {
  643. BtHelper.getInstance().disconnect({
  644. "deviceId": deviceId
  645. });
  646. }
  647. },
  648. /// 断开连接wifi
  649. cancelWifi(index, onlyCancel) {
  650. var that = this;
  651. var deviceList = that.getDeviceList();
  652. if (index === null || deviceList.length <= index) {
  653. return;
  654. };
  655. var deviceId = deviceList[index].deviceId;
  656. // 取消订阅
  657. var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
  658. constant.app.unsubscribe(pubResponse);
  659. if (onlyCancel) {
  660. return;
  661. }
  662. ///是否删除的当前播放这个
  663. var isCurrentIndex = false;
  664. var deviceListSelect = that.getDeviceListSelect();
  665. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  666. isCurrentIndex = (deviceListSelect == index);
  667. };
  668. deviceList = deviceList.filter((item, i) => {
  669. return deviceId !== item.deviceId;
  670. });
  671. constant.store.setStore("deviceList", deviceList);
  672. var indexPage = that.data.indexPage;
  673. var deviceMacId = that.data.deviceMacId;
  674. var actionIndex = that.data.actionIndex;
  675. that.setData({
  676. deviceList: deviceList,
  677. deviceMacId: isCurrentIndex ? null : deviceMacId,
  678. actionIndex: isCurrentIndex ? null : actionIndex,
  679. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  680. indexPage: indexPage == 1 ? 0 : indexPage,
  681. });
  682. },
  683. /// 新添加蓝牙设备
  684. addBlueDevice(device) {
  685. var that = this;
  686. var addDeviceList = constant.lexinAdd.addBlueDevice(device);
  687. let hasOnline = 0
  688. addDeviceList.forEach(item => {
  689. // 蓝牙设备下线。wifi不动
  690. if (item.deviceId != device.deviceId && item.connectType === 1) {
  691. item.state = "offline"
  692. }
  693. })
  694. console.log("新添加蓝牙设备", addDeviceList)
  695. that.updateDeviceList(addDeviceList, false, false);
  696. that.setData({
  697. deviceListSelect: 0,
  698. autoConnected: true,
  699. });
  700. that.addNotification()
  701. },
  702. disconnectDev(event, device) {
  703. let _this = this;
  704. if (_this.data._clickOtherDevice) {
  705. _this.data._clickOtherDevice = false
  706. return;
  707. }
  708. console.log("断开连接--", event.commonValue, event.deviceId);
  709. if (event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  710. _this.data._hasShowModal = true
  711. wx.showModal({
  712. title: '提示',
  713. content: '设备断开连接,请重新连接设备',
  714. showCancel: false,
  715. success: function (res) {
  716. _this.data._hasShowModal = false;
  717. if (res.confirm) {
  718. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  719. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  720. }
  721. }
  722. }
  723. })
  724. }
  725. // 设备断开连接,请重新连接设备
  726. },
  727. addNotification() {
  728. let that = this;
  729. constant.eventBus.removeNotification(CmdEvent.eventName, that);
  730. constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
  731. let deviceList = that.getDeviceList()
  732. // console.log("收到监听:", JSON.stringify(event))
  733. if (event.cmdEvent == EnumCmdEvent.btMac) {
  734. let disDevice = deviceList.find(item => {
  735. return item.state == "online" && item.connectType === 1
  736. })
  737. if (disDevice) {
  738. disDevice.mac = event.btMac
  739. console.log("设备mac:", disDevice.mac)
  740. }
  741. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  742. let isFirst = false;
  743. // 有下线就开启监听扫描
  744. if (event.commonValue == "online") {
  745. console.log("有蓝牙设备上线")
  746. that.stopIntervalId1();
  747. } else {
  748. // 有wifi设备在线
  749. let wifiDevice = deviceList.find(item => {
  750. return item.state == "online" && item.connectType === 3
  751. })
  752. if (!wifiDevice) {
  753. console.log("开启蓝牙监听:", event.deviceId, event.commonValue)
  754. that.startBleTimer(isFirst, true);
  755. }
  756. }
  757. console.log("首页ble通知:", event.deviceId, event.commonValue)
  758. let hasOnleBle = deviceList.find(item => {
  759. return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
  760. })
  761. let disDevice = deviceList.find(item => {
  762. return item.deviceId == event.deviceId && item.connectType == 1
  763. })
  764. // that.data.deviceList.forEach(element => {
  765. // if (item.deviceId == tmp.deviceId) {
  766. // }
  767. // });
  768. if (!disDevice) {
  769. // console.log("")
  770. console.log("没有找到在线的对应的蓝牙设备", JSON.stringify(deviceList))
  771. return;
  772. }
  773. disDevice.state = event.commonValue;
  774. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  775. if (event.commonValue == "online") {
  776. // BtHelper.getInstance().connect()
  777. console.log("首页连接上线", disDevice)
  778. } else {
  779. if (hasOnleBle) {
  780. that.disconnectDev(event, disDevice)
  781. }
  782. console.log("首页连接断开", disDevice)
  783. that.updateDeviceList(deviceList, false, false);
  784. }
  785. }
  786. }, that);
  787. },
  788. // 新添加Wifi设备
  789. addWifiDevice(device) {
  790. var that = this;
  791. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  792. if (!isOK) {
  793. constant.app.connect();
  794. setTimeout(() => {
  795. that.addWifiDevice(device);
  796. }, 500);
  797. return;
  798. };
  799. console.log("有新的wifi设备0", device)
  800. var deviceId = device.deviceId;
  801. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  802. console.log("有新的wifi设备", addDeviceList)
  803. that.updateDeviceList(addDeviceList, false, false);
  804. var indexPage = that.data.indexPage;
  805. that.setData({
  806. deviceListSelect: null,
  807. connectDeviceIding: deviceId,
  808. indexPage: indexPage == 1 ? 0 : indexPage,
  809. });
  810. constant.lexinSubscribe.subscribeSingleDevice(device);
  811. },
  812. refreshCurrentDevice(device) {
  813. var that = this;
  814. console.log("刷新列表0:", device)
  815. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  816. console.log("刷新列表1:", addDeviceList)
  817. that.updateDeviceList(addDeviceList, false, false);
  818. that.setData({
  819. autoConnected: true,
  820. deviceListSelect: 0,
  821. connectDeviceIding: "",
  822. });
  823. },
  824. /// 手机关闭蓝牙,所有蓝牙设备离线
  825. closeBlueResetOffline(isInit, closeAllBlue) {
  826. var that = this;
  827. var deviceList = that.getDeviceList();
  828. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  829. },
  830. /// 更新列表排序
  831. updateDeviceList(deviceList, isInit, closeAllBlue) {
  832. var that = this;
  833. var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
  834. constant.store.setStore("deviceList", finalList);
  835. that.setData({
  836. deviceList: finalList,
  837. });
  838. },
  839. ///点击banner事件
  840. onTapBanner(e) {
  841. var item = e.currentTarget.dataset.item;
  842. try {
  843. if (constant.strings.isEmpty(item.forwardUrl)) {
  844. return;
  845. }
  846. // 内链
  847. if (item.forwardType == 0) {
  848. var param = "?param=" + item.forwardUrl;
  849. constant.app.webSkipUrl = item.forwardUrl;
  850. constant.routeUtil.jumpParam(constant.routePath.webview, param);
  851. }
  852. // 外链
  853. else if (item.forwardType == 1) {
  854. } else {
  855. }
  856. } catch (err) {
  857. console.log("跳转失败", err)
  858. }
  859. },
  860. ///去充值内容会员
  861. toRecharge(e) {
  862. var device = e.currentTarget.dataset.item;
  863. },
  864. ///添加设备
  865. jumpToAddDevice() {
  866. var that = this;
  867. var isLogin = that.getIsLogin();
  868. if (!isLogin) {
  869. var param = "?toDeviceList=" + true;
  870. constant.routeUtil.jumpParam(constant.routePath.login, param);
  871. return;
  872. }
  873. constant.routeUtil.jump(constant.routePath.deviceList);
  874. },
  875. // 关于我们
  876. jumpToAboutUs() {
  877. constant.lexinJump.toAboutUs();
  878. },
  879. /// 是否已登录
  880. getIsLogin() {
  881. var that = this;
  882. var isLogin = that.data.isLogin;
  883. return isLogin;
  884. },
  885. /// 获取列表数据
  886. getDeviceList() {
  887. var that = this;
  888. var deviceList = that.data.deviceList;
  889. return deviceList;
  890. },
  891. ///获取当前选中的
  892. getDeviceListSelect() {
  893. var that = this;
  894. var deviceListSelect = that.data.deviceListSelect;
  895. return deviceListSelect;
  896. },
  897. ///获取是否已经第一次连接过
  898. getAutoConnected() {
  899. var that = this;
  900. var autoConnected = that.data.autoConnected;
  901. return autoConnected;
  902. },
  903. ///当前正在连接的设备id
  904. getConnectDeviceIding() {
  905. var that = this;
  906. var connectDeviceIding = that.data.connectDeviceIding
  907. return connectDeviceIding;
  908. },
  909. ///获取频道数据
  910. getChannelData() {
  911. var that = this;
  912. var channelData = that.data.channelData;
  913. return channelData;
  914. },
  915. /// 当前播放频道
  916. getActionIndex() {
  917. var that = this;
  918. var actionIndex = that.data.actionIndex;
  919. return actionIndex;
  920. },
  921. /// 获取当前的device
  922. getCurrentDevice() {
  923. var that = this;
  924. var deviceList = that.getDeviceList();
  925. var deviceListSelect = that.getDeviceListSelect();
  926. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  927. return null;
  928. };
  929. var device = deviceList[deviceListSelect];
  930. return device;
  931. },
  932. /// 获取当前的deviceId
  933. getCurrentDeviceId() {
  934. var that = this;
  935. var deviceList = that.getDeviceList();
  936. var deviceListSelect = that.getDeviceListSelect();
  937. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  938. return "";
  939. };
  940. var deviceId = deviceList[deviceListSelect].deviceId;
  941. return deviceId;
  942. },
  943. ///设置设备相关信息
  944. setDeviceInfor(other) {
  945. var that = this;
  946. // 接收设备当前信息
  947. that.getchannelData(other.ProdModel);
  948. // 电量
  949. if (other.Power) {
  950. var battery = constant.lexinMessage.getBattery(other.Power);
  951. that.setData({
  952. battery: battery,
  953. });
  954. }
  955. // 当前设备木有设置定时
  956. that.setData({
  957. isSetWake: false,
  958. });
  959. other.alarm.map((v) => {
  960. if (v.enable === "1") {
  961. that.setData({
  962. isSetWake: true
  963. });
  964. }
  965. });
  966. },
  967. ///******************************* 可折叠 ********************************************///
  968. onUserInfoLoad() {
  969. var that = this;
  970. var userInfo = wx.getStorageSync("userInfo") || "";
  971. if (!constant.strings.isEmpty(userInfo)) {
  972. var phone = userInfo.phone;
  973. if (!phone) {
  974. return;
  975. };
  976. var nickName = userInfo.nickname || "";
  977. var greeting = that.getGreetBuNickName(nickName);
  978. // 获取缓存的频道数据
  979. var channelData = wx.getStorageSync("channelData") || "";
  980. if (!constant.strings.isEmpty(channelData)) {
  981. that.setData({
  982. channelData: channelData,
  983. greeting: greeting,
  984. nickName: nickName,
  985. userPic: userInfo.headUrl || "",
  986. isLogin: true,
  987. })
  988. } else {
  989. that.setData({
  990. greeting: greeting,
  991. nickName: nickName,
  992. userPic: userInfo.headUrl || "",
  993. isLogin: true,
  994. });
  995. }
  996. } else {
  997. var greeting = that.getGreetBuNickName("");
  998. that.setData({
  999. greeting: greeting,
  1000. })
  1001. }
  1002. },
  1003. getGreetBuNickName(nickName) {
  1004. var greeting = constant.timeUtil.getGreet();
  1005. if (nickName != "" && nickName != "未登录") {
  1006. greeting = greeting + ',' + nickName;;
  1007. }
  1008. return greeting;
  1009. },
  1010. onBannerLoad() {
  1011. var that = this;
  1012. constant.apiUtil.getBanner({}).then((res) => {
  1013. that.setData({
  1014. bannerList: res,
  1015. });
  1016. constant.store.setStore("homeBanner", res);
  1017. });
  1018. },
  1019. stopIntervalId1: function () {
  1020. var that = this;
  1021. if (!constant.strings.isEmpty(that.data.intervalId1)) {
  1022. clearInterval(that.data.intervalId1);
  1023. that.data.intervalId1 = null;
  1024. }
  1025. },
  1026. stopIntervalId2: function () {
  1027. var that = this;
  1028. if (!constant.strings.isEmpty(that.data.intervalId2)) {
  1029. clearInterval(that.data.intervalId2);
  1030. that.data.intervalId2 = null;
  1031. }
  1032. },
  1033. ///销毁蓝牙
  1034. onUnload() {
  1035. var that = this;
  1036. constant.eventBus.removeNotification(CmdEvent.eventName, that)
  1037. console.log("界面onUnload")
  1038. that.stopIntervalId1();
  1039. that.stopIntervalId2();
  1040. if (!constant.strings.isEmpty(that.data.intervalId)) {
  1041. clearInterval(that.data.intervalId);
  1042. that.data.intervalId = null;
  1043. }
  1044. BtHelper.getInstance().disconnect();
  1045. if (constant.app.globalData.client === null) {
  1046. return;
  1047. };
  1048. constant.app.globalData.client.end(true);
  1049. constant.app.globalData.client = null;
  1050. },
  1051. })