index.js 31 KB

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