index.js 33 KB

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