index.js 27 KB

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