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