index.js 34 KB

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