index.js 28 KB

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