index.js 34 KB

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