index.js 29 KB

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