index.js 26 KB

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