index.js 27 KB

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