index.js 34 KB

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