index.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. wx.navigateTo({
  451. url: './../channelDetails/channelDetails'
  452. });
  453. }
  454. },
  455. ///选择对应的频道
  456. getchannelData(clientType) {
  457. var that = this;
  458. listByDevice({
  459. clientType: clientType,
  460. unShowLoad: true,
  461. }).then((res) => {
  462. store.setStore("channelData", res);
  463. that.setData({
  464. channelData: res,
  465. });
  466. // 接收设备当前播放状态
  467. var deviceList = that.getDeviceList();
  468. var deviceListSelect = that.getDeviceListSelect();
  469. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  470. return;
  471. };
  472. var deviceId = deviceList[deviceListSelect].deviceId;
  473. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  474. const obj = {
  475. DstDeviceName: deviceMacId
  476. }
  477. app.PubMsg({
  478. type: "get_position",
  479. ...obj
  480. });
  481. })
  482. },
  483. onTapIndex(e) {
  484. var that = this;
  485. var index = e.currentTarget.dataset.index;
  486. var indexPage = that.data.indexPage;
  487. if (indexPage != index) {
  488. that.setData({
  489. indexPage: index,
  490. });
  491. }
  492. },
  493. onTapLogin() {
  494. var that = this;
  495. ///退出登录
  496. var isLogin = that.getIsLogin();
  497. if (isLogin) {
  498. that.logOut();
  499. return;
  500. }
  501. routeUtil.jump(routePath.login);
  502. },
  503. ///退出登录
  504. logOut() {
  505. var that = this;
  506. wx.removeStorageSync('userInfo');
  507. wx.removeStorageSync('token');
  508. var nickName = "未登录";
  509. var greeting = that.getGreetBuNickName(nickName);
  510. that.setData({
  511. isLogin: false,
  512. greeting: greeting,
  513. nickName: nickName,
  514. userPic: "./../../../img/head_pic.png",
  515. });
  516. var device = that.getCurrentDevice();
  517. if (device == null) {
  518. return;
  519. }
  520. ///有设备在线被选中,则让它不被选择
  521. if (device.connectType == 3) {
  522. var deviceMacId = that.data.deviceMacId;
  523. if (!strings.isEmpty(deviceMacId)) {
  524. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  525. app.unsubscribe(pubResponse);
  526. var indexPage = that.data.indexPage;
  527. that.setData({
  528. deviceMacId: null,
  529. actionIndex: null,
  530. deviceListSelect: null,
  531. indexPage: indexPage == 1 ? 0 : indexPage,
  532. });
  533. };
  534. } else {
  535. // console.log("退出登录,断开连接", deviceList);
  536. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  537. // var indexPage = that.data.indexPage;
  538. // that.setData({
  539. // deviceMacId: null,
  540. // actionIndex: null,
  541. // deviceListSelect: null,
  542. // indexPage: indexPage == 1 ? 0 : indexPage,
  543. // });
  544. }
  545. },
  546. ///点击item
  547. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  548. async onTapItem(e) {
  549. var that = this;
  550. var isLogin = that.getIsLogin();
  551. if (!isLogin) {
  552. routeUtil.jump(routePath.login);
  553. return;
  554. }
  555. var index = e.currentTarget.dataset.index;
  556. var device = e.currentTarget.dataset.item;
  557. var connectType = device.connectType;
  558. var deviceListSelect = that.getDeviceListSelect();
  559. // wifi只支持在线点击
  560. if (connectType == 3 && device.state === "online") {
  561. if (index === deviceListSelect) {
  562. that.goWake();
  563. } else {
  564. // 现断线第一个
  565. // that.disconnectFirst()
  566. that.addWifiDevice(device);
  567. }
  568. return;
  569. }
  570. ///去蓝牙连接处理
  571. // {"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 妙播收音机"}
  572. var deviceList = that.getDeviceList();
  573. let curDevice = deviceList[index]
  574. if (index === deviceListSelect && device.state === "online") {
  575. var param = "?param=" + JSON.stringify(curDevice);
  576. routeUtil.jumpParam(routePath.deviceDetail, param);
  577. } else if (device.state === "offline") {
  578. var deviceListSelect = that.getDeviceListSelect();
  579. if (deviceListSelect != null) {
  580. if (deviceList.length > deviceListSelect) {
  581. var currentDevice = deviceList[deviceListSelect];
  582. console.log("断开连接0", currentDevice)
  583. // 现断线第一个
  584. // that.disconnectFirst()
  585. }
  586. that.setData({
  587. deviceListSelect: null,
  588. });
  589. }
  590. console.log("断开连接2", device)
  591. device.name = device.devName;
  592. that.data._clickOtherDevice = true;
  593. BtHelper.getInstance().connect(device, function (isConnected, d) {
  594. that.data._clickOtherDevice = false;
  595. if (isConnected) {
  596. device.state = "online";
  597. that.addBlueDevice(device);
  598. }
  599. });
  600. } else {
  601. var deviceList = that.getDeviceList();
  602. var deviceListSelect = that.getDeviceListSelect();
  603. if (deviceListSelect != null) {
  604. if (deviceList.length > deviceListSelect) {
  605. var currentDevice = deviceList[deviceListSelect];
  606. if (currentDevice.connectType === 1) {
  607. console.log("断开连接1", currentDevice)
  608. await BtHelper.getInstance().disconnect(currentDevice);
  609. }
  610. // await time_util.delayMills(500);
  611. }
  612. that.setData({
  613. deviceListSelect: null,
  614. });
  615. }
  616. that.data._clickOtherDevice = true;
  617. device.name = device.devName;
  618. BtHelper.getInstance().connect(device, function (isConnected, d) {
  619. that.data._clickOtherDevice = false;
  620. if (isConnected) {
  621. device.state = "online";
  622. that.addBlueDevice(device);
  623. }
  624. });
  625. }
  626. },
  627. disconnectFirst() {
  628. let that = this;
  629. let deviceList = that.getDeviceList()
  630. let first = deviceList[0] ?? {}
  631. if (first.state == "online") {
  632. if (first.connectType === 1) {
  633. BtHelper.getInstance().disconnect(first)
  634. first.state == "offline"
  635. } else {
  636. // 取消订阅
  637. // var pubResponse = lexin_util.getResponseByDeviceId(first.deviceId);
  638. // app.unsubscribe(pubResponse);
  639. }
  640. }
  641. },
  642. // 去唤醒界面
  643. goWake() {
  644. var that = this;
  645. var deviceList = that.getDeviceList();
  646. var deviceListSelect = that.getDeviceListSelect();
  647. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  648. return
  649. };
  650. var device = deviceList[deviceListSelect];
  651. wx.navigateTo({
  652. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  653. });
  654. },
  655. ///删除当前设备
  656. deleteDevice(e) {
  657. var that = this;
  658. var index = e.currentTarget.dataset.index;
  659. wx.showModal({
  660. title: '确定删除?',
  661. success: function (res) {
  662. if (res.confirm) {
  663. var deviceList = that.getDeviceList();
  664. if (deviceList[index].connectType == 3) {
  665. that.cancelWifi(index, false);
  666. } else {
  667. console.log("确定删除0", index, deviceList[index].deviceId)
  668. that.cancelBlue(index, false);
  669. }
  670. }
  671. }
  672. });
  673. },
  674. ///是否是同一个蓝牙
  675. isTheSameBlue(device) {
  676. var that = this;
  677. var deviceList = that.getDeviceList();
  678. var deviceListSelect = that.getDeviceListSelect();
  679. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  680. return false;
  681. };
  682. if (deviceList[deviceListSelect].connectType == 3) {
  683. return false;
  684. } else {
  685. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  686. return true;
  687. } else {
  688. return false;
  689. }
  690. }
  691. },
  692. ///断开当前的
  693. cancelCurrent() {
  694. var that = this;
  695. var deviceList = that.getDeviceList();
  696. var deviceListSelect = that.getDeviceListSelect();
  697. if (deviceList[deviceListSelect].connectType == 3) {
  698. that.cancelWifi(deviceListSelect, true);
  699. } else {
  700. that.cancelBlue(deviceListSelect, true);
  701. }
  702. },
  703. /// 断开蓝牙连接
  704. cancelBlue(index, onlyCancel) {
  705. var that = this;
  706. if (onlyCancel) {
  707. return;
  708. }
  709. var deviceList = that.getDeviceList();
  710. var deviceListSelect = that.getDeviceListSelect();
  711. ///是否删除的当前播放这个
  712. var isCurrentIndex = false;
  713. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  714. isCurrentIndex = (deviceListSelect == index);
  715. };
  716. let device = deviceList[index];
  717. ///删除当前设备
  718. var deviceId = device.deviceId;
  719. deviceList = deviceList.filter((item, i) => {
  720. return deviceId !== item.deviceId;
  721. });
  722. store.setStore("deviceList", deviceList);
  723. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  724. var indexPage = that.data.indexPage;
  725. that.setData({
  726. deviceList: deviceList,
  727. indexPage: indexPage == 1 ? 0 : indexPage,
  728. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  729. });
  730. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  731. if (device.state === "online") {
  732. BtHelper.getInstance().disconnect({
  733. "deviceId": deviceId
  734. });
  735. }
  736. },
  737. /// 断开连接wifi
  738. cancelWifi(index, onlyCancel) {
  739. var that = this;
  740. var deviceList = that.getDeviceList();
  741. if (index === null || deviceList.length <= index) {
  742. return;
  743. };
  744. var deviceId = deviceList[index].deviceId;
  745. // 取消订阅
  746. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  747. app.unsubscribe(pubResponse);
  748. if (onlyCancel) {
  749. return;
  750. }
  751. ///是否删除的当前播放这个
  752. var isCurrentIndex = false;
  753. var deviceListSelect = that.getDeviceListSelect();
  754. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  755. isCurrentIndex = (deviceListSelect == index);
  756. };
  757. deviceList = deviceList.filter((item, i) => {
  758. return deviceId !== item.deviceId;
  759. });
  760. store.setStore("deviceList", deviceList);
  761. var indexPage = that.data.indexPage;
  762. var deviceMacId = that.data.deviceMacId;
  763. var actionIndex = that.data.actionIndex;
  764. that.setData({
  765. deviceList: deviceList,
  766. deviceMacId: isCurrentIndex ? null : deviceMacId,
  767. actionIndex: isCurrentIndex ? null : actionIndex,
  768. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  769. indexPage: indexPage == 1 ? 0 : indexPage,
  770. });
  771. },
  772. /// 新添加蓝牙设备
  773. addBlueDevice(device) {
  774. var that = this;
  775. var addDeviceList = lexin_add.addBlueDevice(device);
  776. let hasOnline = 0
  777. addDeviceList.forEach(item => {
  778. // 蓝牙设备下线。wifi不动
  779. if (item.deviceId != device.deviceId && item.connectType === 1) {
  780. item.state = "offline"
  781. }
  782. })
  783. console.log("新添加蓝牙设备", addDeviceList)
  784. that.updateDeviceList(addDeviceList, false, false);
  785. that.setData({
  786. deviceListSelect: 0,
  787. autoConnected: true,
  788. });
  789. that.addNotification()
  790. },
  791. disconnectDev(event, device) {
  792. let _this = this;
  793. if (_this.data._clickOtherDevice) {
  794. _this.data._clickOtherDevice = false
  795. return;
  796. }
  797. console.log("断开连接--", event.commonValue, event.deviceId);
  798. if (event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  799. _this.data._hasShowModal = true
  800. wx.showModal({
  801. title: '提示',
  802. content: '设备断开连接,请重新连接设备',
  803. showCancel: false,
  804. success: function (res) {
  805. _this.data._hasShowModal = false;
  806. if (res.confirm) {
  807. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  808. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  809. }
  810. }
  811. }
  812. })
  813. }
  814. // 设备断开连接,请重新连接设备
  815. },
  816. addNotification() {
  817. let that = this;
  818. eventBus.removeNotification(CmdEvent.eventName, that);
  819. eventBus.addNotification(CmdEvent.eventName, function (event) {
  820. let deviceList = that.getDeviceList()
  821. if (event.cmdEvent == EnumCmdEvent.btMac) {
  822. let disDevice = deviceList.find(item => {
  823. return item.state == "online" && item.connectType === 1
  824. })
  825. if (disDevice) {
  826. disDevice.mac = event.btMac
  827. console.log("设备mac:", disDevice.mac)
  828. }
  829. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  830. let isFirst = false;
  831. // 有下线就开启监听扫描
  832. if (event.commonValue == "online") {
  833. console.log("有蓝牙设备上线")
  834. that.stopIntervalId1();
  835. } else {
  836. // 有wifi设备在线
  837. let wifiDevice = deviceList.find(item => {
  838. return item.state == "online" && item.connectType === 3
  839. })
  840. if (!wifiDevice) {
  841. that.startBleTimer(isFirst);
  842. }
  843. }
  844. console.log("首页ble通知:", event.deviceId, event.commonValue)
  845. let hasOnleBle = deviceList.find(item => {
  846. return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
  847. })
  848. let disDevice = deviceList.find(item => {
  849. return item.deviceId == event.deviceId && item.connectType == 1
  850. })
  851. // that.data.deviceList.forEach(element => {
  852. // if (item.deviceId == tmp.deviceId) {
  853. // }
  854. // });
  855. if (!disDevice) {
  856. console.log("没有找到在线的对应的蓝牙设备")
  857. return;
  858. }
  859. disDevice.state = event.commonValue;
  860. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  861. if (event.commonValue == "online") {
  862. // BtHelper.getInstance().connect()
  863. console.log("首页连接上线", disDevice)
  864. } else {
  865. if (hasOnleBle) {
  866. that.disconnectDev(event, disDevice)
  867. }
  868. console.log("首页连接断开", disDevice)
  869. that.updateDeviceList(deviceList, false, false);
  870. }
  871. }
  872. }, that);
  873. },
  874. // 新添加Wifi设备
  875. addWifiDevice(device) {
  876. var that = this;
  877. var isOK = app.globalData.client && app.globalData.client.connected;
  878. if (!isOK) {
  879. app.connect();
  880. setTimeout(() => {
  881. that.addWifiDevice(device);
  882. }, 500);
  883. return;
  884. };
  885. console.log("有新的wifi设备0", device)
  886. var deviceId = device.deviceId;
  887. var addDeviceList = lexin_add.addWifiDevice(device);
  888. console.log("有新的wifi设备", addDeviceList)
  889. that.updateDeviceList(addDeviceList, false, false);
  890. var indexPage = that.data.indexPage;
  891. that.setData({
  892. deviceListSelect: null,
  893. connectDeviceIding: deviceId,
  894. indexPage: indexPage == 1 ? 0 : indexPage,
  895. });
  896. lexin_subscribe.subscribeSingleDevice(device);
  897. },
  898. refreshCurrentDevice(device) {
  899. var that = this;
  900. console.log("刷新列表0:", device)
  901. var addDeviceList = lexin_add.addWifiDevice(device);
  902. console.log("刷新列表1:", addDeviceList)
  903. that.updateDeviceList(addDeviceList, false, false);
  904. that.setData({
  905. autoConnected: true,
  906. deviceListSelect: 0,
  907. connectDeviceIding: "",
  908. });
  909. },
  910. /// 手机关闭蓝牙,所有蓝牙设备离线
  911. closeBlueResetOffline(isInit, closeAllBlue) {
  912. var that = this;
  913. var deviceList = that.getDeviceList();
  914. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  915. },
  916. /// 更新列表排序
  917. updateDeviceList(deviceList, isInit, closeAllBlue) {
  918. var that = this;
  919. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  920. store.setStore("deviceList", finalList);
  921. that.setData({
  922. deviceList: finalList,
  923. });
  924. },
  925. ///点击banner事件
  926. onTapBanner(e) {
  927. var item = e.currentTarget.dataset.item;
  928. try {
  929. if (strings.isEmpty(item.forwardUrl)) {
  930. return;
  931. }
  932. // 内链,暂时不支持
  933. if (item.forwardType == 0) {
  934. routeUtil.jump(item.forwardUrl, item.forwardUrl);
  935. }
  936. // 外链
  937. else if (item.forwardType == 1) {
  938. var param = "?param=" + item.forwardUrl;
  939. routeUtil.jumpParam(routePath.webview, param);
  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. var isLogin = that.getIsLogin();
  954. if (!isLogin) {
  955. routeUtil.jump(routePath.login);
  956. return;
  957. }
  958. routeUtil.jump(routePath.deviceList);
  959. },
  960. // 关于我们
  961. jumpToAboutUs() {
  962. lexin_jump.toAboutUs();
  963. },
  964. /// 是否已登录
  965. getIsLogin() {
  966. var that = this;
  967. var isLogin = that.data.isLogin;
  968. return isLogin;
  969. },
  970. /// 获取列表数据
  971. getDeviceList() {
  972. var that = this;
  973. var deviceList = that.data.deviceList;
  974. return deviceList;
  975. },
  976. ///获取当前选中的
  977. getDeviceListSelect() {
  978. var that = this;
  979. var deviceListSelect = that.data.deviceListSelect;
  980. return deviceListSelect;
  981. },
  982. ///获取是否已经第一次连接过
  983. getAutoConnected() {
  984. var that = this;
  985. var autoConnected = that.data.autoConnected;
  986. return autoConnected;
  987. },
  988. ///当前正在连接的设备id
  989. getConnectDeviceIding() {
  990. var that = this;
  991. var connectDeviceIding = that.data.connectDeviceIding
  992. return connectDeviceIding;
  993. },
  994. ///获取频道数据
  995. getChannelData() {
  996. var that = this;
  997. var channelData = that.data.channelData;
  998. return channelData;
  999. },
  1000. /// 当前播放频道
  1001. getActionIndex() {
  1002. var that = this;
  1003. var actionIndex = that.data.actionIndex;
  1004. return actionIndex;
  1005. },
  1006. /// 获取当前的device
  1007. getCurrentDevice() {
  1008. var that = this;
  1009. var deviceList = that.getDeviceList();
  1010. var deviceListSelect = that.getDeviceListSelect();
  1011. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  1012. return null;
  1013. };
  1014. var device = deviceList[deviceListSelect];
  1015. return device;
  1016. },
  1017. /// 获取当前的deviceId
  1018. getCurrentDeviceId() {
  1019. var that = this;
  1020. var deviceList = that.getDeviceList();
  1021. var deviceListSelect = that.getDeviceListSelect();
  1022. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  1023. return "";
  1024. };
  1025. var deviceId = deviceList[deviceListSelect].deviceId;
  1026. return deviceId;
  1027. },
  1028. ///设置设备相关信息
  1029. setDeviceInfor(other) {
  1030. var that = this;
  1031. // 接收设备当前信息
  1032. that.getchannelData(other.ProdModel);
  1033. // 电量
  1034. if (other.Power) {
  1035. var battery = lexin_message.getBattery(other.Power);
  1036. that.setData({
  1037. battery: battery,
  1038. });
  1039. }
  1040. // 当前设备木有设置定时
  1041. that.setData({
  1042. isSetWake: false,
  1043. });
  1044. other.alarm.map((v) => {
  1045. if (v.enable === "1") {
  1046. that.setData({
  1047. isSetWake: true
  1048. });
  1049. }
  1050. });
  1051. },
  1052. ///******************************* 可折叠 ********************************************///
  1053. onUserInfoLoad() {
  1054. var that = this;
  1055. var userInfo = wx.getStorageSync("userInfo") || "";
  1056. if (!strings.isEmpty(userInfo)) {
  1057. var phone = userInfo.phone;
  1058. if (!phone) {
  1059. return;
  1060. };
  1061. var nickName = userInfo.nickname || "";
  1062. var greeting = that.getGreetBuNickName(nickName);
  1063. // 获取缓存的频道数据
  1064. var channelData = wx.getStorageSync("channelData") || "";
  1065. if (!strings.isEmpty(channelData)) {
  1066. that.setData({
  1067. channelData: channelData,
  1068. greeting: greeting,
  1069. nickName: nickName,
  1070. userPic: userInfo.headUrl || "",
  1071. isLogin: true,
  1072. })
  1073. } else {
  1074. that.setData({
  1075. greeting: greeting,
  1076. nickName: nickName,
  1077. userPic: userInfo.headUrl || "",
  1078. isLogin: true,
  1079. });
  1080. }
  1081. } else {
  1082. var greeting = that.getGreetBuNickName("");
  1083. that.setData({
  1084. greeting: greeting,
  1085. })
  1086. }
  1087. },
  1088. getGreetBuNickName(nickName) {
  1089. var greeting = timeUtil.getGreet();
  1090. if (nickName != "" && nickName != "未登录") {
  1091. greeting = greeting + ',' + nickName;;
  1092. }
  1093. return greeting;
  1094. },
  1095. onBannerLoad() {
  1096. var that = this;
  1097. getBanner({}).then((res) => {
  1098. that.setData({
  1099. bannerList: res,
  1100. });
  1101. store.setStore("homeBanner", res);
  1102. });
  1103. },
  1104. stopIntervalId1: function () {
  1105. var that = this;
  1106. if (!strings.isEmpty(that.data.intervalId1)) {
  1107. clearInterval(that.data.intervalId1);
  1108. that.data.intervalId1 = null;
  1109. }
  1110. },
  1111. stopIntervalId2: function () {
  1112. var that = this;
  1113. if (!strings.isEmpty(that.data.intervalId2)) {
  1114. clearInterval(that.data.intervalId2);
  1115. that.data.intervalId2 = null;
  1116. }
  1117. },
  1118. ///销毁蓝牙
  1119. onUnload() {
  1120. var that = this;
  1121. eventBus.removeNotification(CmdEvent.eventName, that)
  1122. console.log("界面onUnload")
  1123. that.stopIntervalId1();
  1124. that.stopIntervalId2();
  1125. if (!strings.isEmpty(that.data.intervalId)) {
  1126. clearInterval(that.data.intervalId);
  1127. that.data.intervalId = null;
  1128. }
  1129. BtHelper.getInstance().disconnect();
  1130. if (app.globalData.client === null) {
  1131. return;
  1132. };
  1133. app.globalData.client.end(true);
  1134. app.globalData.client.end(true);
  1135. app.globalData.client = null;
  1136. },
  1137. })