index.js 36 KB

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