index.js 34 KB

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