index.js 35 KB

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