index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. const app = getApp();
  2. const {
  3. login,
  4. listByDevice
  5. } = require('../../utils/api.js');
  6. import time_util from '../../utils/time_util.js';
  7. import strings from '../../utils/strings.js';
  8. import store from '../../utils/store.js';
  9. import route_constant from '../../utils/route_constant.js';
  10. import route_util from '../../utils/route_util.js';
  11. /// 乐鑫相关
  12. import lexin_util from '../../utils/lexin/util.js';
  13. import lexin_jump from '../../utils/lexin/jump.js';
  14. import lexin_add from '../../utils/lexin/add.js';
  15. import lexin_scan from '../../utils/lexin/scan.js';
  16. import lexin_subscribe from '../../utils/lexin/subscribe.js';
  17. import lexin_connect from '../../utils/lexin/connect.js';
  18. import lexin_message from '../../utils/lexin/message.js';
  19. import {
  20. BtHelper
  21. } from '../../devices/bt_helper.js';
  22. import event_bus from '../../utils/event_bus.js';
  23. import {
  24. EnumCmdEvent,
  25. CmdEvent
  26. } from '../../devices/cmd_key_event';
  27. Page({
  28. data: {
  29. navBarHeight: app.globalData.navBarHeight,
  30. safeBottomHeight: app.globalData.safeBottomHeight,
  31. nvabarData: {
  32. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  33. title: 'OhPlay', //导航栏 中间的标题
  34. },
  35. ///下午好
  36. isLogin: false,
  37. greeting: "",
  38. bannerList: [{
  39. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  40. },
  41. {
  42. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  43. }
  44. ],
  45. autoplay: true,
  46. interval: 3000, // 切换时间间隔
  47. duration: 500, // 滑动动画时长
  48. circular: true, // 衔接滑动
  49. indexPage: 0,
  50. ///是否展示频道
  51. showChannel: false,
  52. ////首页
  53. navBarHeight: app.globalData.navBarHeight,
  54. MenuButtonheight: app.globalData.MenuButtonheight,
  55. MenuButtonTop: app.globalData.MenuButtonTop,
  56. actionIndex: null,
  57. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  58. channelData: [],
  59. isOneLoading: true,
  60. uid: null,
  61. isSetWake: false,
  62. battery: 4, // 0≤电量<20,0格
  63. ////我的界面
  64. loginStatus: true,
  65. nickName: "未登录",
  66. userPic: './../../img/head_pic.png',
  67. intervalId: null,
  68. /// 是否第一次自动连接过
  69. autoConnected: false,
  70. intervalId1: null,
  71. intervalId2: null,
  72. deviceMacId: null,
  73. deviceListSelect: null,
  74. deviceList: [],
  75. _willConnectBle: null,
  76. _hasShowModal: 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. disconnectDev(event, device) {
  770. let _this = this;
  771. console.log("断开连接--", event.commonValue, event.deviceId);
  772. if (event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  773. _this.data._hasShowModal = true
  774. wx.showModal({
  775. title: '提示',
  776. content: '设备断开连接,请重新连接设备',
  777. showCancel: false,
  778. success: function (res) {
  779. _this.data._hasShowModal = false;
  780. if (res.confirm) {
  781. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  782. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  783. }
  784. }
  785. }
  786. })
  787. }
  788. // 设备断开连接,请重新连接设备
  789. },
  790. addNotification() {
  791. let that = this;
  792. event_bus.removeNotification(CmdEvent.eventName, that);
  793. event_bus.addNotification(CmdEvent.eventName, function (event) {
  794. let deviceList = that.getDeviceList()
  795. if (event.cmdEvent == EnumCmdEvent.btMac) {
  796. let disDevice = deviceList.find(item => {
  797. return item.state == "online" && item.connectType === 1
  798. })
  799. if (disDevice) {
  800. disDevice.mac = event.btMac
  801. console.log("设备mac:", disDevice.mac)
  802. }
  803. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  804. let isFirst = false;
  805. // 有下线就开启监听扫描
  806. if (event.commonValue == "online") {
  807. console.log("有蓝牙设备上线")
  808. that.stopIntervalId1();
  809. } else {
  810. let onLineBle = deviceList.find(item => {
  811. return item.state == "online" && item.connectType === 1
  812. })
  813. if (!onLineBle) {
  814. // 没有在线的蓝牙
  815. that.startBleTimer(isFirst);
  816. }
  817. }
  818. console.log("首页ble通知:", event.deviceId, event.commonValue)
  819. let disDevice = deviceList.find(item => {
  820. return item.deviceId == event.deviceId && item.connectType == 1
  821. })
  822. // that.data.deviceList.forEach(element => {
  823. // if (item.deviceId == tmp.deviceId) {
  824. // }
  825. // });
  826. if (!disDevice) {
  827. console.log("没有找到在线的对应的蓝牙设备")
  828. return;
  829. }
  830. that.disconnectDev(event, disDevice)
  831. disDevice.state = event.commonValue;
  832. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  833. if (event.commonValue == "online") {
  834. // BtHelper.getInstance().connect()
  835. console.log("首页上线连接", disDevice)
  836. } else {
  837. console.log("首页断开连接", disDevice)
  838. that.updateDeviceList(deviceList, false, false);
  839. }
  840. }
  841. }, that);
  842. },
  843. // 新添加Wifi设备
  844. addWifiDevice(device) {
  845. var that = this;
  846. var isOK = app.globalData.client && app.globalData.client.connected;
  847. if (!isOK) {
  848. app.connect();
  849. setTimeout(() => {
  850. that.addWifiDevice(device);
  851. }, 500);
  852. return;
  853. };
  854. console.log("有新的wifi设备0", device)
  855. var deviceId = device.deviceId;
  856. var addDeviceList = lexin_add.addWifiDevice(device);
  857. console.log("有新的wifi设备", addDeviceList)
  858. that.updateDeviceList(addDeviceList, false, false);
  859. var indexPage = that.data.indexPage;
  860. that.setData({
  861. deviceListSelect: null,
  862. connectDeviceIding: deviceId,
  863. indexPage: indexPage == 1 ? 0 : indexPage,
  864. });
  865. lexin_subscribe.subscribeSingleDevice(device);
  866. },
  867. refreshCurrentDevice(device) {
  868. var that = this;
  869. console.log("刷新列表0:", device)
  870. var addDeviceList = lexin_add.addWifiDevice(device);
  871. console.log("刷新列表1:", addDeviceList)
  872. that.updateDeviceList(addDeviceList, false, false);
  873. that.setData({
  874. autoConnected: true,
  875. deviceListSelect: 0,
  876. connectDeviceIding: "",
  877. });
  878. },
  879. /// 手机关闭蓝牙,所有蓝牙设备离线
  880. closeBlueResetOffline(isInit, closeAllBlue) {
  881. var that = this;
  882. var deviceList = that.getDeviceList();
  883. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  884. },
  885. /// 更新列表排序
  886. updateDeviceList(deviceList, isInit, closeAllBlue) {
  887. var that = this;
  888. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  889. store.setStore("deviceList", finalList);
  890. that.setData({
  891. deviceList: finalList,
  892. });
  893. },
  894. ///点击banner事件
  895. onTapBanner() {
  896. var that = this;
  897. var item = e.currentTarget.dataset.item;
  898. },
  899. ///添加设备
  900. jumpToAddDevice() {
  901. var that = this;
  902. // route_util.jump("../deviceConnect4/deviceConnect4");
  903. // return;
  904. var isLogin = that.getIsLogin();
  905. if (!isLogin) {
  906. route_util.jump(route_constant.login);
  907. return;
  908. }
  909. route_util.jump(route_constant.deviceList);
  910. },
  911. // 关于我们
  912. jumpToAboutUs() {
  913. lexin_jump.toAboutUs();
  914. },
  915. /// 是否已登录
  916. getIsLogin() {
  917. var that = this;
  918. var isLogin = that.data.isLogin;
  919. return isLogin;
  920. },
  921. /// 获取列表数据
  922. getDeviceList() {
  923. var that = this;
  924. var deviceList = that.data.deviceList;
  925. return deviceList;
  926. },
  927. ///获取当前选中的
  928. getDeviceListSelect() {
  929. var that = this;
  930. var deviceListSelect = that.data.deviceListSelect;
  931. return deviceListSelect;
  932. },
  933. ///获取是否已经第一次连接过
  934. getAutoConnected() {
  935. var that = this;
  936. var autoConnected = that.data.autoConnected;
  937. return autoConnected;
  938. },
  939. ///当前正在连接的设备id
  940. getConnectDeviceIding() {
  941. var that = this;
  942. var connectDeviceIding = that.data.connectDeviceIding
  943. return connectDeviceIding;
  944. },
  945. ///获取频道数据
  946. getChannelData() {
  947. var that = this;
  948. var channelData = that.data.channelData;
  949. return channelData;
  950. },
  951. /// 当前播放频道
  952. getActionIndex() {
  953. var that = this;
  954. var actionIndex = that.data.actionIndex;
  955. return actionIndex;
  956. },
  957. /// 获取当前的deviceId
  958. getCurrentDeviceId() {
  959. var that = this;
  960. var deviceList = that.getDeviceList();
  961. var deviceListSelect = that.getDeviceListSelect();
  962. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  963. return "";
  964. };
  965. var deviceId = deviceList[deviceListSelect].deviceId;
  966. return deviceId;
  967. },
  968. ///设置设备相关信息
  969. setDeviceInfor(other) {
  970. var that = this;
  971. // 接收设备当前信息
  972. that.getchannelData(other.ProdModel);
  973. // 电量
  974. if (other.Power) {
  975. var battery = lexin_message.getBattery(other.Power);
  976. that.setData({
  977. battery: battery,
  978. });
  979. }
  980. // 当前设备木有设置定时
  981. that.setData({
  982. isSetWake: false,
  983. });
  984. other.alarm.map((v) => {
  985. if (v.enable === "1") {
  986. that.setData({
  987. isSetWake: true
  988. });
  989. }
  990. });
  991. },
  992. ///******************************* 可折叠 ********************************************///
  993. onUserInfoLoad() {
  994. var that = this;
  995. var userInfo = wx.getStorageSync("userInfo") || "";
  996. if (!strings.isEmpty(userInfo)) {
  997. var phone = userInfo.phone;
  998. if (!phone) {
  999. return;
  1000. };
  1001. var nickName = userInfo.nickname || "";
  1002. var greeting = that.getGreetBuNickName(nickName);
  1003. // 获取缓存的频道数据
  1004. var channelData = wx.getStorageSync("channelData") || "";
  1005. if (!strings.isEmpty(channelData)) {
  1006. that.setData({
  1007. channelData: channelData,
  1008. greeting: greeting,
  1009. nickName: nickName,
  1010. userPic: userInfo.headUrl || "",
  1011. isLogin: true,
  1012. })
  1013. } else {
  1014. that.setData({
  1015. greeting: greeting,
  1016. nickName: nickName,
  1017. userPic: userInfo.headUrl || "",
  1018. isLogin: true,
  1019. });
  1020. }
  1021. } else {
  1022. var greeting = that.getGreetBuNickName("");
  1023. that.setData({
  1024. greeting: greeting,
  1025. })
  1026. }
  1027. },
  1028. getGreetBuNickName(nickName) {
  1029. var greeting = time_util.getGreet();
  1030. if (nickName != "" && nickName != "未登录") {
  1031. greeting = greeting + ',' + nickName;;
  1032. }
  1033. return greeting;
  1034. },
  1035. onBannerLoad() {
  1036. var that = this;
  1037. // var bannerList = wx.getStorageSync("homeBanner") || [];
  1038. // if (that.data.bannerList.length > 0) {
  1039. // that.setData({
  1040. // bannerList: bannerList
  1041. // });
  1042. // }
  1043. },
  1044. onBannerShow() {
  1045. var that = this;
  1046. // getBanner({}).then((res) => {
  1047. // that.setData({
  1048. // bannerList: res
  1049. // });
  1050. // store.setStore("homeBanner", res);
  1051. // })
  1052. },
  1053. stopIntervalId1: function () {
  1054. var that = this;
  1055. if (!strings.isEmpty(that.data.intervalId1)) {
  1056. clearInterval(that.data.intervalId1);
  1057. that.data.intervalId1 = null;
  1058. }
  1059. },
  1060. stopIntervalId2: function () {
  1061. var that = this;
  1062. if (!strings.isEmpty(that.data.intervalId2)) {
  1063. clearInterval(that.data.intervalId2);
  1064. that.data.intervalId2 = null;
  1065. }
  1066. },
  1067. ///销毁蓝牙
  1068. onUnload() {
  1069. var that = this;
  1070. event_bus.removeNotification(CmdEvent.eventName, that)
  1071. console.log("界面onUnload")
  1072. that.stopIntervalId1();
  1073. that.stopIntervalId2();
  1074. if (!strings.isEmpty(that.data.intervalId)) {
  1075. clearInterval(that.data.intervalId);
  1076. that.data.intervalId = null;
  1077. }
  1078. BtHelper.getInstance().disconnect();
  1079. if (app.globalData.client === null) {
  1080. return;
  1081. };
  1082. app.globalData.client.end(true);
  1083. app.globalData.client.end(true);
  1084. app.globalData.client = null;
  1085. },
  1086. })