index.js 36 KB

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