index.js 34 KB

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