index.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. autoplay: true,
  22. interval: 3000, // 切换时间间隔
  23. duration: 500, // 滑动动画时长
  24. circular: true, // 衔接滑动
  25. indexPage: 0,
  26. ///是否展示频道
  27. showChannel: false,
  28. ////首页
  29. navBarHeight: constant.app.globalData.navBarHeight,
  30. MenuButtonheight: constant.app.globalData.MenuButtonheight,
  31. MenuButtonTop: constant.app.globalData.MenuButtonTop,
  32. actionIndex: null,
  33. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  34. channelData: [],
  35. isOneLoading: true,
  36. uid: null,
  37. isSetWake: false,
  38. battery: 4, // 0≤电量<20,0格
  39. ////我的界面
  40. loginStatus: true,
  41. nickName: "未登录",
  42. userPic: './../../img/head_pic.png',
  43. intervalId: null,
  44. /// 是否第一次自动连接过
  45. autoConnected: false,
  46. intervalId1: null,
  47. intervalId2: null,
  48. deviceMacId: null,
  49. deviceListSelect: null,
  50. deviceList: [],
  51. _willConnectBle: null,
  52. _hasShowModal: null,
  53. _clickOtherDevice: false,
  54. connectDeviceIding: "",
  55. },
  56. onLoad(options) {
  57. var that = this;
  58. that.onLoginLoad();
  59. that.onDeviceLoad();
  60. that.onUserInfoLoad();
  61. that.onBannerLoad();
  62. },
  63. onShow() { },
  64. onLoginLoad() {
  65. var that = this;
  66. var userInfo = wx.getStorageSync("userInfo") || "";
  67. if (!constant.strings.isEmpty(userInfo)) {
  68. var phone = userInfo.phone;
  69. if (!constant.strings.isEmpty(phone)) {
  70. that.loginReset(phone, null, null, null);
  71. };
  72. }
  73. },
  74. ///重新登录
  75. loginReset(phone, succeedCall1, succeedCall2, failCall) {
  76. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
  77. var that = this;
  78. wx.login({
  79. success: res0 => {
  80. // {"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}
  81. constant.apiUtil.login({
  82. code: res0.code,
  83. phone: phone,
  84. }).then((res1) => {
  85. constant.app.globalData.userInfo = res1;
  86. constant.store.setStore("userInfo", res1);
  87. that.setData({
  88. isLogin: true,
  89. });
  90. constant.store.setStore("token", JSON.stringify({
  91. id: res1.userId,
  92. token: res1.accessToken,
  93. }));
  94. that.onUserInfoLoad();
  95. if (succeedCall1 != null) {
  96. succeedCall1();
  97. }
  98. if (succeedCall2 != null) {
  99. succeedCall2();
  100. }
  101. });
  102. },
  103. fail(fail4) {
  104. if (failCall != null) {
  105. failCall();
  106. }
  107. },
  108. });
  109. },
  110. testData() {
  111. return {
  112. "deviceId": "xxxxx",
  113. "deviceName": "xxxxxx",
  114. "deviceType": "xxxx",
  115. "type": 1,
  116. "deviceIcon": "xxxx",
  117. "deviceMacId": "xxxx",
  118. "deviceBattery": 4,
  119. "state": "online",
  120. "connectType": 1,
  121. "clientType": "猫王妙播黑胶音箱(BLE)",
  122. "deviceConnectStatus": 1,
  123. "deviceConnectStatusText": "xxxx",
  124. }
  125. },
  126. onDeviceLoad() {
  127. var that = this;
  128. var deviceList = constant.store.getStore("deviceList");
  129. if (!constant.strings.isEmpty(deviceList)) {
  130. // deviceList = deviceList.concat(deviceList);
  131. that.updateDeviceList(deviceList, true, false);
  132. }
  133. // that.addBlueDevice(that.testData())
  134. let hasBle = that.data.deviceList.find(item => {
  135. return item.connectType == 1;
  136. })
  137. if (hasBle) {
  138. ///监听蓝牙设备
  139. BtHelper.getInstance().initBluetoothAdapter();
  140. BtHelper.getInstance().getBluetoothDevices();
  141. BtHelper.getInstance().startScan(null, null, function (res) { });
  142. var isFirst = true;
  143. that.startBleTimer(isFirst, true);
  144. that.addNotification()
  145. }
  146. },
  147. startBleTimer(isFirst, openTimer) {
  148. let that = this;
  149. let deviceList = that.getDeviceList()
  150. let findBle = deviceList.find(item => {
  151. return (item.connectType === 1 && item.state === "online")
  152. })
  153. let findWifi = deviceList.find(item => {
  154. return (item.connectType === 3 && item.state === "online")
  155. })
  156. if (!openTimer && (findBle || findWifi)) {
  157. // 强制开启监听 && (有蓝牙在线 || 有wifi在线)就return
  158. return;
  159. }
  160. ///再秒再对比一次
  161. that.stopIntervalId1();
  162. let count = 0;
  163. that.data.intervalId1 = setInterval(async function () {
  164. console.log("开始扫描蓝牙设备");
  165. that.compareList();
  166. // if (count > 4) {
  167. if (isFirst) {
  168. that.stopIntervalId1();
  169. var newIsFirst = false
  170. that.startBleTimer(newIsFirst, true);
  171. }
  172. // } else {
  173. // count++;
  174. // }
  175. isFirst = false;
  176. }, isFirst ? 3 * 1000 : 10 * 1000);
  177. },
  178. /// 对比蓝牙数据
  179. compareList() {
  180. let that = this;
  181. constant.lexinScan.compareList2(that.data.deviceList, function (device) {
  182. if (device) {
  183. console.log("找到蓝牙了", JSON.stringify(device));
  184. that.data._willConnectBle = device;
  185. getApp().getBluetoothStatus();
  186. }
  187. });
  188. // constant.lexinScan.compareList(function (list) {
  189. // console.log("对比蓝牙数据", list);
  190. // that.updateDeviceList(list, false, false);
  191. // }, function (item) {
  192. // that.addBlueDevice(item);
  193. // });
  194. },
  195. getBluetoothStatusCallck(v) {
  196. var that = this;
  197. if (!that.data._willConnectBle) {
  198. // true 走下面
  199. return;
  200. }
  201. if (v) {
  202. var device = that.data._willConnectBle;
  203. console.log("开始连接设备0", device);
  204. // if (device.state === 'online' || device.connectType != 1) {
  205. // return;
  206. // }
  207. BtHelper.getInstance().connect(device, function (isConnected, d) {
  208. console.log("自动连接成功", isConnected)
  209. if (isConnected) {
  210. let hasOtherOnline = false;
  211. that.data.deviceList.forEach(item => {
  212. if (item.state === 'online') {
  213. if (item.deviceId == device.deviceId) { } else {
  214. hasOtherOnline = true;
  215. return;
  216. }
  217. }
  218. })
  219. if (!hasOtherOnline) {
  220. // 自己已经在线,没有其他设备在线, 直接更新
  221. console.log("自己已经在线,没有其他设备在线, 直接更新");
  222. that.addBlueDevice(device);
  223. } else {
  224. let bleDev = that.data.deviceList.find(item => {
  225. return item.deviceId === device.deviceId;
  226. });
  227. if (bleDev) {
  228. bleDev.state = 'online';
  229. }
  230. that.updateDeviceList(that.data.deviceList, false, false);
  231. }
  232. that.stopIntervalId1();
  233. } else {
  234. device.state = 'offline';
  235. }
  236. });
  237. } else {
  238. // wx.showModal({
  239. // title: '温馨提示',
  240. // content: '请检查手机蓝牙是否打开',
  241. // showCancel: false,
  242. // success: function (res) { }
  243. // });
  244. }
  245. that.data._willConnectBle = null;
  246. },
  247. // 回调
  248. mqttCallback(type, option) {
  249. // console.log("gadsfadsfadsfa==999===" + type + "====" + JSON.stringify(option));
  250. var that = this;
  251. let payloads = null;
  252. try {
  253. if (option && option.payload) {
  254. payloads = JSON.parse(option.payload);
  255. };
  256. switch (type) {
  257. ///先走订阅
  258. case "connect":
  259. constant.lexinSubscribe.subscribeAllDevice();
  260. break;
  261. ///再走有在线
  262. case "message_onoffline":
  263. constant.lexinConnect.searchOnlineDevice(payloads, function (list) {
  264. that.updateDeviceList(list, false, false);
  265. let hasOnline = that.data.deviceList.find(item => {
  266. return item.state === 'online';
  267. });
  268. if (hasOnline) {
  269. console.log("停止蓝牙监听1:",)
  270. that.stopIntervalId1()
  271. } else {
  272. console.log("开启蓝牙监听1:",)
  273. that.startBleTimer(true, false);
  274. }
  275. }, function () {
  276. var indexPage = that.data.indexPage;
  277. that.setData({
  278. actionIndex: null,
  279. deviceListSelect: null,
  280. indexPage: indexPage == 1 ? 0 : indexPage,
  281. });
  282. }, function () {
  283. var indexPage = that.data.indexPage;
  284. that.setData({
  285. actionIndex: null,
  286. deviceListSelect: null,
  287. indexPage: indexPage == 1 ? 0 : indexPage,
  288. });
  289. });
  290. break;
  291. case "message":
  292. if (payloads) {
  293. var payloadType = payloads.type;
  294. var other = payloads.other;
  295. ///获取频道位置
  296. if (payloadType === "get_position" && other) {
  297. constant.lexinMessage.getPosition(other, function (actionIndex) {
  298. that.setData({
  299. actionIndex: actionIndex,
  300. });
  301. });
  302. }
  303. // 获取歌曲信息
  304. else if (payloadType === "get_dev_info" && other) {
  305. that.setDeviceInfor(other);
  306. constant.lexinMessage.getDeviceInfo(payloads);
  307. }
  308. ///获取电量
  309. else if (payloadType === "battery" && other) {
  310. if (other.battery) {
  311. var battery = constant.lexinMessage.getBattery(other.battery);
  312. that.setData({
  313. battery: battery,
  314. });
  315. }
  316. }
  317. ///获取播放状态
  318. else if (payloadType === "play" || payloadType === "play_state") {
  319. constant.lexinMessage.getPlay();
  320. }
  321. }
  322. break;
  323. default:
  324. }
  325. } catch (e) { }
  326. },
  327. ///去连接设备数据
  328. actionDevice(device) {
  329. var that = this;
  330. // 取消订阅
  331. var deviceMacId = that.data.deviceMacId;
  332. if (!constant.strings.isEmpty(deviceMacId)) {
  333. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  334. constant.app.unsubscribe(pubResponse);
  335. };
  336. that.subscribeCurrDevice(device);
  337. },
  338. ///数据处理
  339. subscribeCurrDevice(device) {
  340. var that = this;
  341. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  342. if (!isOK) {
  343. constant.app.connect();
  344. setTimeout(() => {
  345. that.subscribeCurrDevice(device);
  346. }, 500);
  347. return;
  348. };
  349. var deviceId = device.deviceId;
  350. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  351. that.setData({
  352. deviceMacId: deviceMacId,
  353. });
  354. /// 订阅设备
  355. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  356. constant.app.subscribe(pubResponse);
  357. /// 获取设备信息
  358. const obj = {
  359. DstDeviceName: deviceMacId
  360. };
  361. constant.app.PubMsg({
  362. type: "get_dev_info",
  363. ...obj
  364. });
  365. },
  366. ///选择对应的频道
  367. onTapChannelIndex(e) {
  368. var that = this;
  369. var index = e.currentTarget.dataset.index;
  370. constant.lexinJump.toChannelIndex(index, function (i) {
  371. that.setData({
  372. actionIndex: i,
  373. });
  374. });
  375. },
  376. ///去频道详情
  377. onTapToChannel() {
  378. var that = this;
  379. var actionIndex = that.data.actionIndex;
  380. if (actionIndex == null) {
  381. return;
  382. }
  383. var channelData = that.getChannelData();
  384. if (channelData.length > actionIndex) {
  385. constant.store.setStore("channelDeta", channelData[actionIndex]);
  386. constant.routeUtil.jump(constant.routePath.channelDetailIndex);
  387. }
  388. },
  389. ///选择对应的频道
  390. getchannelData(clientType) {
  391. var that = this;
  392. constant.apiUtil.listByDevice({
  393. clientType: clientType,
  394. unShowLoad: true,
  395. }).then((res) => {
  396. constant.store.setStore("channelData", res);
  397. that.setData({
  398. channelData: res,
  399. });
  400. // 接收设备当前播放状态
  401. var deviceList = that.getDeviceList();
  402. var deviceListSelect = that.getDeviceListSelect();
  403. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  404. return;
  405. };
  406. var deviceId = deviceList[deviceListSelect].deviceId;
  407. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  408. const obj = {
  409. DstDeviceName: deviceMacId
  410. }
  411. constant.app.PubMsg({
  412. type: "get_position",
  413. ...obj
  414. });
  415. })
  416. },
  417. onTapIndex(e) {
  418. var that = this;
  419. var index = e.currentTarget.dataset.index;
  420. var indexPage = that.data.indexPage;
  421. if (indexPage != index) {
  422. that.setData({
  423. indexPage: index,
  424. });
  425. }
  426. },
  427. onTapLogin() {
  428. var that = this;
  429. ///退出登录
  430. var isLogin = that.getIsLogin();
  431. if (isLogin) {
  432. that.logOut();
  433. return;
  434. }
  435. constant.routeUtil.jump(constant.routePath.loginIndex);
  436. },
  437. ///退出登录
  438. logOut() {
  439. var that = this;
  440. wx.removeStorageSync('userInfo');
  441. wx.removeStorageSync('token');
  442. var nickName = "未登录";
  443. var greeting = that.getGreetBuNickName(nickName);
  444. that.setData({
  445. isLogin: false,
  446. greeting: greeting,
  447. nickName: nickName,
  448. userPic: "./../../img/head_pic.png",
  449. });
  450. var device = that.getCurrentDevice();
  451. if (device == null) {
  452. return;
  453. }
  454. ///有设备在线被选中,则让它不被选择
  455. if (device.connectType == 3) {
  456. var deviceMacId = that.data.deviceMacId;
  457. if (!constant.strings.isEmpty(deviceMacId)) {
  458. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  459. constant.app.unsubscribe(pubResponse);
  460. var indexPage = that.data.indexPage;
  461. that.setData({
  462. deviceMacId: null,
  463. actionIndex: null,
  464. deviceListSelect: null,
  465. indexPage: indexPage == 1 ? 0 : indexPage,
  466. });
  467. };
  468. } else {
  469. // console.log("退出登录,断开连接", deviceList);
  470. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  471. // var indexPage = that.data.indexPage;
  472. // that.setData({
  473. // deviceMacId: null,
  474. // actionIndex: null,
  475. // deviceListSelect: null,
  476. // indexPage: indexPage == 1 ? 0 : indexPage,
  477. // });
  478. }
  479. },
  480. ///点击item
  481. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  482. async onTapItem(e) {
  483. var that = this;
  484. var isLogin = that.getIsLogin();
  485. if (!isLogin) {
  486. constant.routeUtil.jump(constant.routePath.loginIndex);
  487. return;
  488. }
  489. var index = e.currentTarget.dataset.index;
  490. var device = e.currentTarget.dataset.item;
  491. var connectType = device.connectType;
  492. var deviceListSelect = that.getDeviceListSelect();
  493. // wifi只支持在线点击
  494. if (connectType == 3 && device.state === "online") {
  495. if (index === deviceListSelect) {
  496. that.goWake();
  497. } else {
  498. // 现断线第一个
  499. // that.disconnectFirst()
  500. that.addWifiDevice(device);
  501. }
  502. return;
  503. }
  504. ///去蓝牙连接处理
  505. // {"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 妙播收音机"}
  506. var deviceList = that.getDeviceList();
  507. let curDevice = deviceList[index]
  508. if (index === deviceListSelect && device.state === "online") {
  509. var param = "?param=" + JSON.stringify(curDevice);
  510. constant.routeUtil.jumpParam(constant.routePath.deviceDetailIndex, param);
  511. } else if (device.state === "offline") {
  512. var deviceListSelect = that.getDeviceListSelect();
  513. if (deviceListSelect != null) {
  514. if (deviceList.length > deviceListSelect) {
  515. var currentDevice = deviceList[deviceListSelect];
  516. console.log("断开连接0", currentDevice)
  517. // 现断线第一个
  518. // that.disconnectFirst()
  519. }
  520. that.setData({
  521. deviceListSelect: null,
  522. });
  523. }
  524. console.log("断开连接2", device)
  525. device.name = device.devName;
  526. that.data._clickOtherDevice = true;
  527. BtHelper.getInstance().connect(device, function (isConnected, d) {
  528. that.data._clickOtherDevice = false;
  529. if (isConnected) {
  530. device.state = "online";
  531. that.addBlueDevice(device);
  532. }
  533. });
  534. } else {
  535. var deviceList = that.getDeviceList();
  536. var deviceListSelect = that.getDeviceListSelect();
  537. if (deviceListSelect != null) {
  538. if (deviceList.length > deviceListSelect) {
  539. var currentDevice = deviceList[deviceListSelect];
  540. if (currentDevice.connectType === 1) {
  541. console.log("断开连接1", currentDevice)
  542. await BtHelper.getInstance().disconnect(currentDevice);
  543. }
  544. // await time_util.delayMills(500);
  545. }
  546. that.setData({
  547. deviceListSelect: null,
  548. });
  549. }
  550. that.data._clickOtherDevice = true;
  551. device.name = device.devName;
  552. BtHelper.getInstance().connect(device, function (isConnected, d) {
  553. that.data._clickOtherDevice = false;
  554. if (isConnected) {
  555. device.state = "online";
  556. that.addBlueDevice(device);
  557. }
  558. });
  559. }
  560. },
  561. disconnectFirst() {
  562. let that = this;
  563. let deviceList = that.getDeviceList()
  564. let first = deviceList[0] ?? {}
  565. if (first.state == "online") {
  566. if (first.connectType === 1) {
  567. BtHelper.getInstance().disconnect(first)
  568. first.state == "offline"
  569. } else {
  570. // 取消订阅
  571. // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
  572. //constant.app.unsubscribe(pubResponse);
  573. }
  574. }
  575. },
  576. // 去唤醒界面
  577. goWake() {
  578. var that = this;
  579. var deviceList = that.getDeviceList();
  580. var deviceListSelect = that.getDeviceListSelect();
  581. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  582. return
  583. };
  584. var device = deviceList[deviceListSelect];
  585. console.log("去唤醒界面", device.deviceId, device.ProdModel, device.dsn);
  586. var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel + "&dsn=" + device.dsn;
  587. constant.routeUtil.jumpParam(constant.routePath.deviceWakeIndex, param);
  588. },
  589. ///删除当前设备
  590. deleteDevice(e) {
  591. var that = this;
  592. var index = e.currentTarget.dataset.index;
  593. wx.showModal({
  594. title: '确定删除?',
  595. success: function (res) {
  596. if (res.confirm) {
  597. var deviceList = that.getDeviceList();
  598. if (deviceList[index].connectType == 3) {
  599. that.cancelWifi(index, false);
  600. } else {
  601. console.log("确定删除0", index, deviceList[index].deviceId)
  602. that.cancelBlue(index, false);
  603. }
  604. }
  605. }
  606. });
  607. },
  608. ///是否是同一个蓝牙
  609. isTheSameBlue(device) {
  610. var that = this;
  611. var deviceList = that.getDeviceList();
  612. var deviceListSelect = that.getDeviceListSelect();
  613. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  614. return false;
  615. };
  616. if (deviceList[deviceListSelect].connectType == 3) {
  617. return false;
  618. } else {
  619. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  620. return true;
  621. } else {
  622. return false;
  623. }
  624. }
  625. },
  626. ///断开当前的
  627. cancelCurrent() {
  628. var that = this;
  629. var deviceList = that.getDeviceList();
  630. var deviceListSelect = that.getDeviceListSelect();
  631. if (deviceList[deviceListSelect].connectType == 3) {
  632. that.cancelWifi(deviceListSelect, true);
  633. } else {
  634. that.cancelBlue(deviceListSelect, true);
  635. }
  636. },
  637. /// 断开蓝牙连接
  638. cancelBlue(index, onlyCancel) {
  639. var that = this;
  640. if (onlyCancel) {
  641. return;
  642. }
  643. var deviceList = that.getDeviceList();
  644. var deviceListSelect = that.getDeviceListSelect();
  645. ///是否删除的当前播放这个
  646. var isCurrentIndex = false;
  647. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  648. isCurrentIndex = (deviceListSelect == index);
  649. };
  650. let device = deviceList[index];
  651. ///删除当前设备
  652. var deviceId = device.deviceId;
  653. deviceList = deviceList.filter((item, i) => {
  654. return deviceId !== item.deviceId;
  655. });
  656. constant.store.setStore("deviceList", deviceList);
  657. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  658. var indexPage = that.data.indexPage;
  659. that.setData({
  660. deviceList: deviceList,
  661. indexPage: indexPage == 1 ? 0 : indexPage,
  662. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  663. });
  664. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  665. if (device.state === "online") {
  666. BtHelper.getInstance().disconnect({
  667. "deviceId": deviceId
  668. });
  669. }
  670. if (deviceList.length == 0) {
  671. that.stopIntervalId1()
  672. }
  673. },
  674. /// 断开连接wifi
  675. cancelWifi(index, onlyCancel) {
  676. var that = this;
  677. var deviceList = that.getDeviceList();
  678. if (index === null || deviceList.length <= index) {
  679. return;
  680. };
  681. var deviceId = deviceList[index].deviceId;
  682. // 取消订阅
  683. var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
  684. constant.app.unsubscribe(pubResponse);
  685. if (onlyCancel) {
  686. return;
  687. }
  688. ///是否删除的当前播放这个
  689. var isCurrentIndex = false;
  690. var deviceListSelect = that.getDeviceListSelect();
  691. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  692. isCurrentIndex = (deviceListSelect == index);
  693. };
  694. deviceList = deviceList.filter((item, i) => {
  695. return deviceId !== item.deviceId;
  696. });
  697. constant.store.setStore("deviceList", deviceList);
  698. var indexPage = that.data.indexPage;
  699. var deviceMacId = that.data.deviceMacId;
  700. var actionIndex = that.data.actionIndex;
  701. that.setData({
  702. deviceList: deviceList,
  703. deviceMacId: isCurrentIndex ? null : deviceMacId,
  704. actionIndex: isCurrentIndex ? null : actionIndex,
  705. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  706. indexPage: indexPage == 1 ? 0 : indexPage,
  707. });
  708. },
  709. /// 新添加蓝牙设备
  710. addBlueDevice(device) {
  711. var that = this;
  712. var addDeviceList = constant.lexinAdd.addBlueDevice(device);
  713. let hasOnline = 0
  714. addDeviceList.forEach(item => {
  715. // 蓝牙设备下线。wifi不动
  716. if (item.deviceId != device.deviceId && item.connectType === 1) {
  717. item.state = "offline"
  718. }
  719. })
  720. console.log("新添加蓝牙设备", addDeviceList)
  721. that.updateDeviceList(addDeviceList, false, false);
  722. that.setData({
  723. deviceListSelect: 0,
  724. autoConnected: true,
  725. });
  726. that.addNotification()
  727. },
  728. disconnectDev(event, device) {
  729. let _this = this;
  730. if (_this.data._clickOtherDevice) {
  731. _this.data._clickOtherDevice = false
  732. return;
  733. }
  734. var selectDev = _this.getCurrentDevice()
  735. console.log("断开连接--", event.commonValue, event.deviceId, selectDev);
  736. let selectDevId = "";
  737. if (selectDev) {
  738. selectDevId = selectDev.deviceId;
  739. }
  740. var isSelect = selectDev === null || selectDevId === event.deviceId
  741. if (isSelect && selectDevId === event.deviceId && event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  742. _this.data._hasShowModal = true
  743. wx.showModal({
  744. title: '提示',
  745. content: '设备断开连接,请重新连接设备',
  746. showCancel: false,
  747. success: function (res) {
  748. _this.data._hasShowModal = false;
  749. if (res.confirm) {
  750. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  751. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  752. }
  753. }
  754. }
  755. })
  756. }
  757. // 设备断开连接,请重新连接设备
  758. },
  759. addNotification() {
  760. let that = this;
  761. constant.eventBus.removeNotification(CmdEvent.eventName, that);
  762. constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
  763. let deviceList = that.getDeviceList()
  764. // console.log("收到监听:", JSON.stringify(event))
  765. if (event.cmdEvent == EnumCmdEvent.btMac) {
  766. let disDevice = deviceList.find(item => {
  767. return item.state == "online" && item.connectType === 1
  768. })
  769. if (disDevice) {
  770. disDevice.mac = event.btMac
  771. console.log("设备mac:", disDevice.mac)
  772. }
  773. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  774. let isFirst = false;
  775. // 有下线就开启监听扫描
  776. if (event.commonValue == "online") {
  777. console.log("有蓝牙设备上线")
  778. that.stopIntervalId1();
  779. } else {
  780. // 有wifi设备在线
  781. let wifiDevice = deviceList.find(item => {
  782. return item.state == "online" && item.connectType === 3
  783. })
  784. if (!wifiDevice) {
  785. that.startBleTimer(isFirst, true);
  786. }
  787. }
  788. console.log("首页ble通知:", event.deviceId, event.commonValue)
  789. let hasOnleBle = deviceList.find(item => {
  790. return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
  791. })
  792. let disDevice = deviceList.find(item => {
  793. return item.deviceId == event.deviceId && item.connectType == 1
  794. })
  795. // that.data.deviceList.forEach(element => {
  796. // if (item.deviceId == tmp.deviceId) {
  797. // }
  798. // });
  799. if (!disDevice) {
  800. // console.log("")
  801. console.log("没有找到在线的对应的蓝牙设备", deviceList.length)
  802. return;
  803. }
  804. disDevice.state = event.commonValue;
  805. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  806. if (event.commonValue == "online") {
  807. // BtHelper.getInstance().connect()
  808. console.log("首页连接上线", disDevice)
  809. } else {
  810. if (hasOnleBle) {
  811. that.disconnectDev(event, disDevice)
  812. }
  813. console.log("首页连接断开", disDevice)
  814. that.updateDeviceList(deviceList, false, false);
  815. }
  816. }
  817. }, that);
  818. },
  819. // 新添加Wifi设备
  820. addWifiDevice(device) {
  821. var that = this;
  822. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  823. if (!isOK) {
  824. constant.app.connect();
  825. setTimeout(() => {
  826. that.addWifiDevice(device);
  827. }, 500);
  828. return;
  829. };
  830. console.log("有新的wifi设备0", device)
  831. var deviceId = device.deviceId;
  832. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  833. console.log("有新的wifi设备", addDeviceList)
  834. that.updateDeviceList(addDeviceList, false, false);
  835. var indexPage = that.data.indexPage;
  836. that.setData({
  837. deviceListSelect: null,
  838. connectDeviceIding: deviceId,
  839. indexPage: indexPage == 1 ? 0 : indexPage,
  840. });
  841. constant.lexinSubscribe.subscribeSingleDevice(device);
  842. },
  843. refreshCurrentDevice(device) {
  844. var that = this;
  845. console.log("刷新列表0:", device)
  846. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  847. console.log("刷新列表1:", addDeviceList)
  848. that.updateDeviceList(addDeviceList, false, false);
  849. that.setData({
  850. autoConnected: true,
  851. deviceListSelect: 0,
  852. connectDeviceIding: "",
  853. });
  854. },
  855. /// 手机关闭蓝牙,所有蓝牙设备离线
  856. closeBlueResetOffline(isInit, closeAllBlue) {
  857. var that = this;
  858. var deviceList = that.getDeviceList();
  859. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  860. },
  861. /// 更新列表排序
  862. updateDeviceList(deviceList, isInit, closeAllBlue) {
  863. var that = this;
  864. var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
  865. constant.store.setStore("deviceList", finalList);
  866. that.setData({
  867. deviceList: finalList,
  868. });
  869. },
  870. ///点击banner事件
  871. onTapBanner(e) {
  872. // var url = "https://m.ximalaya.com/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-2AXD:AIrSMArTd031105fe3c0%26client_os_type%3D3%26state%3D1.0.0_47.107.237.154_c65af766a9744b6098c3aec86187023c_3_http://testweb.radio1964.com/pages/ximalaya/index_deviceid=MW-2AXD:AIrSMArTd031105fe3c0_null"
  873. var url = "http://testweb.radio1964.com/pages/ximalaya/index_deviceid=MW-2AXD:AIrSMArTd031105fe3c0_null"
  874. var path = "../pageB/authWeb/authWeb" + "?url=" + encodeURIComponent(url);
  875. console.log("跳转到小米", path)
  876. constant.routeUtil.jump(path);
  877. return;
  878. var item = e.currentTarget.dataset.item;
  879. try {
  880. if (constant.strings.isEmpty(item.forwardUrl)) {
  881. return;
  882. }
  883. // 内链
  884. if (item.forwardType == 0) {
  885. var param = "?param=" + item.forwardUrl;
  886. constant.app.webSkipUrl = item.forwardUrl;
  887. constant.routeUtil.jumpParam(constant.routePath.webviewIndex, 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.loginIndex, param);
  908. return;
  909. }
  910. constant.routeUtil.jump(constant.routePath.deviceListIndex);
  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. })