index.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. "name": "MW-X4(BZK)",
  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==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. route_util.jump(route_constant.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 deviceList = that.getDeviceList();
  516. var deviceListSelect = that.getDeviceListSelect();
  517. if (deviceList.length > 1 && (deviceListSelect === null || deviceList.length <= deviceListSelect)) {
  518. // 2个设备才判断,1个直接删除
  519. return;
  520. };
  521. deviceListSelect = deviceListSelect ?? 0
  522. ///有设备在线被选中,则让它不被选择
  523. if (deviceList[deviceListSelect].connectType == 3) {
  524. var deviceMacId = that.data.deviceMacId;
  525. if (!strings.isEmpty(deviceMacId)) {
  526. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  527. app.unsubscribe(pubResponse);
  528. var indexPage = that.data.indexPage;
  529. that.setData({
  530. deviceMacId: null,
  531. actionIndex: null,
  532. deviceListSelect: null,
  533. indexPage: indexPage == 1 ? 0 : indexPage,
  534. });
  535. };
  536. } else {
  537. // console.log("退出登录,断开连接", deviceList);
  538. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  539. // var indexPage = that.data.indexPage;
  540. // that.setData({
  541. // deviceMacId: null,
  542. // actionIndex: null,
  543. // deviceListSelect: null,
  544. // indexPage: indexPage == 1 ? 0 : indexPage,
  545. // });
  546. }
  547. },
  548. ///点击item
  549. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  550. async onTapItem(e) {
  551. var that = this;
  552. var isLogin = that.getIsLogin();
  553. if (!isLogin) {
  554. route_util.jump(route_constant.login);
  555. return;
  556. }
  557. var index = e.currentTarget.dataset.index;
  558. var device = e.currentTarget.dataset.item;
  559. var connectType = device.connectType;
  560. var deviceListSelect = that.getDeviceListSelect();
  561. // wifi只支持在线点击
  562. if (connectType == 3 && device.state === "online") {
  563. if (index === deviceListSelect) {
  564. that.goWake();
  565. } else {
  566. // 现断线第一个
  567. // that.disconnectFirst()
  568. that.addWifiDevice(device);
  569. }
  570. return;
  571. }
  572. ///去蓝牙连接处理
  573. // {"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 妙播收音机"}
  574. var deviceList = that.getDeviceList();
  575. let curDevice = deviceList[index]
  576. if (index === deviceListSelect && device.state === "online") {
  577. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(curDevice))
  578. } else if (device.state === "offline") {
  579. var deviceListSelect = that.getDeviceListSelect();
  580. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  581. that.setData({
  582. deviceListSelect: null,
  583. });
  584. var currentDevice = deviceList[deviceListSelect];
  585. console.log("断开连接0", currentDevice)
  586. // 现断线第一个
  587. // that.disconnectFirst()
  588. }
  589. console.log("断开连接2", device)
  590. device.name = device.devName;
  591. BtHelper.getInstance().connect(device, function (isConnected, d) {
  592. if (isConnected) {
  593. device.state = "online";
  594. that.addBlueDevice(device);
  595. }
  596. });
  597. } else {
  598. var deviceList = that.getDeviceList();
  599. var deviceListSelect = that.getDeviceListSelect();
  600. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  601. that.setData({
  602. deviceListSelect: null,
  603. });
  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. device.name = device.devName;
  612. BtHelper.getInstance().connect(device, function (isConnected, d) {
  613. if (isConnected) {
  614. device.state = "online";
  615. that.addBlueDevice(device);
  616. }
  617. });
  618. }
  619. },
  620. disconnectFirst() {
  621. let that = this;
  622. let deviceList = that.getDeviceList()
  623. let first = deviceList[0] ?? {}
  624. if (first.state == "online") {
  625. if (first.connectType === 1) {
  626. BtHelper.getInstance().disconnect(first)
  627. first.state == "offline"
  628. } else {
  629. // 取消订阅
  630. // var pubResponse = lexin_util.getResponseByDeviceId(first.deviceId);
  631. // app.unsubscribe(pubResponse);
  632. }
  633. }
  634. },
  635. // 去唤醒界面
  636. goWake() {
  637. var that = this;
  638. var deviceList = that.getDeviceList();
  639. var deviceListSelect = that.getDeviceListSelect();
  640. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  641. return
  642. };
  643. var device = deviceList[deviceListSelect];
  644. wx.navigateTo({
  645. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  646. });
  647. },
  648. ///删除当前设备
  649. deleteDevice(e) {
  650. var that = this;
  651. var index = e.currentTarget.dataset.index;
  652. wx.showModal({
  653. title: '确定删除?',
  654. success: function (res) {
  655. if (res.confirm) {
  656. var deviceList = that.getDeviceList();
  657. if (deviceList[index].connectType == 3) {
  658. that.cancelWifi(index, false);
  659. } else {
  660. console.log("确定删除0", index, deviceList[index].deviceId)
  661. that.cancelBlue(index, false);
  662. }
  663. }
  664. }
  665. });
  666. },
  667. ///是否是同一个蓝牙
  668. isTheSameBlue(device) {
  669. var that = this;
  670. var deviceList = that.getDeviceList();
  671. var deviceListSelect = that.getDeviceListSelect();
  672. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  673. return false;
  674. };
  675. if (deviceList[deviceListSelect].connectType == 3) {
  676. return false;
  677. } else {
  678. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  679. return true;
  680. } else {
  681. return false;
  682. }
  683. }
  684. },
  685. ///断开当前的
  686. cancelCurrent() {
  687. var that = this;
  688. var deviceList = that.getDeviceList();
  689. var deviceListSelect = that.getDeviceListSelect();
  690. if (deviceList[deviceListSelect].connectType == 3) {
  691. that.cancelWifi(deviceListSelect, true);
  692. } else {
  693. that.cancelBlue(deviceListSelect, true);
  694. }
  695. },
  696. /// 断开蓝牙连接
  697. cancelBlue(index, onlyCancel) {
  698. var that = this;
  699. if (onlyCancel) {
  700. return;
  701. }
  702. var deviceList = that.getDeviceList();
  703. var deviceListSelect = that.getDeviceListSelect();
  704. ///是否删除的当前播放这个
  705. var isCurrentIndex = false;
  706. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  707. isCurrentIndex = (deviceListSelect == index);
  708. };
  709. let device = deviceList[index];
  710. ///删除当前设备
  711. var deviceId = device.deviceId;
  712. deviceList = deviceList.filter((item, i) => {
  713. return deviceId !== item.deviceId;
  714. });
  715. store.setStore("deviceList", deviceList);
  716. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  717. var indexPage = that.data.indexPage;
  718. that.setData({
  719. deviceList: deviceList,
  720. indexPage: indexPage == 1 ? 0 : indexPage,
  721. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  722. });
  723. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  724. if (device.state === "online") {
  725. BtHelper.getInstance().disconnect({
  726. "deviceId": deviceId
  727. });
  728. }
  729. },
  730. /// 断开连接wifi
  731. cancelWifi(index, onlyCancel) {
  732. var that = this;
  733. var deviceList = that.getDeviceList();
  734. if (index === null || deviceList.length <= index) {
  735. return;
  736. };
  737. var deviceId = deviceList[index].deviceId;
  738. // 取消订阅
  739. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  740. app.unsubscribe(pubResponse);
  741. if (onlyCancel) {
  742. return;
  743. }
  744. ///是否删除的当前播放这个
  745. var isCurrentIndex = false;
  746. var deviceListSelect = that.getDeviceListSelect();
  747. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  748. isCurrentIndex = (deviceListSelect == index);
  749. };
  750. deviceList = deviceList.filter((item, i) => {
  751. return deviceId !== item.deviceId;
  752. });
  753. store.setStore("deviceList", deviceList);
  754. var indexPage = that.data.indexPage;
  755. var deviceMacId = that.data.deviceMacId;
  756. var actionIndex = that.data.actionIndex;
  757. that.setData({
  758. deviceList: deviceList,
  759. deviceMacId: isCurrentIndex ? null : deviceMacId,
  760. actionIndex: isCurrentIndex ? null : actionIndex,
  761. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  762. indexPage: indexPage == 1 ? 0 : indexPage,
  763. });
  764. },
  765. /// 新添加蓝牙设备
  766. addBlueDevice(device) {
  767. var that = this;
  768. var addDeviceList = lexin_add.addBlueDevice(device);
  769. let hasOnline = 0
  770. addDeviceList.forEach(item => {
  771. // 蓝牙设备下线。wifi不动
  772. if (item.deviceId != device.deviceId && item.connectType === 1) {
  773. item.state = "offline"
  774. }
  775. })
  776. console.log("新添加蓝牙设备", addDeviceList)
  777. that.updateDeviceList(addDeviceList, false, false);
  778. that.setData({
  779. deviceListSelect: 0,
  780. autoConnected: true,
  781. });
  782. that.addNotification()
  783. },
  784. disconnectDev(event, device) {
  785. let _this = this;
  786. console.log("断开连接--", event.commonValue, event.deviceId);
  787. if (event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  788. _this.data._hasShowModal = true
  789. wx.showModal({
  790. title: '提示',
  791. content: '设备断开连接,请重新连接设备',
  792. showCancel: false,
  793. success: function (res) {
  794. _this.data._hasShowModal = false;
  795. if (res.confirm) {
  796. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  797. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  798. }
  799. }
  800. }
  801. })
  802. }
  803. // 设备断开连接,请重新连接设备
  804. },
  805. addNotification() {
  806. let that = this;
  807. event_bus.removeNotification(CmdEvent.eventName, that);
  808. event_bus.addNotification(CmdEvent.eventName, function (event) {
  809. let deviceList = that.getDeviceList()
  810. if (event.cmdEvent == EnumCmdEvent.btMac) {
  811. let disDevice = deviceList.find(item => {
  812. return item.state == "online" && item.connectType === 1
  813. })
  814. if (disDevice) {
  815. disDevice.mac = event.btMac
  816. console.log("设备mac:", disDevice.mac)
  817. }
  818. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  819. let isFirst = false;
  820. // 有下线就开启监听扫描
  821. if (event.commonValue == "online") {
  822. console.log("有蓝牙设备上线")
  823. that.stopIntervalId1();
  824. } else {
  825. let onLineBle = deviceList.find(item => {
  826. return item.state == "online" && item.connectType === 1
  827. })
  828. if (!onLineBle) {
  829. // 没有在线的蓝牙
  830. that.startBleTimer(isFirst);
  831. }
  832. }
  833. console.log("首页ble通知:", event.deviceId, event.commonValue)
  834. let disDevice = deviceList.find(item => {
  835. return item.deviceId == event.deviceId && item.connectType == 1
  836. })
  837. // that.data.deviceList.forEach(element => {
  838. // if (item.deviceId == tmp.deviceId) {
  839. // }
  840. // });
  841. if (!disDevice) {
  842. console.log("没有找到在线的对应的蓝牙设备")
  843. return;
  844. }
  845. that.disconnectDev(event, disDevice)
  846. disDevice.state = event.commonValue;
  847. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  848. if (event.commonValue == "online") {
  849. // BtHelper.getInstance().connect()
  850. console.log("首页上线连接", disDevice)
  851. } else {
  852. console.log("首页断开连接", disDevice)
  853. that.updateDeviceList(deviceList, false, false);
  854. }
  855. }
  856. }, that);
  857. },
  858. // 新添加Wifi设备
  859. addWifiDevice(device) {
  860. var that = this;
  861. var isOK = app.globalData.client && app.globalData.client.connected;
  862. if (!isOK) {
  863. app.connect();
  864. setTimeout(() => {
  865. that.addWifiDevice(device);
  866. }, 500);
  867. return;
  868. };
  869. console.log("有新的wifi设备0", device)
  870. var deviceId = device.deviceId;
  871. var addDeviceList = lexin_add.addWifiDevice(device);
  872. console.log("有新的wifi设备", addDeviceList)
  873. that.updateDeviceList(addDeviceList, false, false);
  874. var indexPage = that.data.indexPage;
  875. that.setData({
  876. deviceListSelect: null,
  877. connectDeviceIding: deviceId,
  878. indexPage: indexPage == 1 ? 0 : indexPage,
  879. });
  880. lexin_subscribe.subscribeSingleDevice(device);
  881. },
  882. refreshCurrentDevice(device) {
  883. var that = this;
  884. console.log("刷新列表0:", device)
  885. var addDeviceList = lexin_add.addWifiDevice(device);
  886. console.log("刷新列表1:", addDeviceList)
  887. that.updateDeviceList(addDeviceList, false, false);
  888. that.setData({
  889. autoConnected: true,
  890. deviceListSelect: 0,
  891. connectDeviceIding: "",
  892. });
  893. },
  894. /// 手机关闭蓝牙,所有蓝牙设备离线
  895. closeBlueResetOffline(isInit, closeAllBlue) {
  896. var that = this;
  897. var deviceList = that.getDeviceList();
  898. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  899. },
  900. /// 更新列表排序
  901. updateDeviceList(deviceList, isInit, closeAllBlue) {
  902. var that = this;
  903. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  904. store.setStore("deviceList", finalList);
  905. that.setData({
  906. deviceList: finalList,
  907. });
  908. },
  909. ///点击banner事件
  910. onTapBanner(e) {
  911. var that = this;
  912. var item = e.currentTarget.dataset.item;
  913. },
  914. ///添加设备
  915. jumpToAddDevice() {
  916. var that = this;
  917. // route_util.jump("../deviceConnect4/deviceConnect4");
  918. // return;
  919. var isLogin = that.getIsLogin();
  920. if (!isLogin) {
  921. route_util.jump(route_constant.login);
  922. return;
  923. }
  924. route_util.jump(route_constant.deviceList);
  925. },
  926. // 关于我们
  927. jumpToAboutUs() {
  928. lexin_jump.toAboutUs();
  929. },
  930. /// 是否已登录
  931. getIsLogin() {
  932. var that = this;
  933. var isLogin = that.data.isLogin;
  934. return isLogin;
  935. },
  936. /// 获取列表数据
  937. getDeviceList() {
  938. var that = this;
  939. var deviceList = that.data.deviceList;
  940. return deviceList;
  941. },
  942. ///获取当前选中的
  943. getDeviceListSelect() {
  944. var that = this;
  945. var deviceListSelect = that.data.deviceListSelect;
  946. return deviceListSelect;
  947. },
  948. ///获取是否已经第一次连接过
  949. getAutoConnected() {
  950. var that = this;
  951. var autoConnected = that.data.autoConnected;
  952. return autoConnected;
  953. },
  954. ///当前正在连接的设备id
  955. getConnectDeviceIding() {
  956. var that = this;
  957. var connectDeviceIding = that.data.connectDeviceIding
  958. return connectDeviceIding;
  959. },
  960. ///获取频道数据
  961. getChannelData() {
  962. var that = this;
  963. var channelData = that.data.channelData;
  964. return channelData;
  965. },
  966. /// 当前播放频道
  967. getActionIndex() {
  968. var that = this;
  969. var actionIndex = that.data.actionIndex;
  970. return actionIndex;
  971. },
  972. /// 获取当前的deviceId
  973. getCurrentDeviceId() {
  974. var that = this;
  975. var deviceList = that.getDeviceList();
  976. var deviceListSelect = that.getDeviceListSelect();
  977. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  978. return "";
  979. };
  980. var deviceId = deviceList[deviceListSelect].deviceId;
  981. return deviceId;
  982. },
  983. ///设置设备相关信息
  984. setDeviceInfor(other) {
  985. var that = this;
  986. // 接收设备当前信息
  987. that.getchannelData(other.ProdModel);
  988. // 电量
  989. if (other.Power) {
  990. var battery = lexin_message.getBattery(other.Power);
  991. that.setData({
  992. battery: battery,
  993. });
  994. }
  995. // 当前设备木有设置定时
  996. that.setData({
  997. isSetWake: false,
  998. });
  999. other.alarm.map((v) => {
  1000. if (v.enable === "1") {
  1001. that.setData({
  1002. isSetWake: true
  1003. });
  1004. }
  1005. });
  1006. },
  1007. ///******************************* 可折叠 ********************************************///
  1008. onUserInfoLoad() {
  1009. var that = this;
  1010. var userInfo = wx.getStorageSync("userInfo") || "";
  1011. if (!strings.isEmpty(userInfo)) {
  1012. var phone = userInfo.phone;
  1013. if (!phone) {
  1014. return;
  1015. };
  1016. var nickName = userInfo.nickname || "";
  1017. var greeting = that.getGreetBuNickName(nickName);
  1018. // 获取缓存的频道数据
  1019. var channelData = wx.getStorageSync("channelData") || "";
  1020. if (!strings.isEmpty(channelData)) {
  1021. that.setData({
  1022. channelData: channelData,
  1023. greeting: greeting,
  1024. nickName: nickName,
  1025. userPic: userInfo.headUrl || "",
  1026. isLogin: true,
  1027. })
  1028. } else {
  1029. that.setData({
  1030. greeting: greeting,
  1031. nickName: nickName,
  1032. userPic: userInfo.headUrl || "",
  1033. isLogin: true,
  1034. });
  1035. }
  1036. } else {
  1037. var greeting = that.getGreetBuNickName("");
  1038. that.setData({
  1039. greeting: greeting,
  1040. })
  1041. }
  1042. },
  1043. getGreetBuNickName(nickName) {
  1044. var greeting = time_util.getGreet();
  1045. if (nickName != "" && nickName != "未登录") {
  1046. greeting = greeting + ',' + nickName;;
  1047. }
  1048. return greeting;
  1049. },
  1050. onBannerLoad() {
  1051. var that = this;
  1052. // var bannerList = wx.getStorageSync("homeBanner") || [];
  1053. // if (that.data.bannerList.length > 0) {
  1054. // that.setData({
  1055. // bannerList: bannerList
  1056. // });
  1057. // }
  1058. },
  1059. onBannerShow() {
  1060. var that = this;
  1061. // getBanner({}).then((res) => {
  1062. // that.setData({
  1063. // bannerList: res
  1064. // });
  1065. // store.setStore("homeBanner", res);
  1066. // })
  1067. },
  1068. stopIntervalId1: function () {
  1069. var that = this;
  1070. if (!strings.isEmpty(that.data.intervalId1)) {
  1071. clearInterval(that.data.intervalId1);
  1072. that.data.intervalId1 = null;
  1073. }
  1074. },
  1075. stopIntervalId2: function () {
  1076. var that = this;
  1077. if (!strings.isEmpty(that.data.intervalId2)) {
  1078. clearInterval(that.data.intervalId2);
  1079. that.data.intervalId2 = null;
  1080. }
  1081. },
  1082. ///销毁蓝牙
  1083. onUnload() {
  1084. var that = this;
  1085. event_bus.removeNotification(CmdEvent.eventName, that)
  1086. console.log("界面onUnload")
  1087. that.stopIntervalId1();
  1088. that.stopIntervalId2();
  1089. if (!strings.isEmpty(that.data.intervalId)) {
  1090. clearInterval(that.data.intervalId);
  1091. that.data.intervalId = null;
  1092. }
  1093. BtHelper.getInstance().disconnect();
  1094. if (app.globalData.client === null) {
  1095. return;
  1096. };
  1097. app.globalData.client.end(true);
  1098. app.globalData.client.end(true);
  1099. app.globalData.client = null;
  1100. },
  1101. })