index.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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: true,
  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. var isFirst = true;
  225. console.log("开始监听蓝牙设备");
  226. ///再秒再对比一次
  227. that.stopIntervalId1();
  228. let count = 0;
  229. that.data.intervalId1 = setInterval(async function () {
  230. isFirst = false;
  231. that.compareList();
  232. if (count > 3) {
  233. that.stopIntervalId1();
  234. } else {
  235. count++;
  236. }
  237. }, isFirst ? 6 * 1000 : 12 * 1000);
  238. }
  239. },
  240. /// 对比蓝牙数据
  241. compareList() {
  242. var that = this;
  243. if (that.data.deviceList.length == 1) {
  244. let device = that.data.deviceList[0];
  245. if (device.state === 'online') {
  246. return;
  247. }
  248. getApp().getBluetoothStatus();
  249. that.data._willConnectBle = true;
  250. return;
  251. }
  252. lexin_scan.compareList(function (list) {
  253. that.updateDeviceList(list, false, false);
  254. }, function (item) {
  255. that.addBlueDevice(item);
  256. });
  257. },
  258. getBluetoothStatusCallck(v) {
  259. var that = this;
  260. if (!that.data._willConnectBle) {
  261. // true 走下面
  262. return;
  263. }
  264. if (v) {
  265. var device = that.data.deviceList[0];
  266. console.log("开始连接设备0", device);
  267. if (device.state === 'online' || device.connectType != 1) {
  268. return;
  269. }
  270. // });
  271. BtHelper.getInstance().connect(device, function (isConnected, d) {
  272. if (isConnected) {
  273. device.state = 'online';
  274. that.addBlueDevice(device);
  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 = false;
  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. 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. if (index === deviceListSelect) {
  539. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(device))
  540. } else if (device.state === "offline") {
  541. console.log("去连接蓝牙", device)
  542. device.name = device.devName;
  543. BtHelper.getInstance().connect(device, function (isConnected, d) {
  544. if (isConnected) {
  545. device.state = "online";
  546. that.addBlueDevice(device);
  547. }
  548. });
  549. } else {
  550. // {"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 妙播收音机"}
  551. }
  552. },
  553. // 去唤醒界面
  554. goWake() {
  555. var that = this;
  556. var deviceList = that.getDeviceList();
  557. var deviceListSelect = that.getDeviceListSelect();
  558. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  559. return
  560. };
  561. var device = deviceList[deviceListSelect];
  562. wx.navigateTo({
  563. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  564. });
  565. },
  566. ///删除当前设备
  567. deleteDevice(e) {
  568. var that = this;
  569. var index = e.currentTarget.dataset.index;
  570. wx.showModal({
  571. title: '确定删除?',
  572. success: function (res) {
  573. if (res.confirm) {
  574. var deviceList = that.getDeviceList();
  575. if (deviceList[index].connectType == 3) {
  576. that.cancelWifi(index, false);
  577. } else {
  578. that.cancelBlue(index, false);
  579. }
  580. }
  581. }
  582. });
  583. },
  584. ///是否是同一个蓝牙
  585. isTheSameBlue(device) {
  586. var that = this;
  587. var deviceList = that.getDeviceList();
  588. var deviceListSelect = that.getDeviceListSelect();
  589. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  590. return false;
  591. };
  592. if (deviceList[deviceListSelect].connectType == 3) {
  593. return false;
  594. } else {
  595. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  596. return true;
  597. } else {
  598. return false;
  599. }
  600. }
  601. },
  602. ///断开当前的
  603. cancelCurrent() {
  604. var that = this;
  605. var deviceList = that.getDeviceList();
  606. var deviceListSelect = that.getDeviceListSelect();
  607. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  608. return;
  609. };
  610. if (deviceList[deviceListSelect].connectType == 3) {
  611. that.cancelWifi(deviceListSelect, true);
  612. } else {
  613. that.cancelBlue(deviceListSelect, true);
  614. }
  615. },
  616. /// 断开蓝牙连接
  617. cancelBlue(index, onlyCancel) {
  618. var that = this;
  619. if (onlyCancel) {
  620. return;
  621. }
  622. var deviceList = that.getDeviceList();
  623. var deviceListSelect = that.getDeviceListSelect();
  624. if (deviceList.length > 1 && (deviceListSelect === null || deviceList.length <= deviceListSelect)) {
  625. // 2个设备才判断,1个直接删除
  626. return;
  627. };
  628. ///是否删除的当前播放这个
  629. var isCurrentIndex = false;
  630. deviceListSelect = deviceListSelect ?? 0;
  631. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  632. isCurrentIndex = (deviceListSelect == index);
  633. };
  634. ///删除当前设备
  635. var deviceId = deviceList[index].deviceId;
  636. deviceList = deviceList.filter((item, i) => {
  637. return deviceId !== item.deviceId;
  638. });
  639. console.log("断开蓝牙连接", deviceId)
  640. BtHelper.getInstance().disconnect({ "deviceId": deviceId });
  641. store.setStore("deviceList", deviceList);
  642. var indexPage = that.data.indexPage;
  643. that.setData({
  644. deviceList: deviceList,
  645. indexPage: indexPage == 1 ? 0 : indexPage,
  646. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  647. });
  648. },
  649. /// 断开连接wifi
  650. cancelWifi(index, onlyCancel) {
  651. var that = this;
  652. var deviceList = that.getDeviceList();
  653. if (index === null || deviceList.length <= index) {
  654. return;
  655. };
  656. var deviceId = deviceList[index].deviceId;
  657. // 取消订阅
  658. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  659. app.unsubscribe(pubResponse);
  660. if (onlyCancel) {
  661. return;
  662. }
  663. ///是否删除的当前播放这个
  664. var isCurrentIndex = false;
  665. var deviceListSelect = that.getDeviceListSelect();
  666. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  667. isCurrentIndex = (deviceListSelect == index);
  668. };
  669. deviceList = deviceList.filter((item, i) => {
  670. return deviceId !== item.deviceId;
  671. });
  672. store.setStore("deviceList", deviceList);
  673. var indexPage = that.data.indexPage;
  674. var deviceMacId = that.data.deviceMacId;
  675. var actionIndex = that.data.actionIndex;
  676. that.setData({
  677. deviceList: deviceList,
  678. deviceMacId: isCurrentIndex ? null : deviceMacId,
  679. actionIndex: isCurrentIndex ? null : actionIndex,
  680. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  681. indexPage: indexPage == 1 ? 0 : indexPage,
  682. });
  683. },
  684. /// 新添加蓝牙设备
  685. addBlueDevice(device) {
  686. var that = this;
  687. var addDeviceList = lexin_add.addBlueDevice(device);
  688. that.updateDeviceList(addDeviceList, false, false);
  689. that.setData({
  690. deviceListSelect: 0,
  691. autoConnected: true,
  692. });
  693. event_bus.removeNotification(CmdEvent.eventName, that);
  694. event_bus.addNotification(CmdEvent.eventName, function (event) {
  695. if (event.cmdEvent == EnumCmdEvent.onoffline) {
  696. if (tmp.connected != false) {
  697. return
  698. }
  699. let disDevice = that.data.deviceList.find(item => {
  700. return item.deviceId == event.deviceId
  701. })
  702. // that.data.deviceList.forEach(element => {
  703. // if (item.deviceId == tmp.deviceId) {
  704. // }
  705. // });
  706. disDevice.state = event.commonValue;
  707. console.log("断开连接", disDevice)
  708. that.updateDeviceList(that.data.deviceList, false, false);
  709. }
  710. });
  711. },
  712. // 新添加Wifi设备
  713. addWifiDevice(device) {
  714. var that = this;
  715. var isOK = app.globalData.client && app.globalData.client.connected;
  716. if (!isOK) {
  717. app.connect();
  718. setTimeout(() => {
  719. that.addWifiDevice(device);
  720. }, 500);
  721. return;
  722. };
  723. var deviceId = device.deviceId;
  724. var addDeviceList = lexin_add.addWifiDevice(device);
  725. that.updateDeviceList(addDeviceList, false, false);
  726. var indexPage = that.data.indexPage;
  727. that.setData({
  728. deviceListSelect: null,
  729. connectDeviceIding: deviceId,
  730. indexPage: indexPage == 1 ? 0 : indexPage,
  731. });
  732. lexin_subscribe.subscribeSingleDevice(device);
  733. },
  734. refreshCurrentDevice(device) {
  735. var that = this;
  736. var addDeviceList = lexin_add.addWifiDevice(device);
  737. that.updateDeviceList(addDeviceList, false, false);
  738. that.setData({
  739. autoConnected: true,
  740. deviceListSelect: 0,
  741. connectDeviceIding: "",
  742. });
  743. },
  744. /// 手机关闭蓝牙,所有蓝牙设备离线
  745. closeBlueResetOffline(isInit, closeAllBlue) {
  746. var that = this;
  747. var deviceList = that.getDeviceList();
  748. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  749. },
  750. /// 更新列表排序
  751. updateDeviceList(deviceList, isInit, closeAllBlue) {
  752. var that = this;
  753. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  754. if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
  755. store.setStore("deviceList", finalList);
  756. // todo 测试代码
  757. // finalList.forEach(element => {
  758. // element.state = "online";
  759. // });
  760. that.setData({
  761. deviceList: finalList,
  762. });
  763. }
  764. },
  765. ///点击banner事件
  766. onTapBanner() {
  767. var that = this;
  768. var item = e.currentTarget.dataset.item;
  769. },
  770. ///添加设备
  771. jumpToAddDevice() {
  772. var that = this;
  773. var isLogin = that.getIsLogin();
  774. if (!isLogin) {
  775. route_util.jump(route_constant.login);
  776. return;
  777. }
  778. route_util.jump(route_constant.deviceList);
  779. },
  780. // 关于我们
  781. jumpToAboutUs() {
  782. lexin_jump.toAboutUs();
  783. },
  784. /// 是否已登录
  785. getIsLogin() {
  786. var that = this;
  787. var isLogin = that.data.isLogin;
  788. return isLogin;
  789. },
  790. /// 获取列表数据
  791. getDeviceList() {
  792. var that = this;
  793. var deviceList = that.data.deviceList;
  794. return deviceList;
  795. },
  796. ///获取当前选中的
  797. getDeviceListSelect() {
  798. var that = this;
  799. var deviceListSelect = that.data.deviceListSelect;
  800. return deviceListSelect;
  801. },
  802. ///获取是否已经第一次连接过
  803. getAutoConnected() {
  804. var that = this;
  805. var autoConnected = that.data.autoConnected;
  806. return autoConnected;
  807. },
  808. ///当前正在连接的设备id
  809. getConnectDeviceIding() {
  810. var that = this;
  811. var connectDeviceIding = that.data.connectDeviceIding
  812. return connectDeviceIding;
  813. },
  814. ///获取频道数据
  815. getChannelData() {
  816. var that = this;
  817. var channelData = that.data.channelData;
  818. return channelData;
  819. },
  820. /// 当前播放频道
  821. getActionIndex() {
  822. var that = this;
  823. var actionIndex = that.data.actionIndex;
  824. return actionIndex;
  825. },
  826. /// 获取当前的deviceId
  827. getCurrentDeviceId() {
  828. var that = this;
  829. var deviceList = that.getDeviceList();
  830. var deviceListSelect = that.getDeviceListSelect();
  831. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  832. return "";
  833. };
  834. var deviceId = deviceList[deviceListSelect].deviceId;
  835. return deviceId;
  836. },
  837. ///设置设备相关信息
  838. setDeviceInfor(other) {
  839. var that = this;
  840. // 接收设备当前信息
  841. that.getchannelData(other.ProdModel);
  842. // 电量
  843. if (other.Power) {
  844. var battery = lexin_message.getBattery(other.Power);
  845. that.setData({
  846. battery: battery,
  847. });
  848. }
  849. // 当前设备木有设置定时
  850. that.setData({
  851. isSetWake: false,
  852. });
  853. other.alarm.map((v) => {
  854. if (v.enable === "1") {
  855. that.setData({
  856. isSetWake: true
  857. });
  858. }
  859. });
  860. },
  861. ///******************************* 可折叠 ********************************************///
  862. onUserInfoLoad() {
  863. var that = this;
  864. var userInfo = wx.getStorageSync("userInfo") || "";
  865. if (!strings.isEmpty(userInfo)) {
  866. var phone = userInfo.phone;
  867. if (!phone) {
  868. return;
  869. };
  870. var nickName = userInfo.nickname || "";
  871. var greeting = that.getGreetBuNickName(nickName);
  872. // 获取缓存的频道数据
  873. var channelData = wx.getStorageSync("channelData") || "";
  874. if (!strings.isEmpty(channelData)) {
  875. that.setData({
  876. channelData: channelData,
  877. greeting: greeting,
  878. nickName: nickName,
  879. userPic: userInfo.headUrl || "",
  880. isLogin: true,
  881. })
  882. } else {
  883. that.setData({
  884. greeting: greeting,
  885. nickName: nickName,
  886. userPic: userInfo.headUrl || "",
  887. isLogin: true,
  888. });
  889. }
  890. } else {
  891. var greeting = that.getGreetBuNickName("");
  892. that.setData({
  893. greeting: greeting,
  894. })
  895. }
  896. },
  897. getGreetBuNickName(nickName) {
  898. var greeting = timeUtil.getGreet();
  899. if (nickName != "" && nickName != "未登录") {
  900. greeting = greeting + ',' + nickName;;
  901. }
  902. return greeting;
  903. },
  904. onBannerLoad() {
  905. var that = this;
  906. // var bannerList = wx.getStorageSync("homeBanner") || [];
  907. // if (that.data.bannerList.length > 0) {
  908. // that.setData({
  909. // bannerList: bannerList
  910. // });
  911. // }
  912. },
  913. onBannerShow() {
  914. var that = this;
  915. // getBanner({}).then((res) => {
  916. // that.setData({
  917. // bannerList: res
  918. // });
  919. // store.setStore("homeBanner", res);
  920. // })
  921. },
  922. stopIntervalId1: function () {
  923. var that = this;
  924. if (!strings.isEmpty(that.data.intervalId1)) {
  925. clearInterval(that.data.intervalId1);
  926. that.data.intervalId1 = null;
  927. }
  928. },
  929. stopIntervalId2: function () {
  930. var that = this;
  931. if (!strings.isEmpty(that.data.intervalId2)) {
  932. clearInterval(that.data.intervalId2);
  933. that.data.intervalId2 = null;
  934. }
  935. },
  936. ///销毁蓝牙
  937. onUnload() {
  938. var that = this;
  939. event_bus.removeNotification(CmdEvent.eventName, that)
  940. that.stopIntervalId1();
  941. that.stopIntervalId2();
  942. if (!strings.isEmpty(that.data.intervalId)) {
  943. clearInterval(that.data.intervalId);
  944. that.data.intervalId = null;
  945. }
  946. BtHelper.getInstance().disconnect();
  947. if (app.globalData.client === null) {
  948. return;
  949. };
  950. app.globalData.client.end(true);
  951. app.globalData.client.end(true);
  952. app.globalData.client = null;
  953. },
  954. })