index.js 34 KB

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