index.js 33 KB

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