index.js 36 KB

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