index.js 30 KB

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