index.js 25 KB

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