index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. that.stopIntervalId1()
  274. }, function () {
  275. var indexPage = that.data.indexPage;
  276. that.setData({
  277. actionIndex: null,
  278. deviceListSelect: null,
  279. indexPage: indexPage == 1 ? 0 : indexPage,
  280. });
  281. console.log("开启蓝牙监听1:",)
  282. that.startBleTimer(true, false);
  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. console.log("开启蓝牙监听2:",)
  291. that.startBleTimer(true, false);
  292. });
  293. break;
  294. case "message":
  295. if (payloads) {
  296. var payloadType = payloads.type;
  297. var other = payloads.other;
  298. ///获取频道位置
  299. if (payloadType === "get_position" && other) {
  300. constant.lexinMessage.getPosition(other, function (actionIndex) {
  301. that.setData({
  302. actionIndex: actionIndex,
  303. });
  304. });
  305. }
  306. // 获取歌曲信息
  307. else if (payloadType === "get_dev_info" && other) {
  308. that.setDeviceInfor(other);
  309. constant.lexinMessage.getDeviceInfo(payloads);
  310. }
  311. ///获取电量
  312. else if (payloadType === "battery" && other) {
  313. if (other.battery) {
  314. var battery = constant.lexinMessage.getBattery(other.battery);
  315. that.setData({
  316. battery: battery,
  317. });
  318. }
  319. }
  320. ///获取播放状态
  321. else if (payloadType === "play" || payloadType === "play_state") {
  322. constant.lexinMessage.getPlay();
  323. }
  324. }
  325. break;
  326. default:
  327. }
  328. } catch (e) { }
  329. },
  330. ///去连接设备数据
  331. actionDevice(device) {
  332. var that = this;
  333. // 取消订阅
  334. var deviceMacId = that.data.deviceMacId;
  335. if (!constant.strings.isEmpty(deviceMacId)) {
  336. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  337. constant.app.unsubscribe(pubResponse);
  338. };
  339. that.subscribeCurrDevice(device);
  340. },
  341. ///数据处理
  342. subscribeCurrDevice(device) {
  343. var that = this;
  344. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  345. if (!isOK) {
  346. constant.app.connect();
  347. setTimeout(() => {
  348. that.subscribeCurrDevice(device);
  349. }, 500);
  350. return;
  351. };
  352. var deviceId = device.deviceId;
  353. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  354. that.setData({
  355. deviceMacId: deviceMacId,
  356. });
  357. /// 订阅设备
  358. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  359. constant.app.subscribe(pubResponse);
  360. /// 获取设备信息
  361. const obj = {
  362. DstDeviceName: deviceMacId
  363. };
  364. constant.app.PubMsg({
  365. type: "get_dev_info",
  366. ...obj
  367. });
  368. },
  369. ///选择对应的频道
  370. onTapChannelIndex(e) {
  371. var that = this;
  372. var index = e.currentTarget.dataset.index;
  373. constant.lexinJump.toChannelIndex(index, function (i) {
  374. that.setData({
  375. actionIndex: i,
  376. });
  377. });
  378. },
  379. ///去频道详情
  380. onTapToChannel() {
  381. var that = this;
  382. var actionIndex = that.data.actionIndex;
  383. if (actionIndex == null) {
  384. return;
  385. }
  386. var channelData = that.getChannelData();
  387. if (channelData.length > actionIndex) {
  388. constant.store.setStore("channelDeta", channelData[actionIndex]);
  389. constant.routeUtil.jump(constant.routePath.channelDetail);
  390. }
  391. },
  392. ///选择对应的频道
  393. getchannelData(clientType) {
  394. var that = this;
  395. constant.apiUtil.listByDevice({
  396. clientType: clientType,
  397. unShowLoad: true,
  398. }).then((res) => {
  399. constant.store.setStore("channelData", res);
  400. that.setData({
  401. channelData: res,
  402. });
  403. // 接收设备当前播放状态
  404. var deviceList = that.getDeviceList();
  405. var deviceListSelect = that.getDeviceListSelect();
  406. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  407. return;
  408. };
  409. var deviceId = deviceList[deviceListSelect].deviceId;
  410. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  411. const obj = {
  412. DstDeviceName: deviceMacId
  413. }
  414. constant.app.PubMsg({
  415. type: "get_position",
  416. ...obj
  417. });
  418. })
  419. },
  420. onTapIndex(e) {
  421. var that = this;
  422. var index = e.currentTarget.dataset.index;
  423. var indexPage = that.data.indexPage;
  424. if (indexPage != index) {
  425. that.setData({
  426. indexPage: index,
  427. });
  428. }
  429. },
  430. onTapLogin() {
  431. var that = this;
  432. ///退出登录
  433. var isLogin = that.getIsLogin();
  434. if (isLogin) {
  435. that.logOut();
  436. return;
  437. }
  438. constant.routeUtil.jump(constant.routePath.login);
  439. },
  440. ///退出登录
  441. logOut() {
  442. var that = this;
  443. wx.removeStorageSync('userInfo');
  444. wx.removeStorageSync('token');
  445. var nickName = "未登录";
  446. var greeting = that.getGreetBuNickName(nickName);
  447. that.setData({
  448. isLogin: false,
  449. greeting: greeting,
  450. nickName: nickName,
  451. userPic: "./../../../img/head_pic.png",
  452. });
  453. var device = that.getCurrentDevice();
  454. if (device == null) {
  455. return;
  456. }
  457. ///有设备在线被选中,则让它不被选择
  458. if (device.connectType == 3) {
  459. var deviceMacId = that.data.deviceMacId;
  460. if (!constant.strings.isEmpty(deviceMacId)) {
  461. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  462. constant.app.unsubscribe(pubResponse);
  463. var indexPage = that.data.indexPage;
  464. that.setData({
  465. deviceMacId: null,
  466. actionIndex: null,
  467. deviceListSelect: null,
  468. indexPage: indexPage == 1 ? 0 : indexPage,
  469. });
  470. };
  471. } else {
  472. // console.log("退出登录,断开连接", deviceList);
  473. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  474. // var indexPage = that.data.indexPage;
  475. // that.setData({
  476. // deviceMacId: null,
  477. // actionIndex: null,
  478. // deviceListSelect: null,
  479. // indexPage: indexPage == 1 ? 0 : indexPage,
  480. // });
  481. }
  482. },
  483. ///点击item
  484. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  485. async onTapItem(e) {
  486. var that = this;
  487. var isLogin = that.getIsLogin();
  488. if (!isLogin) {
  489. constant.routeUtil.jump(constant.routePath.login);
  490. return;
  491. }
  492. var index = e.currentTarget.dataset.index;
  493. var device = e.currentTarget.dataset.item;
  494. var connectType = device.connectType;
  495. var deviceListSelect = that.getDeviceListSelect();
  496. // wifi只支持在线点击
  497. if (connectType == 3 && device.state === "online") {
  498. if (index === deviceListSelect) {
  499. that.goWake();
  500. } else {
  501. // 现断线第一个
  502. // that.disconnectFirst()
  503. that.addWifiDevice(device);
  504. }
  505. return;
  506. }
  507. ///去蓝牙连接处理
  508. // {"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 妙播收音机"}
  509. var deviceList = that.getDeviceList();
  510. let curDevice = deviceList[index]
  511. if (index === deviceListSelect && device.state === "online") {
  512. var param = "?param=" + JSON.stringify(curDevice);
  513. constant.routeUtil.jumpParam(constant.routePath.deviceDetail, param);
  514. } else if (device.state === "offline") {
  515. var deviceListSelect = that.getDeviceListSelect();
  516. if (deviceListSelect != null) {
  517. if (deviceList.length > deviceListSelect) {
  518. var currentDevice = deviceList[deviceListSelect];
  519. console.log("断开连接0", currentDevice)
  520. // 现断线第一个
  521. // that.disconnectFirst()
  522. }
  523. that.setData({
  524. deviceListSelect: null,
  525. });
  526. }
  527. console.log("断开连接2", device)
  528. device.name = device.devName;
  529. that.data._clickOtherDevice = true;
  530. BtHelper.getInstance().connect(device, function (isConnected, d) {
  531. that.data._clickOtherDevice = false;
  532. if (isConnected) {
  533. device.state = "online";
  534. that.addBlueDevice(device);
  535. }
  536. });
  537. } else {
  538. var deviceList = that.getDeviceList();
  539. var deviceListSelect = that.getDeviceListSelect();
  540. if (deviceListSelect != null) {
  541. if (deviceList.length > deviceListSelect) {
  542. var currentDevice = deviceList[deviceListSelect];
  543. if (currentDevice.connectType === 1) {
  544. console.log("断开连接1", currentDevice)
  545. await BtHelper.getInstance().disconnect(currentDevice);
  546. }
  547. // await time_util.delayMills(500);
  548. }
  549. that.setData({
  550. deviceListSelect: null,
  551. });
  552. }
  553. that.data._clickOtherDevice = true;
  554. device.name = device.devName;
  555. BtHelper.getInstance().connect(device, function (isConnected, d) {
  556. that.data._clickOtherDevice = false;
  557. if (isConnected) {
  558. device.state = "online";
  559. that.addBlueDevice(device);
  560. }
  561. });
  562. }
  563. },
  564. disconnectFirst() {
  565. let that = this;
  566. let deviceList = that.getDeviceList()
  567. let first = deviceList[0] ?? {}
  568. if (first.state == "online") {
  569. if (first.connectType === 1) {
  570. BtHelper.getInstance().disconnect(first)
  571. first.state == "offline"
  572. } else {
  573. // 取消订阅
  574. // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
  575. //constant.app.unsubscribe(pubResponse);
  576. }
  577. }
  578. },
  579. // 去唤醒界面
  580. goWake() {
  581. var that = this;
  582. var deviceList = that.getDeviceList();
  583. var deviceListSelect = that.getDeviceListSelect();
  584. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  585. return
  586. };
  587. var device = deviceList[deviceListSelect];
  588. var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel;
  589. constant.routeUtil.jumpParam(constant.routePath.deviceWake, param);
  590. },
  591. ///删除当前设备
  592. deleteDevice(e) {
  593. var that = this;
  594. var index = e.currentTarget.dataset.index;
  595. wx.showModal({
  596. title: '确定删除?',
  597. success: function (res) {
  598. if (res.confirm) {
  599. var deviceList = that.getDeviceList();
  600. if (deviceList[index].connectType == 3) {
  601. that.cancelWifi(index, false);
  602. } else {
  603. console.log("确定删除0", index, deviceList[index].deviceId)
  604. that.cancelBlue(index, false);
  605. }
  606. }
  607. }
  608. });
  609. },
  610. ///是否是同一个蓝牙
  611. isTheSameBlue(device) {
  612. var that = this;
  613. var deviceList = that.getDeviceList();
  614. var deviceListSelect = that.getDeviceListSelect();
  615. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  616. return false;
  617. };
  618. if (deviceList[deviceListSelect].connectType == 3) {
  619. return false;
  620. } else {
  621. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  622. return true;
  623. } else {
  624. return false;
  625. }
  626. }
  627. },
  628. ///断开当前的
  629. cancelCurrent() {
  630. var that = this;
  631. var deviceList = that.getDeviceList();
  632. var deviceListSelect = that.getDeviceListSelect();
  633. if (deviceList[deviceListSelect].connectType == 3) {
  634. that.cancelWifi(deviceListSelect, true);
  635. } else {
  636. that.cancelBlue(deviceListSelect, true);
  637. }
  638. },
  639. /// 断开蓝牙连接
  640. cancelBlue(index, onlyCancel) {
  641. var that = this;
  642. if (onlyCancel) {
  643. return;
  644. }
  645. var deviceList = that.getDeviceList();
  646. var deviceListSelect = that.getDeviceListSelect();
  647. ///是否删除的当前播放这个
  648. var isCurrentIndex = false;
  649. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  650. isCurrentIndex = (deviceListSelect == index);
  651. };
  652. let device = deviceList[index];
  653. ///删除当前设备
  654. var deviceId = device.deviceId;
  655. deviceList = deviceList.filter((item, i) => {
  656. return deviceId !== item.deviceId;
  657. });
  658. constant.store.setStore("deviceList", deviceList);
  659. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  660. var indexPage = that.data.indexPage;
  661. that.setData({
  662. deviceList: deviceList,
  663. indexPage: indexPage == 1 ? 0 : indexPage,
  664. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  665. });
  666. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  667. if (device.state === "online") {
  668. BtHelper.getInstance().disconnect({
  669. "deviceId": deviceId
  670. });
  671. }
  672. },
  673. /// 断开连接wifi
  674. cancelWifi(index, onlyCancel) {
  675. var that = this;
  676. var deviceList = that.getDeviceList();
  677. if (index === null || deviceList.length <= index) {
  678. return;
  679. };
  680. var deviceId = deviceList[index].deviceId;
  681. // 取消订阅
  682. var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
  683. constant.app.unsubscribe(pubResponse);
  684. if (onlyCancel) {
  685. return;
  686. }
  687. ///是否删除的当前播放这个
  688. var isCurrentIndex = false;
  689. var deviceListSelect = that.getDeviceListSelect();
  690. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  691. isCurrentIndex = (deviceListSelect == index);
  692. };
  693. deviceList = deviceList.filter((item, i) => {
  694. return deviceId !== item.deviceId;
  695. });
  696. constant.store.setStore("deviceList", deviceList);
  697. var indexPage = that.data.indexPage;
  698. var deviceMacId = that.data.deviceMacId;
  699. var actionIndex = that.data.actionIndex;
  700. that.setData({
  701. deviceList: deviceList,
  702. deviceMacId: isCurrentIndex ? null : deviceMacId,
  703. actionIndex: isCurrentIndex ? null : actionIndex,
  704. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  705. indexPage: indexPage == 1 ? 0 : indexPage,
  706. });
  707. },
  708. /// 新添加蓝牙设备
  709. addBlueDevice(device) {
  710. var that = this;
  711. var addDeviceList = constant.lexinAdd.addBlueDevice(device);
  712. let hasOnline = 0
  713. addDeviceList.forEach(item => {
  714. // 蓝牙设备下线。wifi不动
  715. if (item.deviceId != device.deviceId && item.connectType === 1) {
  716. item.state = "offline"
  717. }
  718. })
  719. console.log("新添加蓝牙设备", addDeviceList)
  720. that.updateDeviceList(addDeviceList, false, false);
  721. that.setData({
  722. deviceListSelect: 0,
  723. autoConnected: true,
  724. });
  725. that.addNotification()
  726. },
  727. disconnectDev(event, device) {
  728. let _this = this;
  729. if (_this.data._clickOtherDevice) {
  730. _this.data._clickOtherDevice = false
  731. return;
  732. }
  733. var selectDev = _this.getCurrentDevice()
  734. console.log("断开连接--", event.commonValue, event.deviceId, selectDev);
  735. let selectDevId = "";
  736. if (selectDev) {
  737. selectDevId = selectDev.deviceId;
  738. }
  739. var isSelect = selectDev === null || selectDevId === event.deviceId
  740. if (isSelect && selectDevId === event.deviceId && event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  741. _this.data._hasShowModal = true
  742. wx.showModal({
  743. title: '提示',
  744. content: '设备断开连接,请重新连接设备',
  745. showCancel: false,
  746. success: function (res) {
  747. _this.data._hasShowModal = false;
  748. if (res.confirm) {
  749. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  750. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  751. }
  752. }
  753. }
  754. })
  755. }
  756. // 设备断开连接,请重新连接设备
  757. },
  758. addNotification() {
  759. let that = this;
  760. constant.eventBus.removeNotification(CmdEvent.eventName, that);
  761. constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
  762. let deviceList = that.getDeviceList()
  763. // console.log("收到监听:", JSON.stringify(event))
  764. if (event.cmdEvent == EnumCmdEvent.btMac) {
  765. let disDevice = deviceList.find(item => {
  766. return item.state == "online" && item.connectType === 1
  767. })
  768. if (disDevice) {
  769. disDevice.mac = event.btMac
  770. console.log("设备mac:", disDevice.mac)
  771. }
  772. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  773. let isFirst = false;
  774. // 有下线就开启监听扫描
  775. if (event.commonValue == "online") {
  776. console.log("有蓝牙设备上线")
  777. that.stopIntervalId1();
  778. } else {
  779. // 有wifi设备在线
  780. let wifiDevice = deviceList.find(item => {
  781. return item.state == "online" && item.connectType === 3
  782. })
  783. if (!wifiDevice) {
  784. that.startBleTimer(isFirst, true);
  785. }
  786. }
  787. console.log("首页ble通知:", event.deviceId, event.commonValue)
  788. let hasOnleBle = deviceList.find(item => {
  789. return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
  790. })
  791. let disDevice = deviceList.find(item => {
  792. return item.deviceId == event.deviceId && item.connectType == 1
  793. })
  794. // that.data.deviceList.forEach(element => {
  795. // if (item.deviceId == tmp.deviceId) {
  796. // }
  797. // });
  798. if (!disDevice) {
  799. // console.log("")
  800. console.log("没有找到在线的对应的蓝牙设备", deviceList.length)
  801. return;
  802. }
  803. disDevice.state = event.commonValue;
  804. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  805. if (event.commonValue == "online") {
  806. // BtHelper.getInstance().connect()
  807. console.log("首页连接上线", disDevice)
  808. } else {
  809. if (hasOnleBle) {
  810. that.disconnectDev(event, disDevice)
  811. }
  812. console.log("首页连接断开", disDevice)
  813. that.updateDeviceList(deviceList, false, false);
  814. }
  815. }
  816. }, that);
  817. },
  818. // 新添加Wifi设备
  819. addWifiDevice(device) {
  820. var that = this;
  821. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  822. if (!isOK) {
  823. constant.app.connect();
  824. setTimeout(() => {
  825. that.addWifiDevice(device);
  826. }, 500);
  827. return;
  828. };
  829. console.log("有新的wifi设备0", device)
  830. var deviceId = device.deviceId;
  831. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  832. console.log("有新的wifi设备", addDeviceList)
  833. that.updateDeviceList(addDeviceList, false, false);
  834. var indexPage = that.data.indexPage;
  835. that.setData({
  836. deviceListSelect: null,
  837. connectDeviceIding: deviceId,
  838. indexPage: indexPage == 1 ? 0 : indexPage,
  839. });
  840. constant.lexinSubscribe.subscribeSingleDevice(device);
  841. },
  842. refreshCurrentDevice(device) {
  843. var that = this;
  844. console.log("刷新列表0:", device)
  845. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  846. console.log("刷新列表1:", addDeviceList)
  847. that.updateDeviceList(addDeviceList, false, false);
  848. that.setData({
  849. autoConnected: true,
  850. deviceListSelect: 0,
  851. connectDeviceIding: "",
  852. });
  853. },
  854. /// 手机关闭蓝牙,所有蓝牙设备离线
  855. closeBlueResetOffline(isInit, closeAllBlue) {
  856. var that = this;
  857. var deviceList = that.getDeviceList();
  858. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  859. },
  860. /// 更新列表排序
  861. updateDeviceList(deviceList, isInit, closeAllBlue) {
  862. var that = this;
  863. var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
  864. constant.store.setStore("deviceList", finalList);
  865. that.setData({
  866. deviceList: finalList,
  867. });
  868. },
  869. ///点击banner事件
  870. onTapBanner(e) {
  871. var item = e.currentTarget.dataset.item;
  872. try {
  873. if (constant.strings.isEmpty(item.forwardUrl)) {
  874. return;
  875. }
  876. // 内链
  877. if (item.forwardType == 0) {
  878. var param = "?param=" + item.forwardUrl;
  879. constant.app.webSkipUrl = item.forwardUrl;
  880. constant.routeUtil.jumpParam(constant.routePath.webview, param);
  881. }
  882. // 外链
  883. else if (item.forwardType == 1) {
  884. } else {
  885. }
  886. } catch (err) {
  887. console.log("跳转失败", err)
  888. }
  889. },
  890. ///去充值内容会员
  891. toRecharge(e) {
  892. var device = e.currentTarget.dataset.item;
  893. },
  894. ///添加设备
  895. jumpToAddDevice() {
  896. var that = this;
  897. var isLogin = that.getIsLogin();
  898. if (!isLogin) {
  899. var param = "?toDeviceList=" + true;
  900. constant.routeUtil.jumpParam(constant.routePath.login, param);
  901. return;
  902. }
  903. constant.routeUtil.jump(constant.routePath.deviceList);
  904. },
  905. // 关于我们
  906. jumpToAboutUs() {
  907. constant.lexinJump.toAboutUs();
  908. },
  909. /// 是否已登录
  910. getIsLogin() {
  911. var that = this;
  912. var isLogin = that.data.isLogin;
  913. return isLogin;
  914. },
  915. /// 获取列表数据
  916. getDeviceList() {
  917. var that = this;
  918. var deviceList = that.data.deviceList;
  919. return deviceList;
  920. },
  921. ///获取当前选中的
  922. getDeviceListSelect() {
  923. var that = this;
  924. var deviceListSelect = that.data.deviceListSelect;
  925. return deviceListSelect;
  926. },
  927. ///获取是否已经第一次连接过
  928. getAutoConnected() {
  929. var that = this;
  930. var autoConnected = that.data.autoConnected;
  931. return autoConnected;
  932. },
  933. ///当前正在连接的设备id
  934. getConnectDeviceIding() {
  935. var that = this;
  936. var connectDeviceIding = that.data.connectDeviceIding
  937. return connectDeviceIding;
  938. },
  939. ///获取频道数据
  940. getChannelData() {
  941. var that = this;
  942. var channelData = that.data.channelData;
  943. return channelData;
  944. },
  945. /// 当前播放频道
  946. getActionIndex() {
  947. var that = this;
  948. var actionIndex = that.data.actionIndex;
  949. return actionIndex;
  950. },
  951. /// 获取当前的device
  952. getCurrentDevice() {
  953. var that = this;
  954. var deviceList = that.getDeviceList();
  955. var deviceListSelect = that.getDeviceListSelect();
  956. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  957. return null;
  958. };
  959. var device = deviceList[deviceListSelect];
  960. return device;
  961. },
  962. /// 获取当前的deviceId
  963. getCurrentDeviceId() {
  964. var that = this;
  965. var deviceList = that.getDeviceList();
  966. var deviceListSelect = that.getDeviceListSelect();
  967. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  968. return "";
  969. };
  970. var deviceId = deviceList[deviceListSelect].deviceId;
  971. return deviceId;
  972. },
  973. ///设置设备相关信息
  974. setDeviceInfor(other) {
  975. var that = this;
  976. // 接收设备当前信息
  977. that.getchannelData(other.ProdModel);
  978. // 电量
  979. if (other.Power) {
  980. var battery = constant.lexinMessage.getBattery(other.Power);
  981. that.setData({
  982. battery: battery,
  983. });
  984. }
  985. // 当前设备木有设置定时
  986. that.setData({
  987. isSetWake: false,
  988. });
  989. other.alarm.map((v) => {
  990. if (v.enable === "1") {
  991. that.setData({
  992. isSetWake: true
  993. });
  994. }
  995. });
  996. },
  997. ///******************************* 可折叠 ********************************************///
  998. onUserInfoLoad() {
  999. var that = this;
  1000. var userInfo = wx.getStorageSync("userInfo") || "";
  1001. if (!constant.strings.isEmpty(userInfo)) {
  1002. var phone = userInfo.phone;
  1003. if (!phone) {
  1004. return;
  1005. };
  1006. var nickName = userInfo.nickname || "";
  1007. var greeting = that.getGreetBuNickName(nickName);
  1008. // 获取缓存的频道数据
  1009. var channelData = wx.getStorageSync("channelData") || "";
  1010. if (!constant.strings.isEmpty(channelData)) {
  1011. that.setData({
  1012. channelData: channelData,
  1013. greeting: greeting,
  1014. nickName: nickName,
  1015. userPic: userInfo.headUrl || "",
  1016. isLogin: true,
  1017. })
  1018. } else {
  1019. that.setData({
  1020. greeting: greeting,
  1021. nickName: nickName,
  1022. userPic: userInfo.headUrl || "",
  1023. isLogin: true,
  1024. });
  1025. }
  1026. } else {
  1027. var greeting = that.getGreetBuNickName("");
  1028. that.setData({
  1029. greeting: greeting,
  1030. })
  1031. }
  1032. },
  1033. getGreetBuNickName(nickName) {
  1034. var greeting = constant.timeUtil.getGreet();
  1035. if (nickName != "" && nickName != "未登录") {
  1036. greeting = greeting + ',' + nickName;;
  1037. }
  1038. return greeting;
  1039. },
  1040. onBannerLoad() {
  1041. var that = this;
  1042. constant.apiUtil.getBanner({}).then((res) => {
  1043. that.setData({
  1044. bannerList: res,
  1045. });
  1046. constant.store.setStore("homeBanner", res);
  1047. });
  1048. },
  1049. stopIntervalId1: function () {
  1050. var that = this;
  1051. if (!constant.strings.isEmpty(that.data.intervalId1)) {
  1052. clearInterval(that.data.intervalId1);
  1053. that.data.intervalId1 = null;
  1054. }
  1055. },
  1056. stopIntervalId2: function () {
  1057. var that = this;
  1058. if (!constant.strings.isEmpty(that.data.intervalId2)) {
  1059. clearInterval(that.data.intervalId2);
  1060. that.data.intervalId2 = null;
  1061. }
  1062. },
  1063. ///销毁蓝牙
  1064. onUnload() {
  1065. var that = this;
  1066. constant.eventBus.removeNotification(CmdEvent.eventName, that)
  1067. console.log("界面onUnload")
  1068. that.stopIntervalId1();
  1069. that.stopIntervalId2();
  1070. if (!constant.strings.isEmpty(that.data.intervalId)) {
  1071. clearInterval(that.data.intervalId);
  1072. that.data.intervalId = null;
  1073. }
  1074. BtHelper.getInstance().disconnect();
  1075. if (constant.app.globalData.client === null) {
  1076. return;
  1077. };
  1078. constant.app.globalData.client.end(true);
  1079. constant.app.globalData.client = null;
  1080. },
  1081. })