index.js 26 KB

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