index.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. if (device.connectType === 3) {
  208. return;
  209. }
  210. BtHelper.getInstance().connect(device, function (isConnected, d) {
  211. console.log("自动连接成功", isConnected)
  212. if (isConnected) {
  213. let hasOtherOnline = false;
  214. that.data.deviceList.forEach(item => {
  215. if (item.state === 'online') {
  216. if (item.deviceId == device.deviceId) { } else {
  217. hasOtherOnline = true;
  218. return;
  219. }
  220. }
  221. })
  222. if (!hasOtherOnline) {
  223. // 自己已经在线,没有其他设备在线, 直接更新
  224. console.log("自己已经在线,没有其他设备在线, 直接更新");
  225. that.addBlueDevice(device);
  226. } else {
  227. let bleDev = that.data.deviceList.find(item => {
  228. return item.deviceId === device.deviceId;
  229. });
  230. if (bleDev) {
  231. bleDev.state = 'online';
  232. }
  233. that.updateDeviceList(that.data.deviceList, false, false);
  234. }
  235. that.stopIntervalId1();
  236. } else {
  237. device.state = 'offline';
  238. }
  239. });
  240. } else {
  241. // wx.showModal({
  242. // title: '温馨提示',
  243. // content: '请检查手机蓝牙是否打开',
  244. // showCancel: false,
  245. // success: function (res) { }
  246. // });
  247. }
  248. that.data._willConnectBle = null;
  249. },
  250. // 回调
  251. mqttCallback(type, option) {
  252. // console.log("gadsfadsfadsfa==999===" + type + "====" + JSON.stringify(option));
  253. var that = this;
  254. let payloads = null;
  255. try {
  256. if (option && option.payload) {
  257. payloads = JSON.parse(option.payload);
  258. };
  259. switch (type) {
  260. ///先走订阅
  261. case "connect":
  262. constant.lexinSubscribe.subscribeAllDevice();
  263. break;
  264. ///再走有在线
  265. case "message_onoffline":
  266. constant.lexinConnect.searchOnlineDevice(payloads, function (list) {
  267. that.updateDeviceList(list, false, false);
  268. let hasOnline = that.data.deviceList.find(item => {
  269. return item.state === 'online';
  270. });
  271. if (hasOnline) {
  272. console.log("停止蓝牙监听1:",)
  273. that.stopIntervalId1()
  274. } else {
  275. console.log("开启蓝牙监听1:",)
  276. that.startBleTimer(true, false);
  277. }
  278. }, function () {
  279. var indexPage = that.data.indexPage;
  280. that.setData({
  281. actionIndex: null,
  282. deviceListSelect: null,
  283. indexPage: indexPage == 1 ? 0 : indexPage,
  284. });
  285. }, function () {
  286. var indexPage = that.data.indexPage;
  287. that.setData({
  288. actionIndex: null,
  289. deviceListSelect: null,
  290. indexPage: indexPage == 1 ? 0 : indexPage,
  291. });
  292. });
  293. break;
  294. case "message":
  295. if (payloads) {
  296. var payloadType = payloads.type;
  297. var other = payloads.other;
  298. ///获取频道位置
  299. if (payloadType === "get_position" && other) {
  300. constant.lexinMessage.getPosition(other, function (actionIndex) {
  301. that.setData({
  302. actionIndex: actionIndex,
  303. });
  304. });
  305. }
  306. // 获取歌曲信息
  307. else if (payloadType === "get_dev_info" && other) {
  308. that.setDeviceInfor(other);
  309. constant.lexinMessage.getDeviceInfo(payloads);
  310. }
  311. ///获取电量
  312. else if (payloadType === "battery" && other) {
  313. if (other.battery) {
  314. var battery = constant.lexinMessage.getBattery(other.battery);
  315. that.setData({
  316. battery: battery,
  317. });
  318. }
  319. }
  320. ///获取播放状态
  321. else if (payloadType === "play" || payloadType === "play_state") {
  322. constant.lexinMessage.getPlay();
  323. }
  324. }
  325. break;
  326. default:
  327. }
  328. } catch (e) { }
  329. },
  330. ///去连接设备数据
  331. actionDevice(device) {
  332. var that = this;
  333. // 取消订阅
  334. var deviceMacId = that.data.deviceMacId;
  335. if (!constant.strings.isEmpty(deviceMacId)) {
  336. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  337. constant.app.unsubscribe(pubResponse);
  338. };
  339. that.subscribeCurrDevice(device);
  340. },
  341. ///数据处理
  342. subscribeCurrDevice(device) {
  343. var that = this;
  344. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  345. if (!isOK) {
  346. constant.app.connect();
  347. setTimeout(() => {
  348. that.subscribeCurrDevice(device);
  349. }, 500);
  350. return;
  351. };
  352. var deviceId = device.deviceId;
  353. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  354. that.setData({
  355. deviceMacId: deviceMacId,
  356. });
  357. /// 订阅设备
  358. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  359. constant.app.subscribe(pubResponse);
  360. /// 获取设备信息
  361. const obj = {
  362. DstDeviceName: deviceMacId
  363. };
  364. constant.app.PubMsg({
  365. type: "get_dev_info",
  366. ...obj
  367. });
  368. },
  369. ///选择对应的频道
  370. onTapChannelIndex(e) {
  371. var that = this;
  372. var index = e.currentTarget.dataset.index;
  373. constant.lexinJump.toChannelIndex(index, function (i) {
  374. that.setData({
  375. actionIndex: i,
  376. });
  377. });
  378. },
  379. ///去频道详情
  380. onTapToChannel() {
  381. var that = this;
  382. var actionIndex = that.data.actionIndex;
  383. if (actionIndex == null) {
  384. return;
  385. }
  386. var channelData = that.getChannelData();
  387. if (channelData.length > actionIndex) {
  388. constant.store.setStore("channelDeta", channelData[actionIndex]);
  389. constant.routeUtil.jump(constant.routePath.channelDetailIndex);
  390. }
  391. },
  392. ///选择对应的频道
  393. getchannelData(clientType) {
  394. var that = this;
  395. constant.apiUtil.listByDevice({
  396. clientType: clientType,
  397. unShowLoad: true,
  398. }).then((res) => {
  399. constant.store.setStore("channelData", res);
  400. that.setData({
  401. channelData: res,
  402. });
  403. // 接收设备当前播放状态
  404. var deviceList = that.getDeviceList();
  405. var deviceListSelect = that.getDeviceListSelect();
  406. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  407. return;
  408. };
  409. var deviceId = deviceList[deviceListSelect].deviceId;
  410. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  411. const obj = {
  412. DstDeviceName: deviceMacId
  413. }
  414. constant.app.PubMsg({
  415. type: "get_position",
  416. ...obj
  417. });
  418. })
  419. },
  420. onTapIndex(e) {
  421. var that = this;
  422. var index = e.currentTarget.dataset.index;
  423. var indexPage = that.data.indexPage;
  424. if (indexPage != index) {
  425. that.setData({
  426. indexPage: index,
  427. });
  428. }
  429. },
  430. onTapLogin() {
  431. var that = this;
  432. ///退出登录
  433. var isLogin = that.getIsLogin();
  434. if (isLogin) {
  435. that.logOut();
  436. return;
  437. }
  438. constant.routeUtil.jump(constant.routePath.loginIndex);
  439. },
  440. ///退出登录
  441. logOut() {
  442. var that = this;
  443. wx.removeStorageSync('userInfo');
  444. wx.removeStorageSync('token');
  445. var nickName = "未登录";
  446. var greeting = that.getGreetBuNickName(nickName);
  447. that.setData({
  448. isLogin: false,
  449. greeting: greeting,
  450. nickName: nickName,
  451. userPic: "./../../img/head_pic.png",
  452. });
  453. var device = that.getCurrentDevice();
  454. if (device == null) {
  455. return;
  456. }
  457. ///有设备在线被选中,则让它不被选择
  458. if (device.connectType == 3) {
  459. var deviceMacId = that.data.deviceMacId;
  460. if (!constant.strings.isEmpty(deviceMacId)) {
  461. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  462. constant.app.unsubscribe(pubResponse);
  463. var indexPage = that.data.indexPage;
  464. that.setData({
  465. deviceMacId: null,
  466. actionIndex: null,
  467. deviceListSelect: null,
  468. indexPage: indexPage == 1 ? 0 : indexPage,
  469. });
  470. };
  471. } else {
  472. // console.log("退出登录,断开连接", deviceList);
  473. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  474. // var indexPage = that.data.indexPage;
  475. // that.setData({
  476. // deviceMacId: null,
  477. // actionIndex: null,
  478. // deviceListSelect: null,
  479. // indexPage: indexPage == 1 ? 0 : indexPage,
  480. // });
  481. }
  482. },
  483. ///点击item
  484. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  485. async onTapItem(e) {
  486. var that = this;
  487. var isLogin = that.getIsLogin();
  488. if (!isLogin) {
  489. constant.routeUtil.jump(constant.routePath.loginIndex);
  490. return;
  491. }
  492. var index = e.currentTarget.dataset.index;
  493. var device = e.currentTarget.dataset.item;
  494. var connectType = device.connectType;
  495. var deviceListSelect = that.getDeviceListSelect();
  496. // wifi只支持在线点击
  497. if (connectType == 3 && device.state === "online") {
  498. if (index === deviceListSelect) {
  499. that.goWake();
  500. } else {
  501. // 现断线第一个
  502. // that.disconnectFirst()
  503. that.addWifiDevice(device);
  504. }
  505. return;
  506. }
  507. ///去蓝牙连接处理
  508. // {"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 妙播收音机"}
  509. var deviceList = that.getDeviceList();
  510. let curDevice = deviceList[index]
  511. if (index === deviceListSelect && device.state === "online") {
  512. var param = "?param=" + JSON.stringify(curDevice);
  513. constant.routeUtil.jumpParam(constant.routePath.deviceDetailIndex, param);
  514. } else if (device.state === "offline") {
  515. var deviceListSelect = that.getDeviceListSelect();
  516. if (deviceListSelect != null) {
  517. if (deviceList.length > deviceListSelect) {
  518. var currentDevice = deviceList[deviceListSelect];
  519. console.log("断开连接0", currentDevice)
  520. // 现断线第一个
  521. // that.disconnectFirst()
  522. }
  523. that.setData({
  524. deviceListSelect: null,
  525. });
  526. }
  527. console.log("断开连接2", device)
  528. device.name = device.devName;
  529. that.data._clickOtherDevice = true;
  530. if (device.connectType === 3) {
  531. return;
  532. }
  533. BtHelper.getInstance().connect(device, function (isConnected, d) {
  534. that.data._clickOtherDevice = false;
  535. if (isConnected) {
  536. device.state = "online";
  537. that.addBlueDevice(device);
  538. }
  539. });
  540. } else {
  541. var deviceList = that.getDeviceList();
  542. var deviceListSelect = that.getDeviceListSelect();
  543. if (deviceListSelect != null) {
  544. if (deviceList.length > deviceListSelect) {
  545. var currentDevice = deviceList[deviceListSelect];
  546. if (currentDevice.connectType === 1) {
  547. console.log("断开连接1", currentDevice)
  548. await BtHelper.getInstance().disconnect(currentDevice);
  549. }
  550. // await time_util.delayMills(500);
  551. }
  552. that.setData({
  553. deviceListSelect: null,
  554. });
  555. }
  556. that.data._clickOtherDevice = true;
  557. device.name = device.devName;
  558. if (device.connectType === 3) {
  559. return;
  560. }
  561. BtHelper.getInstance().connect(device, function (isConnected, d) {
  562. that.data._clickOtherDevice = false;
  563. if (isConnected) {
  564. device.state = "online";
  565. that.addBlueDevice(device);
  566. }
  567. });
  568. }
  569. },
  570. disconnectFirst() {
  571. let that = this;
  572. let deviceList = that.getDeviceList()
  573. let first = deviceList[0] ?? {}
  574. if (first.state == "online") {
  575. if (first.connectType === 1) {
  576. BtHelper.getInstance().disconnect(first)
  577. first.state == "offline"
  578. } else {
  579. // 取消订阅
  580. // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
  581. //constant.app.unsubscribe(pubResponse);
  582. }
  583. }
  584. },
  585. // 去唤醒界面
  586. goWake() {
  587. var that = this;
  588. var deviceList = that.getDeviceList();
  589. var deviceListSelect = that.getDeviceListSelect();
  590. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  591. return
  592. };
  593. var device = deviceList[deviceListSelect];
  594. console.log("去唤醒界面", device.deviceId, device.ProdModel, device.dsn);
  595. var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel + "&dsn=" + device.dsn;
  596. constant.routeUtil.jumpParam(constant.routePath.deviceWakeIndex, param);
  597. },
  598. ///删除当前设备
  599. deleteDevice(e) {
  600. var that = this;
  601. var index = e.currentTarget.dataset.index;
  602. wx.showModal({
  603. title: '确定删除?',
  604. success: function (res) {
  605. if (res.confirm) {
  606. var deviceList = that.getDeviceList();
  607. if (deviceList[index].connectType == 3) {
  608. that.cancelWifi(index, false);
  609. } else {
  610. console.log("确定删除0", index, deviceList[index].deviceId)
  611. that.cancelBlue(index, false);
  612. }
  613. }
  614. }
  615. });
  616. },
  617. ///是否是同一个蓝牙
  618. isTheSameBlue(device) {
  619. var that = this;
  620. var deviceList = that.getDeviceList();
  621. var deviceListSelect = that.getDeviceListSelect();
  622. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  623. return false;
  624. };
  625. if (deviceList[deviceListSelect].connectType == 3) {
  626. return false;
  627. } else {
  628. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  629. return true;
  630. } else {
  631. return false;
  632. }
  633. }
  634. },
  635. ///断开当前的
  636. cancelCurrent() {
  637. var that = this;
  638. var deviceList = that.getDeviceList();
  639. var deviceListSelect = that.getDeviceListSelect();
  640. if (deviceList[deviceListSelect].connectType == 3) {
  641. that.cancelWifi(deviceListSelect, true);
  642. } else {
  643. that.cancelBlue(deviceListSelect, true);
  644. }
  645. },
  646. /// 断开蓝牙连接
  647. cancelBlue(index, onlyCancel) {
  648. var that = this;
  649. if (onlyCancel) {
  650. return;
  651. }
  652. var deviceList = that.getDeviceList();
  653. var deviceListSelect = that.getDeviceListSelect();
  654. ///是否删除的当前播放这个
  655. var isCurrentIndex = false;
  656. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  657. isCurrentIndex = (deviceListSelect == index);
  658. };
  659. let device = deviceList[index];
  660. ///删除当前设备
  661. var deviceId = device.deviceId;
  662. deviceList = deviceList.filter((item, i) => {
  663. return deviceId !== item.deviceId;
  664. });
  665. constant.store.setStore("deviceList", deviceList);
  666. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  667. var indexPage = that.data.indexPage;
  668. that.setData({
  669. deviceList: deviceList,
  670. indexPage: indexPage == 1 ? 0 : indexPage,
  671. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  672. });
  673. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  674. if (device.state === "online") {
  675. BtHelper.getInstance().disconnect({
  676. "deviceId": deviceId
  677. });
  678. }
  679. if (deviceList.length == 0) {
  680. that.stopIntervalId1()
  681. }
  682. },
  683. /// 断开连接wifi
  684. cancelWifi(index, onlyCancel) {
  685. var that = this;
  686. var deviceList = that.getDeviceList();
  687. if (index === null || deviceList.length <= index) {
  688. return;
  689. };
  690. var deviceId = deviceList[index].deviceId;
  691. // 取消订阅
  692. var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
  693. constant.app.unsubscribe(pubResponse);
  694. if (onlyCancel) {
  695. return;
  696. }
  697. ///是否删除的当前播放这个
  698. var isCurrentIndex = false;
  699. var deviceListSelect = that.getDeviceListSelect();
  700. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  701. isCurrentIndex = (deviceListSelect == index);
  702. };
  703. deviceList = deviceList.filter((item, i) => {
  704. return deviceId !== item.deviceId;
  705. });
  706. constant.store.setStore("deviceList", deviceList);
  707. var indexPage = that.data.indexPage;
  708. var deviceMacId = that.data.deviceMacId;
  709. var actionIndex = that.data.actionIndex;
  710. that.setData({
  711. deviceList: deviceList,
  712. deviceMacId: isCurrentIndex ? null : deviceMacId,
  713. actionIndex: isCurrentIndex ? null : actionIndex,
  714. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  715. indexPage: indexPage == 1 ? 0 : indexPage,
  716. });
  717. },
  718. /// 新添加蓝牙设备
  719. addBlueDevice(device) {
  720. var that = this;
  721. var addDeviceList = constant.lexinAdd.addBlueDevice(device);
  722. let hasOnline = 0
  723. addDeviceList.forEach(item => {
  724. // 蓝牙设备下线。wifi不动
  725. if (item.deviceId != device.deviceId && item.connectType === 1) {
  726. item.state = "offline"
  727. }
  728. })
  729. console.log("新添加蓝牙设备", addDeviceList)
  730. that.updateDeviceList(addDeviceList, false, false);
  731. that.setData({
  732. deviceListSelect: 0,
  733. autoConnected: true,
  734. });
  735. that.addNotification()
  736. },
  737. disconnectDev(event, device) {
  738. let _this = this;
  739. if (_this.data._clickOtherDevice) {
  740. _this.data._clickOtherDevice = false
  741. return;
  742. }
  743. var selectDev = _this.getCurrentDevice()
  744. console.log("断开连接--", event.commonValue, event.deviceId, selectDev);
  745. let selectDevId = "";
  746. if (selectDev) {
  747. selectDevId = selectDev.deviceId;
  748. }
  749. var isSelect = selectDev === null || selectDevId === event.deviceId
  750. if (isSelect && selectDevId === event.deviceId && event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  751. _this.data._hasShowModal = true
  752. wx.showModal({
  753. title: '提示',
  754. content: '设备断开连接,请重新连接设备',
  755. showCancel: false,
  756. success: function (res) {
  757. _this.data._hasShowModal = false;
  758. if (res.confirm) {
  759. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  760. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  761. }
  762. }
  763. }
  764. })
  765. }
  766. // 设备断开连接,请重新连接设备
  767. },
  768. addNotification() {
  769. let that = this;
  770. constant.eventBus.removeNotification(CmdEvent.eventName, that);
  771. constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
  772. let deviceList = that.getDeviceList()
  773. // console.log("收到监听:", JSON.stringify(event))
  774. if (event.cmdEvent == EnumCmdEvent.btMac) {
  775. let disDevice = deviceList.find(item => {
  776. return item.state == "online" && item.connectType === 1
  777. })
  778. if (disDevice) {
  779. disDevice.mac = event.btMac
  780. console.log("设备mac:", disDevice.mac)
  781. }
  782. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  783. let isFirst = false;
  784. // 有下线就开启监听扫描
  785. if (event.commonValue == "online") {
  786. console.log("有蓝牙设备上线")
  787. that.stopIntervalId1();
  788. } else {
  789. // 有wifi设备在线
  790. let wifiDevice = deviceList.find(item => {
  791. return item.state == "online" && item.connectType === 3
  792. })
  793. if (!wifiDevice) {
  794. that.startBleTimer(isFirst, true);
  795. }
  796. }
  797. console.log("首页ble通知:", event.deviceId, event.commonValue)
  798. let hasOnleBle = deviceList.find(item => {
  799. return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
  800. })
  801. let disDevice = deviceList.find(item => {
  802. return item.deviceId == event.deviceId && item.connectType == 1
  803. })
  804. // that.data.deviceList.forEach(element => {
  805. // if (item.deviceId == tmp.deviceId) {
  806. // }
  807. // });
  808. if (!disDevice) {
  809. // console.log("")
  810. console.log("没有找到在线的对应的蓝牙设备", deviceList.length)
  811. return;
  812. }
  813. disDevice.state = event.commonValue;
  814. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  815. if (event.commonValue == "online") {
  816. // BtHelper.getInstance().connect()
  817. console.log("首页连接上线", disDevice)
  818. } else {
  819. if (hasOnleBle) {
  820. that.disconnectDev(event, disDevice)
  821. }
  822. console.log("首页连接断开", disDevice)
  823. that.updateDeviceList(deviceList, false, false);
  824. }
  825. }
  826. }, that);
  827. },
  828. // 新添加Wifi设备
  829. addWifiDevice(device) {
  830. var that = this;
  831. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  832. if (!isOK) {
  833. constant.app.connect();
  834. setTimeout(() => {
  835. that.addWifiDevice(device);
  836. }, 500);
  837. return;
  838. };
  839. console.log("有新的wifi设备0", device)
  840. var deviceId = device.deviceId;
  841. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  842. console.log("有新的wifi设备", addDeviceList)
  843. that.updateDeviceList(addDeviceList, false, false);
  844. var indexPage = that.data.indexPage;
  845. that.setData({
  846. deviceListSelect: null,
  847. connectDeviceIding: deviceId,
  848. indexPage: indexPage == 1 ? 0 : indexPage,
  849. });
  850. constant.lexinSubscribe.subscribeSingleDevice(device);
  851. },
  852. refreshCurrentDevice(device) {
  853. var that = this;
  854. console.log("刷新列表0:", device)
  855. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  856. console.log("刷新列表1:", addDeviceList)
  857. that.updateDeviceList(addDeviceList, false, false);
  858. that.setData({
  859. autoConnected: true,
  860. deviceListSelect: 0,
  861. connectDeviceIding: "",
  862. });
  863. },
  864. /// 手机关闭蓝牙,所有蓝牙设备离线
  865. closeBlueResetOffline(isInit, closeAllBlue) {
  866. var that = this;
  867. var deviceList = that.getDeviceList();
  868. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  869. },
  870. /// 更新列表排序
  871. updateDeviceList(deviceList, isInit, closeAllBlue) {
  872. var that = this;
  873. var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
  874. constant.store.setStore("deviceList", finalList);
  875. that.setData({
  876. deviceList: finalList,
  877. });
  878. },
  879. ///点击banner事件
  880. onTapBanner(e) {
  881. // // 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"
  882. // var url = "http://testweb.radio1964.com/pages/ximalaya/index?deviceid=MW-2AXD:AIrSMArTd031105fe3c0_null"
  883. // var path = "../pageB/authWeb/authWeb" + "?url=" + encodeURIComponent(url);
  884. // // http://192.168.198.116:1024/pages/ximalaya/index?deviceid=MW-2AXD:AIrSMArTd031105fe3c0_null
  885. // console.log("跳转到小米", path)
  886. // constant.routeUtil.jump(path);
  887. // return;
  888. var item = e.currentTarget.dataset.item;
  889. try {
  890. if (constant.strings.isEmpty(item.forwardUrl)) {
  891. return;
  892. }
  893. // 内链
  894. if (item.forwardType == 0) {
  895. var param = "?param=" + item.forwardUrl;
  896. constant.app.webSkipUrl = item.forwardUrl;
  897. constant.routeUtil.jumpParam(constant.routePath.webviewIndex, param);
  898. }
  899. // 外链
  900. else if (item.forwardType == 1) {
  901. } else {
  902. }
  903. } catch (err) {
  904. console.log("跳转失败", err)
  905. }
  906. },
  907. ///去充值内容会员
  908. toRecharge(e) {
  909. var device = e.currentTarget.dataset.item;
  910. },
  911. ///添加设备
  912. jumpToAddDevice() {
  913. var that = this;
  914. var isLogin = that.getIsLogin();
  915. if (!isLogin) {
  916. var param = "?toDeviceList=" + true;
  917. constant.routeUtil.jumpParam(constant.routePath.loginIndex, param);
  918. return;
  919. }
  920. constant.routeUtil.jump(constant.routePath.deviceListIndex);
  921. },
  922. // 关于我们
  923. jumpToAboutUs() {
  924. constant.lexinJump.toAboutUs();
  925. },
  926. /// 是否已登录
  927. getIsLogin() {
  928. var that = this;
  929. var isLogin = that.data.isLogin;
  930. return isLogin;
  931. },
  932. /// 获取列表数据
  933. getDeviceList() {
  934. var that = this;
  935. var deviceList = that.data.deviceList;
  936. return deviceList;
  937. },
  938. ///获取当前选中的
  939. getDeviceListSelect() {
  940. var that = this;
  941. var deviceListSelect = that.data.deviceListSelect;
  942. return deviceListSelect;
  943. },
  944. ///获取是否已经第一次连接过
  945. getAutoConnected() {
  946. var that = this;
  947. var autoConnected = that.data.autoConnected;
  948. return autoConnected;
  949. },
  950. ///当前正在连接的设备id
  951. getConnectDeviceIding() {
  952. var that = this;
  953. var connectDeviceIding = that.data.connectDeviceIding
  954. return connectDeviceIding;
  955. },
  956. ///获取频道数据
  957. getChannelData() {
  958. var that = this;
  959. var channelData = that.data.channelData;
  960. return channelData;
  961. },
  962. /// 当前播放频道
  963. getActionIndex() {
  964. var that = this;
  965. var actionIndex = that.data.actionIndex;
  966. return actionIndex;
  967. },
  968. /// 获取当前的device
  969. getCurrentDevice() {
  970. var that = this;
  971. var deviceList = that.getDeviceList();
  972. var deviceListSelect = that.getDeviceListSelect();
  973. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  974. return null;
  975. };
  976. var device = deviceList[deviceListSelect];
  977. return device;
  978. },
  979. /// 获取当前的deviceId
  980. getCurrentDeviceId() {
  981. var that = this;
  982. var deviceList = that.getDeviceList();
  983. var deviceListSelect = that.getDeviceListSelect();
  984. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  985. return "";
  986. };
  987. var deviceId = deviceList[deviceListSelect].deviceId;
  988. return deviceId;
  989. },
  990. ///设置设备相关信息
  991. setDeviceInfor(other) {
  992. var that = this;
  993. // 接收设备当前信息
  994. that.getchannelData(other.ProdModel);
  995. // 电量
  996. if (other.Power) {
  997. var battery = constant.lexinMessage.getBattery(other.Power);
  998. that.setData({
  999. battery: battery,
  1000. });
  1001. }
  1002. // 当前设备木有设置定时
  1003. that.setData({
  1004. isSetWake: false,
  1005. });
  1006. other.alarm.map((v) => {
  1007. if (v.enable === "1") {
  1008. that.setData({
  1009. isSetWake: true
  1010. });
  1011. }
  1012. });
  1013. },
  1014. ///******************************* 可折叠 ********************************************///
  1015. onUserInfoLoad() {
  1016. var that = this;
  1017. var userInfo = wx.getStorageSync("userInfo") || "";
  1018. if (!constant.strings.isEmpty(userInfo)) {
  1019. var phone = userInfo.phone;
  1020. if (!phone) {
  1021. return;
  1022. };
  1023. var nickName = userInfo.nickname || "";
  1024. var greeting = that.getGreetBuNickName(nickName);
  1025. // 获取缓存的频道数据
  1026. var channelData = wx.getStorageSync("channelData") || "";
  1027. if (!constant.strings.isEmpty(channelData)) {
  1028. that.setData({
  1029. channelData: channelData,
  1030. greeting: greeting,
  1031. nickName: nickName,
  1032. userPic: userInfo.headUrl || "",
  1033. isLogin: true,
  1034. })
  1035. } else {
  1036. that.setData({
  1037. greeting: greeting,
  1038. nickName: nickName,
  1039. userPic: userInfo.headUrl || "",
  1040. isLogin: true,
  1041. });
  1042. }
  1043. } else {
  1044. var greeting = that.getGreetBuNickName("");
  1045. that.setData({
  1046. greeting: greeting,
  1047. })
  1048. }
  1049. },
  1050. getGreetBuNickName(nickName) {
  1051. var greeting = constant.timeUtil.getGreet();
  1052. if (nickName != "" && nickName != "未登录") {
  1053. greeting = greeting + ',' + nickName;;
  1054. }
  1055. return greeting;
  1056. },
  1057. onBannerLoad() {
  1058. var that = this;
  1059. constant.apiUtil.getBanner({}).then((res) => {
  1060. that.setData({
  1061. bannerList: res,
  1062. });
  1063. constant.store.setStore("homeBanner", res);
  1064. });
  1065. },
  1066. stopIntervalId1: function () {
  1067. var that = this;
  1068. if (!constant.strings.isEmpty(that.data.intervalId1)) {
  1069. clearInterval(that.data.intervalId1);
  1070. that.data.intervalId1 = null;
  1071. }
  1072. },
  1073. stopIntervalId2: function () {
  1074. var that = this;
  1075. if (!constant.strings.isEmpty(that.data.intervalId2)) {
  1076. clearInterval(that.data.intervalId2);
  1077. that.data.intervalId2 = null;
  1078. }
  1079. },
  1080. ///销毁蓝牙
  1081. onUnload() {
  1082. var that = this;
  1083. constant.eventBus.removeNotification(CmdEvent.eventName, that)
  1084. console.log("界面onUnload")
  1085. that.stopIntervalId1();
  1086. that.stopIntervalId2();
  1087. if (!constant.strings.isEmpty(that.data.intervalId)) {
  1088. clearInterval(that.data.intervalId);
  1089. that.data.intervalId = null;
  1090. }
  1091. BtHelper.getInstance().disconnect();
  1092. if (constant.app.globalData.client === null) {
  1093. return;
  1094. };
  1095. constant.app.globalData.client.end(true);
  1096. constant.app.globalData.client = null;
  1097. },
  1098. })