index.js 26 KB

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