index.js 26 KB

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