index.js 36 KB

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