index.js 30 KB

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