index.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. console.log("gadfeqreqrqr==000==" + JSON.stringify(item));
  843. try {
  844. if (constant.strings.isEmpty(item.forwardUrl)) {
  845. return;
  846. }
  847. // 内链,暂时不支持
  848. if (item.forwardType == 0) {
  849. var param = "?param=" + item.forwardUrl;
  850. console.log("gadfeqreqrqr==111==" + param);
  851. constant.routeUtil.jumpParam(constant.routePath.webview, param);
  852. //
  853. }
  854. // 外链
  855. else if (item.forwardType == 1) {
  856. } else {
  857. }
  858. } catch (err) {
  859. console.log("跳转失败", err)
  860. }
  861. },
  862. ///去充值内容会员
  863. toRecharge(e) {
  864. var device = e.currentTarget.dataset.item;
  865. },
  866. ///添加设备
  867. jumpToAddDevice() {
  868. var that = this;
  869. var isLogin = that.getIsLogin();
  870. if (!isLogin) {
  871. var param = "?toDeviceList=" + true;
  872. constant.routeUtil.jumpParam(constant.routePath.login, param);
  873. return;
  874. }
  875. constant.routeUtil.jump(constant.routePath.deviceList);
  876. },
  877. // 关于我们
  878. jumpToAboutUs() {
  879. constant.lexinJump.toAboutUs();
  880. },
  881. /// 是否已登录
  882. getIsLogin() {
  883. var that = this;
  884. var isLogin = that.data.isLogin;
  885. return isLogin;
  886. },
  887. /// 获取列表数据
  888. getDeviceList() {
  889. var that = this;
  890. var deviceList = that.data.deviceList;
  891. return deviceList;
  892. },
  893. ///获取当前选中的
  894. getDeviceListSelect() {
  895. var that = this;
  896. var deviceListSelect = that.data.deviceListSelect;
  897. return deviceListSelect;
  898. },
  899. ///获取是否已经第一次连接过
  900. getAutoConnected() {
  901. var that = this;
  902. var autoConnected = that.data.autoConnected;
  903. return autoConnected;
  904. },
  905. ///当前正在连接的设备id
  906. getConnectDeviceIding() {
  907. var that = this;
  908. var connectDeviceIding = that.data.connectDeviceIding
  909. return connectDeviceIding;
  910. },
  911. ///获取频道数据
  912. getChannelData() {
  913. var that = this;
  914. var channelData = that.data.channelData;
  915. return channelData;
  916. },
  917. /// 当前播放频道
  918. getActionIndex() {
  919. var that = this;
  920. var actionIndex = that.data.actionIndex;
  921. return actionIndex;
  922. },
  923. /// 获取当前的device
  924. getCurrentDevice() {
  925. var that = this;
  926. var deviceList = that.getDeviceList();
  927. var deviceListSelect = that.getDeviceListSelect();
  928. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  929. return null;
  930. };
  931. var device = deviceList[deviceListSelect];
  932. return device;
  933. },
  934. /// 获取当前的deviceId
  935. getCurrentDeviceId() {
  936. var that = this;
  937. var deviceList = that.getDeviceList();
  938. var deviceListSelect = that.getDeviceListSelect();
  939. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  940. return "";
  941. };
  942. var deviceId = deviceList[deviceListSelect].deviceId;
  943. return deviceId;
  944. },
  945. ///设置设备相关信息
  946. setDeviceInfor(other) {
  947. var that = this;
  948. // 接收设备当前信息
  949. that.getchannelData(other.ProdModel);
  950. // 电量
  951. if (other.Power) {
  952. var battery = constant.lexinMessage.getBattery(other.Power);
  953. that.setData({
  954. battery: battery,
  955. });
  956. }
  957. // 当前设备木有设置定时
  958. that.setData({
  959. isSetWake: false,
  960. });
  961. other.alarm.map((v) => {
  962. if (v.enable === "1") {
  963. that.setData({
  964. isSetWake: true
  965. });
  966. }
  967. });
  968. },
  969. ///******************************* 可折叠 ********************************************///
  970. onUserInfoLoad() {
  971. var that = this;
  972. var userInfo = wx.getStorageSync("userInfo") || "";
  973. if (!constant.strings.isEmpty(userInfo)) {
  974. var phone = userInfo.phone;
  975. if (!phone) {
  976. return;
  977. };
  978. var nickName = userInfo.nickname || "";
  979. var greeting = that.getGreetBuNickName(nickName);
  980. // 获取缓存的频道数据
  981. var channelData = wx.getStorageSync("channelData") || "";
  982. if (!constant.strings.isEmpty(channelData)) {
  983. that.setData({
  984. channelData: channelData,
  985. greeting: greeting,
  986. nickName: nickName,
  987. userPic: userInfo.headUrl || "",
  988. isLogin: true,
  989. })
  990. } else {
  991. that.setData({
  992. greeting: greeting,
  993. nickName: nickName,
  994. userPic: userInfo.headUrl || "",
  995. isLogin: true,
  996. });
  997. }
  998. } else {
  999. var greeting = that.getGreetBuNickName("");
  1000. that.setData({
  1001. greeting: greeting,
  1002. })
  1003. }
  1004. },
  1005. getGreetBuNickName(nickName) {
  1006. var greeting = constant.timeUtil.getGreet();
  1007. if (nickName != "" && nickName != "未登录") {
  1008. greeting = greeting + ',' + nickName;;
  1009. }
  1010. return greeting;
  1011. },
  1012. onBannerLoad() {
  1013. var that = this;
  1014. constant.apiUtil.getBanner({}).then((res) => {
  1015. that.setData({
  1016. bannerList: res,
  1017. });
  1018. constant.store.setStore("homeBanner", res);
  1019. });
  1020. },
  1021. stopIntervalId1: function () {
  1022. var that = this;
  1023. if (!constant.strings.isEmpty(that.data.intervalId1)) {
  1024. clearInterval(that.data.intervalId1);
  1025. that.data.intervalId1 = null;
  1026. }
  1027. },
  1028. stopIntervalId2: function () {
  1029. var that = this;
  1030. if (!constant.strings.isEmpty(that.data.intervalId2)) {
  1031. clearInterval(that.data.intervalId2);
  1032. that.data.intervalId2 = null;
  1033. }
  1034. },
  1035. ///销毁蓝牙
  1036. onUnload() {
  1037. var that = this;
  1038. constant.eventBus.removeNotification(CmdEvent.eventName, that)
  1039. console.log("界面onUnload")
  1040. that.stopIntervalId1();
  1041. that.stopIntervalId2();
  1042. if (!constant.strings.isEmpty(that.data.intervalId)) {
  1043. clearInterval(that.data.intervalId);
  1044. that.data.intervalId = null;
  1045. }
  1046. BtHelper.getInstance().disconnect();
  1047. if (constant.app.globalData.client === null) {
  1048. return;
  1049. };
  1050. constant.app.globalData.client.end(true);
  1051. constant.app.globalData.client = null;
  1052. },
  1053. })