index.js 26 KB

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