index.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. const app = getApp();
  2. const {
  3. login,
  4. listByDevice
  5. } = require('../../utils/api.js');
  6. import time_util from '../../utils/time_util.js';
  7. import strings from '../../utils/strings.js';
  8. import store from '../../utils/store.js';
  9. import route_constant from '../../utils/route_constant.js';
  10. import route_util from '../../utils/route_util.js';
  11. /// 乐鑫相关
  12. import lexin_util from '../../utils/lexin/util.js';
  13. import lexin_jump from '../../utils/lexin/jump.js';
  14. import lexin_add from '../../utils/lexin/add.js';
  15. import lexin_scan from '../../utils/lexin/scan.js';
  16. import lexin_subscribe from '../../utils/lexin/subscribe.js';
  17. import lexin_connect from '../../utils/lexin/connect.js';
  18. import lexin_message from '../../utils/lexin/message.js';
  19. import {
  20. BtHelper
  21. } from '../../devices/bt_helper.js';
  22. import event_bus, { addNotification } from '../../utils/event_bus.js';
  23. import {
  24. EnumCmdEvent,
  25. CmdEvent
  26. } from '../../devices/cmd_key_event';
  27. Page({
  28. data: {
  29. navBarHeight: app.globalData.navBarHeight,
  30. safeBottomHeight: app.globalData.safeBottomHeight,
  31. nvabarData: {
  32. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  33. title: 'OhPlay', //导航栏 中间的标题
  34. },
  35. ///下午好
  36. isLogin: false,
  37. greeting: "",
  38. bannerList: [{
  39. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  40. },
  41. {
  42. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  43. }
  44. ],
  45. autoplay: true,
  46. interval: 3000, // 切换时间间隔
  47. duration: 500, // 滑动动画时长
  48. circular: true, // 衔接滑动
  49. indexPage: 0,
  50. ///是否展示频道
  51. showChannel: false,
  52. ////首页
  53. navBarHeight: app.globalData.navBarHeight,
  54. MenuButtonheight: app.globalData.MenuButtonheight,
  55. MenuButtonTop: app.globalData.MenuButtonTop,
  56. actionIndex: null,
  57. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  58. channelData: [],
  59. isOneLoading: true,
  60. uid: null,
  61. isSetWake: false,
  62. battery: 4, // 0≤电量<20,0格
  63. ////我的界面
  64. loginStatus: true,
  65. nickName: "未登录",
  66. userPic: './../../img/head_pic.png',
  67. intervalId: null,
  68. /// 是否第一次自动连接过
  69. autoConnected: false,
  70. intervalId1: null,
  71. intervalId2: null,
  72. deviceMacId: null,
  73. deviceListSelect: null,
  74. deviceList: [],
  75. _willConnectBle: null,
  76. // deviceList: [{
  77. // "connectType": 3,
  78. // "devName": "猫王小王子OTR-X",
  79. // "state": "online",
  80. // "name": "BLUFI_7cdfa1fcbb24",
  81. // "deviceId": "BLUFI_7cdfa1fcbb24",
  82. // "mac": "BLUFI_7cdfa1fcbb24",
  83. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  84. // "ProdModel": "MW-2AX(WIFI-N)",
  85. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  86. // }, {
  87. // "connectType": 3,
  88. // "devName": "猫王小王子OTR-X",
  89. // "state": "online",
  90. // "name": "BLUFI_7cdfa1fcbb24",
  91. // "deviceId": "BLUFI_7cdfa1fcbb24",
  92. // "mac": "BLUFI_7cdfa1fcbb24",
  93. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  94. // "ProdModel": "MW-2AX(WIFI-N)",
  95. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  96. // }, {
  97. // "connectType": 3,
  98. // "devName": "猫王小王子OTR-X",
  99. // "state": "online",
  100. // "name": "BLUFI_7cdfa1fcbb24",
  101. // "deviceId": "BLUFI_7cdfa1fcbb24",
  102. // "mac": "BLUFI_7cdfa1fcbb24",
  103. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  104. // "ProdModel": "MW-2AX(WIFI-N)",
  105. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  106. // }, {
  107. // "connectType": 3,
  108. // "devName": "猫王小王子OTR-X",
  109. // "state": "online",
  110. // "name": "BLUFI_7cdfa1fcbb24",
  111. // "deviceId": "BLUFI_7cdfa1fcbb24",
  112. // "mac": "BLUFI_7cdfa1fcbb24",
  113. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  114. // "ProdModel": "MW-2AX(WIFI-N)",
  115. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  116. // }, {
  117. // "connectType": 3,
  118. // "devName": "猫王小王子OTR-X",
  119. // "state": "online",
  120. // "name": "BLUFI_7cdfa1fcbb24",
  121. // "deviceId": "BLUFI_7cdfa1fcbb24",
  122. // "mac": "BLUFI_7cdfa1fcbb24",
  123. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  124. // "ProdModel": "MW-2AX(WIFI-N)",
  125. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  126. // }, {
  127. // "connectType": 3,
  128. // "devName": "猫王小王子OTR-X",
  129. // "state": "online",
  130. // "name": "BLUFI_7cdfa1fcbb24",
  131. // "deviceId": "BLUFI_7cdfa1fcbb24",
  132. // "mac": "BLUFI_7cdfa1fcbb24",
  133. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  134. // "ProdModel": "MW-2AX(WIFI-N)",
  135. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  136. // }],
  137. connectDeviceIding: "",
  138. },
  139. onLoad(options) {
  140. var that = this;
  141. that.onLoginLoad();
  142. that.onDeviceLoad();
  143. that.onUserInfoLoad();
  144. that.onBannerLoad();
  145. },
  146. onShow() {
  147. var that = this;
  148. that.onBannerShow();
  149. },
  150. onLoginLoad() {
  151. var that = this;
  152. var userInfo = wx.getStorageSync("userInfo") || "";
  153. if (!strings.isEmpty(userInfo)) {
  154. var phone = userInfo.phone;
  155. if (!strings.isEmpty(phone)) {
  156. that.loginReset(phone, null, null, null);
  157. };
  158. }
  159. },
  160. ///重新登录
  161. loginReset(phone, succeedCall1, succeedCall2, failCall) {
  162. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
  163. var that = this;
  164. wx.login({
  165. success: res0 => {
  166. // {"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}
  167. login({
  168. code: res0.code,
  169. phone: phone,
  170. }).then((res1) => {
  171. app.globalData.userInfo = res1;
  172. store.setStore("userInfo", res1);
  173. store.setStore("token", JSON.stringify({
  174. id: res1.userId,
  175. token: res1.accessToken,
  176. }));
  177. that.onUserInfoLoad();
  178. if (succeedCall1 != null) {
  179. succeedCall1();
  180. }
  181. if (succeedCall2 != null) {
  182. succeedCall2();
  183. }
  184. });
  185. },
  186. fail(fail4) {
  187. if (failCall != null) {
  188. failCall();
  189. }
  190. },
  191. });
  192. },
  193. testData() {
  194. return {
  195. "deviceId": "xxxxx",
  196. "deviceName": "xxxxxx",
  197. "deviceType": "xxxx",
  198. "type": 1,
  199. "deviceIcon": "xxxx",
  200. "deviceMacId": "xxxx",
  201. "deviceBattery": 4,
  202. "state": "online",
  203. "connectType": 1,
  204. "clientType": "猫王妙播黑胶音箱(BLE)",
  205. "deviceConnectStatus": 1,
  206. "deviceConnectStatusText": "xxxx",
  207. }
  208. },
  209. onDeviceLoad() {
  210. var that = this;
  211. var deviceList = store.getStore("deviceList");
  212. if (!strings.isEmpty(deviceList)) {
  213. // deviceList = deviceList.concat(deviceList);
  214. that.updateDeviceList(deviceList, true, false);
  215. }
  216. // that.addBlueDevice(that.testData())
  217. let hasBle = that.data.deviceList.find(item => {
  218. return item.connectType == 1;
  219. })
  220. if (hasBle) {
  221. ///监听蓝牙设备
  222. BtHelper.getInstance().initBluetoothAdapter();
  223. BtHelper.getInstance().getBluetoothDevices();
  224. BtHelper.getInstance().startScan(null, null, function (res) { });
  225. var isFirst = true;
  226. console.log("开始监听蓝牙设备");
  227. that.startBleTimer(isFirst);
  228. that.addNotification()
  229. }
  230. },
  231. startBleTimer(isFirst) {
  232. let that = this;
  233. ///再秒再对比一次
  234. that.stopIntervalId1();
  235. let count = 0;
  236. that.data.intervalId1 = setInterval(async function () {
  237. console.log("开始扫描蓝牙设备");
  238. that.compareList();
  239. // if (count > 4) {
  240. if (isFirst) {
  241. that.stopIntervalId1();
  242. that.startBleTimer(false);
  243. }
  244. // } else {
  245. // count++;
  246. // }
  247. isFirst = false;
  248. }, isFirst ? 3 * 1000 : 10 * 1000);
  249. },
  250. /// 对比蓝牙数据
  251. compareList() {
  252. let that = this;
  253. lexin_scan.compareList2(that.data.deviceList, function (device) {
  254. if (device) {
  255. that.data._willConnectBle = device;
  256. getApp().getBluetoothStatus();
  257. }
  258. });
  259. // lexin_scan.compareList(function (list) {
  260. // console.log("对比蓝牙数据", list);
  261. // that.updateDeviceList(list, false, false);
  262. // }, function (item) {
  263. // that.addBlueDevice(item);
  264. // });
  265. },
  266. getBluetoothStatusCallck(v) {
  267. var that = this;
  268. if (!that.data._willConnectBle) {
  269. // true 走下面
  270. return;
  271. }
  272. if (v) {
  273. var device = that.data._willConnectBle;
  274. console.log("开始连接设备0", device);
  275. if (device.state === 'online' || device.connectType != 1) {
  276. return;
  277. }
  278. // });
  279. BtHelper.getInstance().connect(device, function (isConnected, d) {
  280. if (isConnected) {
  281. device.state = 'online';
  282. that.addBlueDevice(device);
  283. // that.stopIntervalId1();
  284. } else {
  285. device.state = 'offline';
  286. }
  287. });
  288. } else {
  289. // wx.showModal({
  290. // title: '温馨提示',
  291. // content: '请检查手机蓝牙是否打开',
  292. // showCancel: false,
  293. // success: function (res) { }
  294. // });
  295. }
  296. that.data._willConnectBle = null;
  297. },
  298. // 回调
  299. mqttCallback(type, option) {
  300. // console.log("gadsfadsfadsfa==888===" + type);
  301. var that = this;
  302. let payloads = null;
  303. if (option && option.payload) {
  304. payloads = JSON.parse(option.payload);
  305. };
  306. switch (type) {
  307. ///先走订阅
  308. case "connect":
  309. lexin_subscribe.subscribeAllDevice();
  310. break;
  311. ///再走有在线
  312. case "message_onoffline":
  313. lexin_connect.searchOnlineDevice(payloads, function (list) {
  314. that.updateDeviceList(list, false, false);
  315. }, function () {
  316. var indexPage = that.data.indexPage;
  317. that.setData({
  318. actionIndex: null,
  319. deviceListSelect: null,
  320. indexPage: indexPage == 1 ? 0 : indexPage,
  321. });
  322. }, function () {
  323. var indexPage = that.data.indexPage;
  324. that.setData({
  325. actionIndex: null,
  326. deviceListSelect: null,
  327. indexPage: indexPage == 1 ? 0 : indexPage,
  328. });
  329. });
  330. break;
  331. case "message":
  332. if (payloads) {
  333. var payloadType = payloads.type;
  334. var other = payloads.other;
  335. ///获取频道位置
  336. if (payloadType === "get_position" && other) {
  337. lexin_message.getPosition(other, function (actionIndex) {
  338. that.setData({
  339. actionIndex: actionIndex,
  340. });
  341. });
  342. }
  343. // 获取歌曲信息
  344. else if (payloadType === "get_dev_info" && other) {
  345. that.setDeviceInfor(other);
  346. lexin_message.getDeviceInfo(payloads);
  347. }
  348. ///获取电量
  349. else if (payloadType === "battery" && other) {
  350. if (other.battery) {
  351. var battery = lexin_message.getBattery(other.battery);
  352. that.setData({
  353. battery: battery,
  354. });
  355. }
  356. }
  357. ///获取播放状态
  358. else if (payloadType === "play" || payloadType === "play_state") {
  359. lexin_message.getPlay();
  360. }
  361. }
  362. break;
  363. default:
  364. }
  365. },
  366. ///去连接设备数据
  367. actionDevice(device) {
  368. var that = this;
  369. // 取消订阅
  370. var deviceMacId = that.data.deviceMacId;
  371. if (!strings.isEmpty(deviceMacId)) {
  372. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  373. app.unsubscribe(pubResponse);
  374. };
  375. that.subscribeCurrDevice(device);
  376. },
  377. ///数据处理
  378. subscribeCurrDevice(device) {
  379. var that = this;
  380. var isOK = app.globalData.client && app.globalData.client.connected;
  381. if (!isOK) {
  382. app.connect();
  383. setTimeout(() => {
  384. that.subscribeCurrDevice(device);
  385. }, 500);
  386. return;
  387. };
  388. var deviceId = device.deviceId;
  389. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  390. that.setData({
  391. deviceMacId: deviceMacId,
  392. });
  393. /// 订阅设备
  394. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  395. app.subscribe(pubResponse);
  396. /// 获取设备信息
  397. const obj = {
  398. DstDeviceName: deviceMacId
  399. };
  400. app.PubMsg({
  401. type: "get_dev_info",
  402. ...obj
  403. });
  404. },
  405. ///选择对应的频道
  406. onTapChannelIndex(e) {
  407. var that = this;
  408. var index = e.currentTarget.dataset.index;
  409. lexin_jump.toChannelIndex(index, function (i) {
  410. that.setData({
  411. actionIndex: i,
  412. });
  413. });
  414. },
  415. ///去频道详情
  416. onTapToChannel() {
  417. var that = this;
  418. var actionIndex = that.data.actionIndex;
  419. if (actionIndex == null) {
  420. return;
  421. }
  422. var channelData = that.getChannelData();
  423. if (channelData.length > actionIndex) {
  424. store.setStore("channelDeta", channelData[actionIndex]);
  425. wx.navigateTo({
  426. url: './../channelDetails/channelDetails'
  427. });
  428. }
  429. },
  430. ///选择对应的频道
  431. getchannelData(clientType) {
  432. var that = this;
  433. listByDevice({
  434. clientType: clientType,
  435. unShowLoad: true,
  436. }).then((res) => {
  437. store.setStore("channelData", res);
  438. that.setData({
  439. channelData: res,
  440. });
  441. // 接收设备当前播放状态
  442. var deviceList = that.getDeviceList();
  443. var deviceListSelect = that.getDeviceListSelect();
  444. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  445. return;
  446. };
  447. var deviceId = deviceList[deviceListSelect].deviceId;
  448. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  449. const obj = {
  450. DstDeviceName: deviceMacId
  451. }
  452. app.PubMsg({
  453. type: "get_position",
  454. ...obj
  455. });
  456. })
  457. },
  458. onTapIndex(e) {
  459. var that = this;
  460. var index = e.currentTarget.dataset.index;
  461. var indexPage = that.data.indexPage;
  462. if (indexPage != index) {
  463. that.setData({
  464. indexPage: index,
  465. });
  466. }
  467. },
  468. onTapLogin() {
  469. var that = this;
  470. ///退出登录
  471. var isLogin = that.getIsLogin();
  472. if (isLogin) {
  473. that.logOut();
  474. return;
  475. }
  476. route_util.jump(route_constant.login);
  477. },
  478. ///退出登录
  479. logOut() {
  480. var that = this;
  481. wx.removeStorageSync('userInfo');
  482. wx.removeStorageSync('token');
  483. var nickName = "未登录";
  484. var greeting = that.getGreetBuNickName(nickName);
  485. that.setData({
  486. isLogin: false,
  487. greeting: greeting,
  488. nickName: nickName,
  489. userPic: "./../../img/head_pic.png",
  490. });
  491. var deviceList = that.getDeviceList();
  492. var deviceListSelect = that.getDeviceListSelect();
  493. if (deviceList.length > 1 && (deviceListSelect === null || deviceList.length <= deviceListSelect)) {
  494. // 2个设备才判断,1个直接删除
  495. return;
  496. };
  497. deviceListSelect = deviceListSelect ?? 0
  498. ///有设备在线被选中,则让它不被选择
  499. if (deviceList[deviceListSelect].connectType == 3) {
  500. var deviceMacId = that.data.deviceMacId;
  501. if (!strings.isEmpty(deviceMacId)) {
  502. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  503. app.unsubscribe(pubResponse);
  504. var indexPage = that.data.indexPage;
  505. that.setData({
  506. deviceMacId: null,
  507. actionIndex: null,
  508. deviceListSelect: null,
  509. indexPage: indexPage == 1 ? 0 : indexPage,
  510. });
  511. };
  512. } else {
  513. // console.log("退出登录,断开连接", deviceList);
  514. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  515. // var indexPage = that.data.indexPage;
  516. // that.setData({
  517. // deviceMacId: null,
  518. // actionIndex: null,
  519. // deviceListSelect: null,
  520. // indexPage: indexPage == 1 ? 0 : indexPage,
  521. // });
  522. }
  523. },
  524. ///点击item
  525. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  526. async onTapItem(e) {
  527. var that = this;
  528. var isLogin = that.getIsLogin();
  529. if (!isLogin) {
  530. route_util.jump(route_constant.login);
  531. return;
  532. }
  533. var index = e.currentTarget.dataset.index;
  534. var device = e.currentTarget.dataset.item;
  535. var connectType = device.connectType;
  536. var deviceListSelect = that.getDeviceListSelect();
  537. // wifi只支持在线点击
  538. if (connectType == 3 && device.state === "online") {
  539. if (index === deviceListSelect) {
  540. that.goWake();
  541. } else {
  542. that.addWifiDevice(device);
  543. }
  544. return;
  545. }
  546. ///去蓝牙连接处理
  547. // {"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 妙播收音机"}
  548. if (index === deviceListSelect && device.state === "online") {
  549. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(device))
  550. } else if (device.state === "offline") {
  551. var deviceList = that.getDeviceList();
  552. var deviceListSelect = that.getDeviceListSelect();
  553. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  554. that.setData({
  555. deviceListSelect: null,
  556. });
  557. var currentDevice = deviceList[deviceListSelect];
  558. await BtHelper.getInstance().disconnect(currentDevice);
  559. // await time_util.delayMills(500);
  560. }
  561. device.name = device.devName;
  562. BtHelper.getInstance().connect(device, function (isConnected, d) {
  563. if (isConnected) {
  564. device.state = "online";
  565. that.addBlueDevice(device);
  566. }
  567. });
  568. } else {
  569. var deviceList = that.getDeviceList();
  570. var deviceListSelect = that.getDeviceListSelect();
  571. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  572. that.setData({
  573. deviceListSelect: null,
  574. });
  575. var currentDevice = deviceList[deviceListSelect];
  576. await BtHelper.getInstance().disconnect(currentDevice);
  577. // await time_util.delayMills(500);
  578. }
  579. device.name = device.devName;
  580. BtHelper.getInstance().connect(device, function (isConnected, d) {
  581. if (isConnected) {
  582. device.state = "online";
  583. that.addBlueDevice(device);
  584. }
  585. });
  586. }
  587. },
  588. // 去唤醒界面
  589. goWake() {
  590. var that = this;
  591. var deviceList = that.getDeviceList();
  592. var deviceListSelect = that.getDeviceListSelect();
  593. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  594. return
  595. };
  596. var device = deviceList[deviceListSelect];
  597. wx.navigateTo({
  598. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  599. });
  600. },
  601. ///删除当前设备
  602. deleteDevice(e) {
  603. var that = this;
  604. var index = e.currentTarget.dataset.index;
  605. wx.showModal({
  606. title: '确定删除?',
  607. success: function (res) {
  608. if (res.confirm) {
  609. var deviceList = that.getDeviceList();
  610. if (deviceList[index].connectType == 3) {
  611. that.cancelWifi(index, false);
  612. } else {
  613. that.cancelBlue(index, false);
  614. }
  615. }
  616. }
  617. });
  618. },
  619. ///是否是同一个蓝牙
  620. isTheSameBlue(device) {
  621. var that = this;
  622. var deviceList = that.getDeviceList();
  623. var deviceListSelect = that.getDeviceListSelect();
  624. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  625. return false;
  626. };
  627. if (deviceList[deviceListSelect].connectType == 3) {
  628. return false;
  629. } else {
  630. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  631. return true;
  632. } else {
  633. return false;
  634. }
  635. }
  636. },
  637. ///断开当前的
  638. cancelCurrent() {
  639. var that = this;
  640. var deviceList = that.getDeviceList();
  641. var deviceListSelect = that.getDeviceListSelect();
  642. if (deviceList[deviceListSelect].connectType == 3) {
  643. that.cancelWifi(deviceListSelect, true);
  644. } else {
  645. that.cancelBlue(deviceListSelect, true);
  646. }
  647. },
  648. /// 断开蓝牙连接
  649. cancelBlue(index, onlyCancel) {
  650. var that = this;
  651. if (onlyCancel) {
  652. return;
  653. }
  654. var deviceList = that.getDeviceList();
  655. var deviceListSelect = that.getDeviceListSelect();
  656. ///是否删除的当前播放这个
  657. var isCurrentIndex = false;
  658. deviceListSelect = deviceListSelect ?? 0;
  659. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  660. isCurrentIndex = (deviceListSelect == index);
  661. };
  662. ///删除当前设备
  663. var deviceId = deviceList[index].deviceId;
  664. deviceList = deviceList.filter((item, i) => {
  665. return deviceId !== item.deviceId;
  666. });
  667. store.setStore("deviceList", deviceList);
  668. var indexPage = that.data.indexPage;
  669. that.setData({
  670. deviceList: deviceList,
  671. indexPage: indexPage == 1 ? 0 : indexPage,
  672. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  673. });
  674. BtHelper.getInstance().disconnect({
  675. "deviceId": deviceId
  676. });
  677. },
  678. /// 断开连接wifi
  679. cancelWifi(index, onlyCancel) {
  680. var that = this;
  681. var deviceList = that.getDeviceList();
  682. if (index === null || deviceList.length <= index) {
  683. return;
  684. };
  685. var deviceId = deviceList[index].deviceId;
  686. // 取消订阅
  687. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  688. app.unsubscribe(pubResponse);
  689. if (onlyCancel) {
  690. return;
  691. }
  692. ///是否删除的当前播放这个
  693. var isCurrentIndex = false;
  694. var deviceListSelect = that.getDeviceListSelect();
  695. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  696. isCurrentIndex = (deviceListSelect == index);
  697. };
  698. deviceList = deviceList.filter((item, i) => {
  699. return deviceId !== item.deviceId;
  700. });
  701. store.setStore("deviceList", deviceList);
  702. var indexPage = that.data.indexPage;
  703. var deviceMacId = that.data.deviceMacId;
  704. var actionIndex = that.data.actionIndex;
  705. that.setData({
  706. deviceList: deviceList,
  707. deviceMacId: isCurrentIndex ? null : deviceMacId,
  708. actionIndex: isCurrentIndex ? null : actionIndex,
  709. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  710. indexPage: indexPage == 1 ? 0 : indexPage,
  711. });
  712. },
  713. /// 新添加蓝牙设备
  714. addBlueDevice(device) {
  715. var that = this;
  716. var addDeviceList = lexin_add.addBlueDevice(device);
  717. that.updateDeviceList(addDeviceList, false, false);
  718. that.setData({
  719. deviceListSelect: 0,
  720. autoConnected: true,
  721. });
  722. that.addNotification()
  723. },
  724. addNotification() {
  725. let that = this;
  726. event_bus.removeNotification(CmdEvent.eventName, that);
  727. event_bus.addNotification(CmdEvent.eventName, function (event) {
  728. if (event.cmdEvent == EnumCmdEvent.onoffline) {
  729. let isFirst = false;
  730. // 有下线就开启监听扫描
  731. if (event.commonValue == "online") {
  732. that.stopIntervalId1();
  733. } else {
  734. that.startBleTimer(isFirst);
  735. }
  736. console.log("首页ble通知:", event.deviceId)
  737. let disDevice = that.data.deviceList.find(item => {
  738. return item.deviceId == event.deviceId
  739. })
  740. // that.data.deviceList.forEach(element => {
  741. // if (item.deviceId == tmp.deviceId) {
  742. // }
  743. // });
  744. if (!disDevice) {
  745. return;
  746. }
  747. console.log("首页设备状态变化", disDevice)
  748. disDevice.state = event.commonValue;
  749. if (event.commonValue == "online") {
  750. // BtHelper.getInstance().connect()
  751. console.log("首页上线连接", disDevice)
  752. } else {
  753. console.log("首页断开连接", disDevice)
  754. that.updateDeviceList(that.data.deviceList, false, false);
  755. }
  756. }
  757. }, that);
  758. },
  759. // 新添加Wifi设备
  760. addWifiDevice(device) {
  761. var that = this;
  762. var isOK = app.globalData.client && app.globalData.client.connected;
  763. if (!isOK) {
  764. app.connect();
  765. setTimeout(() => {
  766. that.addWifiDevice(device);
  767. }, 500);
  768. return;
  769. };
  770. var deviceId = device.deviceId;
  771. var addDeviceList = lexin_add.addWifiDevice(device);
  772. that.updateDeviceList(addDeviceList, false, false);
  773. var indexPage = that.data.indexPage;
  774. that.setData({
  775. deviceListSelect: null,
  776. connectDeviceIding: deviceId,
  777. indexPage: indexPage == 1 ? 0 : indexPage,
  778. });
  779. lexin_subscribe.subscribeSingleDevice(device);
  780. },
  781. refreshCurrentDevice(device) {
  782. var that = this;
  783. var addDeviceList = lexin_add.addWifiDevice(device);
  784. that.updateDeviceList(addDeviceList, false, false);
  785. that.setData({
  786. autoConnected: true,
  787. deviceListSelect: 0,
  788. connectDeviceIding: "",
  789. });
  790. },
  791. /// 手机关闭蓝牙,所有蓝牙设备离线
  792. closeBlueResetOffline(isInit, closeAllBlue) {
  793. var that = this;
  794. var deviceList = that.getDeviceList();
  795. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  796. },
  797. /// 更新列表排序
  798. updateDeviceList(deviceList, isInit, closeAllBlue) {
  799. var that = this;
  800. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  801. store.setStore("deviceList", finalList);
  802. that.setData({
  803. deviceList: finalList,
  804. });
  805. },
  806. ///点击banner事件
  807. onTapBanner() {
  808. var that = this;
  809. var item = e.currentTarget.dataset.item;
  810. },
  811. ///添加设备
  812. jumpToAddDevice() {
  813. var that = this;
  814. var isLogin = that.getIsLogin();
  815. if (!isLogin) {
  816. route_util.jump(route_constant.login);
  817. return;
  818. }
  819. route_util.jump(route_constant.deviceList);
  820. },
  821. // 关于我们
  822. jumpToAboutUs() {
  823. lexin_jump.toAboutUs();
  824. },
  825. /// 是否已登录
  826. getIsLogin() {
  827. var that = this;
  828. var isLogin = that.data.isLogin;
  829. return isLogin;
  830. },
  831. /// 获取列表数据
  832. getDeviceList() {
  833. var that = this;
  834. var deviceList = that.data.deviceList;
  835. return deviceList;
  836. },
  837. ///获取当前选中的
  838. getDeviceListSelect() {
  839. var that = this;
  840. var deviceListSelect = that.data.deviceListSelect;
  841. return deviceListSelect;
  842. },
  843. ///获取是否已经第一次连接过
  844. getAutoConnected() {
  845. var that = this;
  846. var autoConnected = that.data.autoConnected;
  847. return autoConnected;
  848. },
  849. ///当前正在连接的设备id
  850. getConnectDeviceIding() {
  851. var that = this;
  852. var connectDeviceIding = that.data.connectDeviceIding
  853. return connectDeviceIding;
  854. },
  855. ///获取频道数据
  856. getChannelData() {
  857. var that = this;
  858. var channelData = that.data.channelData;
  859. return channelData;
  860. },
  861. /// 当前播放频道
  862. getActionIndex() {
  863. var that = this;
  864. var actionIndex = that.data.actionIndex;
  865. return actionIndex;
  866. },
  867. /// 获取当前的deviceId
  868. getCurrentDeviceId() {
  869. var that = this;
  870. var deviceList = that.getDeviceList();
  871. var deviceListSelect = that.getDeviceListSelect();
  872. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  873. return "";
  874. };
  875. var deviceId = deviceList[deviceListSelect].deviceId;
  876. return deviceId;
  877. },
  878. ///设置设备相关信息
  879. setDeviceInfor(other) {
  880. var that = this;
  881. // 接收设备当前信息
  882. that.getchannelData(other.ProdModel);
  883. // 电量
  884. if (other.Power) {
  885. var battery = lexin_message.getBattery(other.Power);
  886. that.setData({
  887. battery: battery,
  888. });
  889. }
  890. // 当前设备木有设置定时
  891. that.setData({
  892. isSetWake: false,
  893. });
  894. other.alarm.map((v) => {
  895. if (v.enable === "1") {
  896. that.setData({
  897. isSetWake: true
  898. });
  899. }
  900. });
  901. },
  902. ///******************************* 可折叠 ********************************************///
  903. onUserInfoLoad() {
  904. var that = this;
  905. var userInfo = wx.getStorageSync("userInfo") || "";
  906. if (!strings.isEmpty(userInfo)) {
  907. var phone = userInfo.phone;
  908. if (!phone) {
  909. return;
  910. };
  911. var nickName = userInfo.nickname || "";
  912. var greeting = that.getGreetBuNickName(nickName);
  913. // 获取缓存的频道数据
  914. var channelData = wx.getStorageSync("channelData") || "";
  915. if (!strings.isEmpty(channelData)) {
  916. that.setData({
  917. channelData: channelData,
  918. greeting: greeting,
  919. nickName: nickName,
  920. userPic: userInfo.headUrl || "",
  921. isLogin: true,
  922. })
  923. } else {
  924. that.setData({
  925. greeting: greeting,
  926. nickName: nickName,
  927. userPic: userInfo.headUrl || "",
  928. isLogin: true,
  929. });
  930. }
  931. } else {
  932. var greeting = that.getGreetBuNickName("");
  933. that.setData({
  934. greeting: greeting,
  935. })
  936. }
  937. },
  938. getGreetBuNickName(nickName) {
  939. var greeting = time_util.getGreet();
  940. if (nickName != "" && nickName != "未登录") {
  941. greeting = greeting + ',' + nickName;;
  942. }
  943. return greeting;
  944. },
  945. onBannerLoad() {
  946. var that = this;
  947. // var bannerList = wx.getStorageSync("homeBanner") || [];
  948. // if (that.data.bannerList.length > 0) {
  949. // that.setData({
  950. // bannerList: bannerList
  951. // });
  952. // }
  953. },
  954. onBannerShow() {
  955. var that = this;
  956. // getBanner({}).then((res) => {
  957. // that.setData({
  958. // bannerList: res
  959. // });
  960. // store.setStore("homeBanner", res);
  961. // })
  962. },
  963. stopIntervalId1: function () {
  964. var that = this;
  965. if (!strings.isEmpty(that.data.intervalId1)) {
  966. clearInterval(that.data.intervalId1);
  967. that.data.intervalId1 = null;
  968. }
  969. },
  970. stopIntervalId2: function () {
  971. var that = this;
  972. if (!strings.isEmpty(that.data.intervalId2)) {
  973. clearInterval(that.data.intervalId2);
  974. that.data.intervalId2 = null;
  975. }
  976. },
  977. ///销毁蓝牙
  978. onUnload() {
  979. var that = this;
  980. event_bus.removeNotification(CmdEvent.eventName, that)
  981. console.log("界面onUnload")
  982. that.stopIntervalId1();
  983. that.stopIntervalId2();
  984. if (!strings.isEmpty(that.data.intervalId)) {
  985. clearInterval(that.data.intervalId);
  986. that.data.intervalId = null;
  987. }
  988. BtHelper.getInstance().disconnect();
  989. if (app.globalData.client === null) {
  990. return;
  991. };
  992. app.globalData.client.end(true);
  993. app.globalData.client.end(true);
  994. app.globalData.client = null;
  995. },
  996. })