index.js 36 KB

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