index.js 27 KB

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