index.js 35 KB

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