index.js 35 KB

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