index.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. const app = getApp();
  2. const {
  3. login,
  4. listByDevice,
  5. getBanner
  6. } = require('../../utils/api.js');
  7. import time_util from '../../utils/time_util.js';
  8. import strings from '../../utils/strings.js';
  9. import store from '../../utils/store.js';
  10. import route_constant from '../../utils/route_constant.js';
  11. import route_util from '../../utils/route_util.js';
  12. /// 乐鑫相关
  13. import lexin_util from '../../utils/lexin/util.js';
  14. import lexin_jump from '../../utils/lexin/jump.js';
  15. import lexin_add from '../../utils/lexin/add.js';
  16. import lexin_scan from '../../utils/lexin/scan.js';
  17. import lexin_subscribe from '../../utils/lexin/subscribe.js';
  18. import lexin_connect from '../../utils/lexin/connect.js';
  19. import lexin_message from '../../utils/lexin/message.js';
  20. import {
  21. BtHelper
  22. } from '../../devices/bt_helper.js';
  23. import event_bus from '../../utils/event_bus.js';
  24. import {
  25. EnumCmdEvent,
  26. CmdEvent
  27. } from '../../devices/cmd_key_event';
  28. Page({
  29. data: {
  30. navBarHeight: app.globalData.navBarHeight,
  31. safeBottomHeight: app.globalData.safeBottomHeight,
  32. nvabarData: {
  33. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  34. title: 'OhPlay', //导航栏 中间的标题
  35. },
  36. ///下午好
  37. isLogin: false,
  38. greeting: "",
  39. bannerList: [{
  40. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  41. },
  42. {
  43. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  44. }
  45. ],
  46. autoplay: true,
  47. interval: 3000, // 切换时间间隔
  48. duration: 500, // 滑动动画时长
  49. circular: true, // 衔接滑动
  50. indexPage: 0,
  51. ///是否展示频道
  52. showChannel: false,
  53. ////首页
  54. navBarHeight: app.globalData.navBarHeight,
  55. MenuButtonheight: app.globalData.MenuButtonheight,
  56. MenuButtonTop: app.globalData.MenuButtonTop,
  57. actionIndex: null,
  58. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  59. channelData: [],
  60. isOneLoading: true,
  61. uid: null,
  62. isSetWake: false,
  63. battery: 4, // 0≤电量<20,0格
  64. ////我的界面
  65. loginStatus: true,
  66. nickName: "未登录",
  67. userPic: './../../img/head_pic.png',
  68. intervalId: null,
  69. /// 是否第一次自动连接过
  70. autoConnected: false,
  71. intervalId1: null,
  72. intervalId2: null,
  73. deviceMacId: null,
  74. deviceListSelect: null,
  75. deviceList: [{
  76. "connectType": 3,
  77. // "mac": "AIrSMArT_861210052356337",
  78. // "deviceId": "D0:31:10:86:AC:9A"
  79. "name": "MW-X4(BZK)",
  80. "deviceId": "AIrSMArT_861210052356337"
  81. }],
  82. _willConnectBle: null,
  83. _hasShowModal: null,
  84. // deviceList: [{
  85. // "connectType": 3,
  86. // "devName": "猫王小王子OTR-X",
  87. // "state": "online",
  88. // "name": "BLUFI_7cdfa1fcbb24",
  89. // "deviceId": "BLUFI_7cdfa1fcbb24",
  90. // "mac": "BLUFI_7cdfa1fcbb24",
  91. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  92. // "ProdModel": "MW-2AX(WIFI-N)",
  93. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  94. // }, {
  95. // "connectType": 3,
  96. // "devName": "猫王小王子OTR-X",
  97. // "state": "online",
  98. // "name": "BLUFI_7cdfa1fcbb24",
  99. // "deviceId": "BLUFI_7cdfa1fcbb24",
  100. // "mac": "BLUFI_7cdfa1fcbb24",
  101. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  102. // "ProdModel": "MW-2AX(WIFI-N)",
  103. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  104. // }, {
  105. // "connectType": 3,
  106. // "devName": "猫王小王子OTR-X",
  107. // "state": "online",
  108. // "name": "BLUFI_7cdfa1fcbb24",
  109. // "deviceId": "BLUFI_7cdfa1fcbb24",
  110. // "mac": "BLUFI_7cdfa1fcbb24",
  111. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  112. // "ProdModel": "MW-2AX(WIFI-N)",
  113. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  114. // }, {
  115. // "connectType": 3,
  116. // "devName": "猫王小王子OTR-X",
  117. // "state": "online",
  118. // "name": "BLUFI_7cdfa1fcbb24",
  119. // "deviceId": "BLUFI_7cdfa1fcbb24",
  120. // "mac": "BLUFI_7cdfa1fcbb24",
  121. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  122. // "ProdModel": "MW-2AX(WIFI-N)",
  123. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  124. // }, {
  125. // "connectType": 3,
  126. // "devName": "猫王小王子OTR-X",
  127. // "state": "online",
  128. // "name": "BLUFI_7cdfa1fcbb24",
  129. // "deviceId": "BLUFI_7cdfa1fcbb24",
  130. // "mac": "BLUFI_7cdfa1fcbb24",
  131. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  132. // "ProdModel": "MW-2AX(WIFI-N)",
  133. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  134. // }, {
  135. // "connectType": 3,
  136. // "devName": "猫王小王子OTR-X",
  137. // "state": "online",
  138. // "name": "BLUFI_7cdfa1fcbb24",
  139. // "deviceId": "BLUFI_7cdfa1fcbb24",
  140. // "mac": "BLUFI_7cdfa1fcbb24",
  141. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  142. // "ProdModel": "MW-2AX(WIFI-N)",
  143. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  144. // }],
  145. connectDeviceIding: "",
  146. },
  147. onLoad(options) {
  148. var that = this;
  149. that.onLoginLoad();
  150. that.onDeviceLoad();
  151. that.onUserInfoLoad();
  152. that.onBannerLoad();
  153. // var device = {
  154. // "mac": "AIrSMArT_861210052356337",
  155. // "deviceId": "D0:31:10:86:AC:9A"
  156. // };
  157. // that.data.deviceList.unshift(device);
  158. // that.data.deviceMacId = device.mac;
  159. // that.actionDevice();
  160. },
  161. onShow() {
  162. var that = this;
  163. that.onBannerShow();
  164. },
  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. route_util.jump(route_constant.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 deviceList = that.getDeviceList();
  517. var deviceListSelect = that.getDeviceListSelect();
  518. if (deviceList.length > 1 && (deviceListSelect === null || deviceList.length <= deviceListSelect)) {
  519. // 2个设备才判断,1个直接删除
  520. return;
  521. };
  522. deviceListSelect = deviceListSelect ?? 0
  523. ///有设备在线被选中,则让它不被选择
  524. if (deviceList[deviceListSelect].connectType == 3) {
  525. var deviceMacId = that.data.deviceMacId;
  526. if (!strings.isEmpty(deviceMacId)) {
  527. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  528. app.unsubscribe(pubResponse);
  529. var indexPage = that.data.indexPage;
  530. that.setData({
  531. deviceMacId: null,
  532. actionIndex: null,
  533. deviceListSelect: null,
  534. indexPage: indexPage == 1 ? 0 : indexPage,
  535. });
  536. };
  537. } else {
  538. // console.log("退出登录,断开连接", deviceList);
  539. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  540. // var indexPage = that.data.indexPage;
  541. // that.setData({
  542. // deviceMacId: null,
  543. // actionIndex: null,
  544. // deviceListSelect: null,
  545. // indexPage: indexPage == 1 ? 0 : indexPage,
  546. // });
  547. }
  548. },
  549. ///点击item
  550. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  551. async onTapItem(e) {
  552. var that = this;
  553. var isLogin = that.getIsLogin();
  554. if (!isLogin) {
  555. route_util.jump(route_constant.login);
  556. return;
  557. }
  558. var index = e.currentTarget.dataset.index;
  559. var device = e.currentTarget.dataset.item;
  560. var connectType = device.connectType;
  561. var deviceListSelect = that.getDeviceListSelect();
  562. // wifi只支持在线点击
  563. if (connectType == 3 && device.state === "online") {
  564. if (index === deviceListSelect) {
  565. that.goWake();
  566. } else {
  567. // 现断线第一个
  568. // that.disconnectFirst()
  569. that.addWifiDevice(device);
  570. }
  571. return;
  572. }
  573. ///去蓝牙连接处理
  574. // {"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 妙播收音机"}
  575. var deviceList = that.getDeviceList();
  576. let curDevice = deviceList[index]
  577. if (index === deviceListSelect && device.state === "online") {
  578. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(curDevice))
  579. } else if (device.state === "offline") {
  580. var deviceListSelect = that.getDeviceListSelect();
  581. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  582. that.setData({
  583. deviceListSelect: null,
  584. });
  585. var currentDevice = deviceList[deviceListSelect];
  586. console.log("断开连接0", currentDevice)
  587. // 现断线第一个
  588. // that.disconnectFirst()
  589. }
  590. console.log("断开连接2", device)
  591. device.name = device.devName;
  592. BtHelper.getInstance().connect(device, function (isConnected, d) {
  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 && deviceList.length > deviceListSelect) {
  602. that.setData({
  603. deviceListSelect: null,
  604. });
  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. device.name = device.devName;
  613. BtHelper.getInstance().connect(device, function (isConnected, d) {
  614. if (isConnected) {
  615. device.state = "online";
  616. that.addBlueDevice(device);
  617. }
  618. });
  619. }
  620. },
  621. disconnectFirst() {
  622. let that = this;
  623. let deviceList = that.getDeviceList()
  624. let first = deviceList[0] ?? {}
  625. if (first.state == "online") {
  626. if (first.connectType === 1) {
  627. BtHelper.getInstance().disconnect(first)
  628. first.state == "offline"
  629. } else {
  630. // 取消订阅
  631. // var pubResponse = lexin_util.getResponseByDeviceId(first.deviceId);
  632. // app.unsubscribe(pubResponse);
  633. }
  634. }
  635. },
  636. // 去唤醒界面
  637. goWake() {
  638. var that = this;
  639. var deviceList = that.getDeviceList();
  640. var deviceListSelect = that.getDeviceListSelect();
  641. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  642. return
  643. };
  644. var device = deviceList[deviceListSelect];
  645. wx.navigateTo({
  646. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  647. });
  648. },
  649. ///删除当前设备
  650. deleteDevice(e) {
  651. var that = this;
  652. var index = e.currentTarget.dataset.index;
  653. wx.showModal({
  654. title: '确定删除?',
  655. success: function (res) {
  656. if (res.confirm) {
  657. var deviceList = that.getDeviceList();
  658. if (deviceList[index].connectType == 3) {
  659. that.cancelWifi(index, false);
  660. } else {
  661. console.log("确定删除0", index, deviceList[index].deviceId)
  662. that.cancelBlue(index, false);
  663. }
  664. }
  665. }
  666. });
  667. },
  668. ///是否是同一个蓝牙
  669. isTheSameBlue(device) {
  670. var that = this;
  671. var deviceList = that.getDeviceList();
  672. var deviceListSelect = that.getDeviceListSelect();
  673. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  674. return false;
  675. };
  676. if (deviceList[deviceListSelect].connectType == 3) {
  677. return false;
  678. } else {
  679. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  680. return true;
  681. } else {
  682. return false;
  683. }
  684. }
  685. },
  686. ///断开当前的
  687. cancelCurrent() {
  688. var that = this;
  689. var deviceList = that.getDeviceList();
  690. var deviceListSelect = that.getDeviceListSelect();
  691. if (deviceList[deviceListSelect].connectType == 3) {
  692. that.cancelWifi(deviceListSelect, true);
  693. } else {
  694. that.cancelBlue(deviceListSelect, true);
  695. }
  696. },
  697. /// 断开蓝牙连接
  698. cancelBlue(index, onlyCancel) {
  699. var that = this;
  700. if (onlyCancel) {
  701. return;
  702. }
  703. var deviceList = that.getDeviceList();
  704. var deviceListSelect = that.getDeviceListSelect();
  705. ///是否删除的当前播放这个
  706. var isCurrentIndex = false;
  707. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  708. isCurrentIndex = (deviceListSelect == index);
  709. };
  710. let device = deviceList[index];
  711. ///删除当前设备
  712. var deviceId = device.deviceId;
  713. deviceList = deviceList.filter((item, i) => {
  714. return deviceId !== item.deviceId;
  715. });
  716. store.setStore("deviceList", deviceList);
  717. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  718. var indexPage = that.data.indexPage;
  719. that.setData({
  720. deviceList: deviceList,
  721. indexPage: indexPage == 1 ? 0 : indexPage,
  722. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  723. });
  724. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  725. if (device.state === "online") {
  726. BtHelper.getInstance().disconnect({
  727. "deviceId": deviceId
  728. });
  729. }
  730. },
  731. /// 断开连接wifi
  732. cancelWifi(index, onlyCancel) {
  733. var that = this;
  734. var deviceList = that.getDeviceList();
  735. if (index === null || deviceList.length <= index) {
  736. return;
  737. };
  738. var deviceId = deviceList[index].deviceId;
  739. // 取消订阅
  740. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  741. app.unsubscribe(pubResponse);
  742. if (onlyCancel) {
  743. return;
  744. }
  745. ///是否删除的当前播放这个
  746. var isCurrentIndex = false;
  747. var deviceListSelect = that.getDeviceListSelect();
  748. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  749. isCurrentIndex = (deviceListSelect == index);
  750. };
  751. deviceList = deviceList.filter((item, i) => {
  752. return deviceId !== item.deviceId;
  753. });
  754. store.setStore("deviceList", deviceList);
  755. var indexPage = that.data.indexPage;
  756. var deviceMacId = that.data.deviceMacId;
  757. var actionIndex = that.data.actionIndex;
  758. that.setData({
  759. deviceList: deviceList,
  760. deviceMacId: isCurrentIndex ? null : deviceMacId,
  761. actionIndex: isCurrentIndex ? null : actionIndex,
  762. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  763. indexPage: indexPage == 1 ? 0 : indexPage,
  764. });
  765. },
  766. /// 新添加蓝牙设备
  767. addBlueDevice(device) {
  768. var that = this;
  769. var addDeviceList = lexin_add.addBlueDevice(device);
  770. let hasOnline = 0
  771. addDeviceList.forEach(item => {
  772. // 蓝牙设备下线。wifi不动
  773. if (item.deviceId != device.deviceId && item.connectType === 1) {
  774. item.state = "offline"
  775. }
  776. })
  777. console.log("新添加蓝牙设备", addDeviceList)
  778. that.updateDeviceList(addDeviceList, false, false);
  779. that.setData({
  780. deviceListSelect: 0,
  781. autoConnected: true,
  782. });
  783. that.addNotification()
  784. },
  785. disconnectDev(event, device) {
  786. let _this = this;
  787. console.log("断开连接--", event.commonValue, event.deviceId);
  788. if (event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  789. _this.data._hasShowModal = true
  790. wx.showModal({
  791. title: '提示',
  792. content: '设备断开连接,请重新连接设备',
  793. showCancel: false,
  794. success: function (res) {
  795. _this.data._hasShowModal = false;
  796. if (res.confirm) {
  797. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  798. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  799. }
  800. }
  801. }
  802. })
  803. }
  804. // 设备断开连接,请重新连接设备
  805. },
  806. addNotification() {
  807. let that = this;
  808. event_bus.removeNotification(CmdEvent.eventName, that);
  809. event_bus.addNotification(CmdEvent.eventName, function (event) {
  810. let deviceList = that.getDeviceList()
  811. if (event.cmdEvent == EnumCmdEvent.btMac) {
  812. let disDevice = deviceList.find(item => {
  813. return item.state == "online" && item.connectType === 1
  814. })
  815. if (disDevice) {
  816. disDevice.mac = event.btMac
  817. console.log("设备mac:", disDevice.mac)
  818. }
  819. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  820. let isFirst = false;
  821. // 有下线就开启监听扫描
  822. if (event.commonValue == "online") {
  823. console.log("有蓝牙设备上线")
  824. that.stopIntervalId1();
  825. } else {
  826. // 有wifi设备在线
  827. let wifiDevice = deviceList.find(item => {
  828. return item.state == "online" && item.connectType === 3
  829. })
  830. if (!wifiDevice) {
  831. that.startBleTimer(isFirst);
  832. }
  833. }
  834. console.log("首页ble通知:", event.deviceId, event.commonValue)
  835. let disDevice = deviceList.find(item => {
  836. return item.deviceId == event.deviceId && item.connectType == 1
  837. })
  838. // that.data.deviceList.forEach(element => {
  839. // if (item.deviceId == tmp.deviceId) {
  840. // }
  841. // });
  842. if (!disDevice) {
  843. console.log("没有找到在线的对应的蓝牙设备")
  844. return;
  845. }
  846. disDevice.state = event.commonValue;
  847. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  848. if (event.commonValue == "online") {
  849. // BtHelper.getInstance().connect()
  850. console.log("首页连接上线", disDevice)
  851. } else {
  852. that.disconnectDev(event, disDevice)
  853. console.log("首页连接断开", disDevice)
  854. that.updateDeviceList(deviceList, false, false);
  855. }
  856. }
  857. }, that);
  858. },
  859. // 新添加Wifi设备
  860. addWifiDevice(device) {
  861. var that = this;
  862. var isOK = app.globalData.client && app.globalData.client.connected;
  863. if (!isOK) {
  864. app.connect();
  865. setTimeout(() => {
  866. that.addWifiDevice(device);
  867. }, 500);
  868. return;
  869. };
  870. console.log("有新的wifi设备0", device)
  871. var deviceId = device.deviceId;
  872. var addDeviceList = lexin_add.addWifiDevice(device);
  873. console.log("有新的wifi设备", addDeviceList)
  874. that.updateDeviceList(addDeviceList, false, false);
  875. var indexPage = that.data.indexPage;
  876. that.setData({
  877. deviceListSelect: null,
  878. connectDeviceIding: deviceId,
  879. indexPage: indexPage == 1 ? 0 : indexPage,
  880. });
  881. lexin_subscribe.subscribeSingleDevice(device);
  882. },
  883. refreshCurrentDevice(device) {
  884. var that = this;
  885. console.log("刷新列表0:", device)
  886. var addDeviceList = lexin_add.addWifiDevice(device);
  887. console.log("刷新列表1:", addDeviceList)
  888. that.updateDeviceList(addDeviceList, false, false);
  889. that.setData({
  890. autoConnected: true,
  891. deviceListSelect: 0,
  892. connectDeviceIding: "",
  893. });
  894. },
  895. /// 手机关闭蓝牙,所有蓝牙设备离线
  896. closeBlueResetOffline(isInit, closeAllBlue) {
  897. var that = this;
  898. var deviceList = that.getDeviceList();
  899. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  900. },
  901. /// 更新列表排序
  902. updateDeviceList(deviceList, isInit, closeAllBlue) {
  903. var that = this;
  904. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  905. store.setStore("deviceList", finalList);
  906. that.setData({
  907. deviceList: finalList,
  908. });
  909. },
  910. ///点击banner事件
  911. onTapBanner(e) {
  912. var item = e.currentTarget.dataset.item;
  913. try {
  914. if (!strings.isEmpty(item.forwardUrl)) {
  915. return
  916. }
  917. if (item.forwardType == 0) {
  918. // 内链,暂时不支持
  919. route_util.jump(item.forwardUrl, item.forwardUrl);
  920. } else if (item.forwardType == 1) {
  921. // 外链
  922. route_util.jumpParam(route_constant.webview, item.forwardUrl);
  923. }
  924. } catch (err) {
  925. console.log("跳转失败", err)
  926. }
  927. },
  928. ///添加设备
  929. jumpToAddDevice() {
  930. var that = this;
  931. // route_util.jump("../deviceConnect4/deviceConnect4");
  932. // return;
  933. var isLogin = that.getIsLogin();
  934. if (!isLogin) {
  935. route_util.jump(route_constant.login);
  936. return;
  937. }
  938. route_util.jump(route_constant.deviceList);
  939. },
  940. // 关于我们
  941. jumpToAboutUs() {
  942. lexin_jump.toAboutUs();
  943. },
  944. /// 是否已登录
  945. getIsLogin() {
  946. var that = this;
  947. var isLogin = that.data.isLogin;
  948. return isLogin;
  949. },
  950. /// 获取列表数据
  951. getDeviceList() {
  952. var that = this;
  953. var deviceList = that.data.deviceList;
  954. return deviceList;
  955. },
  956. ///获取当前选中的
  957. getDeviceListSelect() {
  958. var that = this;
  959. var deviceListSelect = that.data.deviceListSelect;
  960. return deviceListSelect;
  961. },
  962. ///获取是否已经第一次连接过
  963. getAutoConnected() {
  964. var that = this;
  965. var autoConnected = that.data.autoConnected;
  966. return autoConnected;
  967. },
  968. ///当前正在连接的设备id
  969. getConnectDeviceIding() {
  970. var that = this;
  971. var connectDeviceIding = that.data.connectDeviceIding
  972. return connectDeviceIding;
  973. },
  974. ///获取频道数据
  975. getChannelData() {
  976. var that = this;
  977. var channelData = that.data.channelData;
  978. return channelData;
  979. },
  980. /// 当前播放频道
  981. getActionIndex() {
  982. var that = this;
  983. var actionIndex = that.data.actionIndex;
  984. return actionIndex;
  985. },
  986. /// 获取当前的deviceId
  987. getCurrentDeviceId() {
  988. var that = this;
  989. var deviceList = that.getDeviceList();
  990. var deviceListSelect = that.getDeviceListSelect();
  991. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  992. return "";
  993. };
  994. var deviceId = deviceList[deviceListSelect].deviceId;
  995. return deviceId;
  996. },
  997. ///设置设备相关信息
  998. setDeviceInfor(other) {
  999. var that = this;
  1000. // 接收设备当前信息
  1001. that.getchannelData(other.ProdModel);
  1002. // 电量
  1003. if (other.Power) {
  1004. var battery = lexin_message.getBattery(other.Power);
  1005. that.setData({
  1006. battery: battery,
  1007. });
  1008. }
  1009. // 当前设备木有设置定时
  1010. that.setData({
  1011. isSetWake: false,
  1012. });
  1013. other.alarm.map((v) => {
  1014. if (v.enable === "1") {
  1015. that.setData({
  1016. isSetWake: true
  1017. });
  1018. }
  1019. });
  1020. },
  1021. ///******************************* 可折叠 ********************************************///
  1022. onUserInfoLoad() {
  1023. var that = this;
  1024. var userInfo = wx.getStorageSync("userInfo") || "";
  1025. if (!strings.isEmpty(userInfo)) {
  1026. var phone = userInfo.phone;
  1027. if (!phone) {
  1028. return;
  1029. };
  1030. var nickName = userInfo.nickname || "";
  1031. var greeting = that.getGreetBuNickName(nickName);
  1032. // 获取缓存的频道数据
  1033. var channelData = wx.getStorageSync("channelData") || "";
  1034. if (!strings.isEmpty(channelData)) {
  1035. that.setData({
  1036. channelData: channelData,
  1037. greeting: greeting,
  1038. nickName: nickName,
  1039. userPic: userInfo.headUrl || "",
  1040. isLogin: true,
  1041. })
  1042. } else {
  1043. that.setData({
  1044. greeting: greeting,
  1045. nickName: nickName,
  1046. userPic: userInfo.headUrl || "",
  1047. isLogin: true,
  1048. });
  1049. }
  1050. } else {
  1051. var greeting = that.getGreetBuNickName("");
  1052. that.setData({
  1053. greeting: greeting,
  1054. })
  1055. }
  1056. },
  1057. getGreetBuNickName(nickName) {
  1058. var greeting = time_util.getGreet();
  1059. if (nickName != "" && nickName != "未登录") {
  1060. greeting = greeting + ',' + nickName;;
  1061. }
  1062. return greeting;
  1063. },
  1064. onBannerLoad() {
  1065. var that = this;
  1066. // var bannerList = wx.getStorageSync("homeBanner") || [];
  1067. // if (that.data.bannerList.length > 0) {
  1068. // that.setData({
  1069. // bannerList: bannerList
  1070. // });
  1071. // }
  1072. },
  1073. onBannerShow() {
  1074. var that = this;
  1075. getBanner({}).then((res) => {
  1076. console.log("banner:", res)
  1077. that.setData({
  1078. bannerList: res
  1079. });
  1080. store.setStore("homeBanner", res);
  1081. })
  1082. },
  1083. stopIntervalId1: function () {
  1084. var that = this;
  1085. if (!strings.isEmpty(that.data.intervalId1)) {
  1086. clearInterval(that.data.intervalId1);
  1087. that.data.intervalId1 = null;
  1088. }
  1089. },
  1090. stopIntervalId2: function () {
  1091. var that = this;
  1092. if (!strings.isEmpty(that.data.intervalId2)) {
  1093. clearInterval(that.data.intervalId2);
  1094. that.data.intervalId2 = null;
  1095. }
  1096. },
  1097. ///销毁蓝牙
  1098. onUnload() {
  1099. var that = this;
  1100. event_bus.removeNotification(CmdEvent.eventName, that)
  1101. console.log("界面onUnload")
  1102. that.stopIntervalId1();
  1103. that.stopIntervalId2();
  1104. if (!strings.isEmpty(that.data.intervalId)) {
  1105. clearInterval(that.data.intervalId);
  1106. that.data.intervalId = null;
  1107. }
  1108. BtHelper.getInstance().disconnect();
  1109. if (app.globalData.client === null) {
  1110. return;
  1111. };
  1112. app.globalData.client.end(true);
  1113. app.globalData.client.end(true);
  1114. app.globalData.client = null;
  1115. },
  1116. })