index.js 36 KB

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