index.js 36 KB

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