index.js 32 KB

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