index.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. const app = getApp();
  2. const {
  3. getBanner,
  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_message from '../../utils/lexin/message.js';
  17. import {
  18. BtHelper
  19. } from '../../devices/bt_helper.js';
  20. Page({
  21. data: {
  22. nvabarData: {
  23. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  24. title: 'OhPlay', //导航栏 中间的标题
  25. },
  26. ///下午好
  27. isLogin: false,
  28. greeting: "",
  29. bannerList: [{
  30. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  31. },
  32. {
  33. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  34. }
  35. ],
  36. autoplay: true,
  37. interval: 3000, // 切换时间间隔
  38. duration: 500, // 滑动动画时长
  39. circular: true, // 衔接滑动
  40. indexPage: 0,
  41. ///是否展示频道
  42. showChannel: false,
  43. ////首页
  44. navBarHeight: app.globalData.navBarHeight,
  45. MenuButtonheight: app.globalData.MenuButtonheight,
  46. MenuButtonTop: app.globalData.MenuButtonTop,
  47. actionIndex: null,
  48. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  49. channelData: [],
  50. isOneLoading: true,
  51. uid: null,
  52. isSetWake: false,
  53. battery: 4, // 0≤电量<20,0格
  54. ////我的界面
  55. loginStatus: true,
  56. nickName: "未登录",
  57. userPic: './../../img/head_pic.png',
  58. intervalId: null,
  59. /// 是否第一次自动连接过
  60. autoConnected: false,
  61. intervalId1: null,
  62. intervalId2: null,
  63. deviceMacId: null,
  64. deviceListSelect: null,
  65. deviceList: [],
  66. connectDeviceIding: "",
  67. },
  68. onLoad(options) {
  69. var that = this;
  70. that.onDeviceLoad();
  71. that.onUserInfoLoad();
  72. that.onBannerLoad();
  73. },
  74. onShow() {
  75. var that = this;
  76. that.onBannerShow();
  77. },
  78. onDeviceLoad() {
  79. var that = this;
  80. var deviceList = store.getStore("deviceList");
  81. if (!strings.isEmpty(deviceList)) {
  82. that.updateDeviceList(deviceList, true, false);
  83. }
  84. ///监听蓝牙设备
  85. BtHelper.getInstance().initBluetoothAdapter();
  86. BtHelper.getInstance().getBluetoothDevices();
  87. BtHelper.getInstance().startScan(null, null, null);
  88. var isFirst = true;
  89. ///再秒再对比一次
  90. that.stopIntervalId1();
  91. that.data.intervalId1 = setInterval(async function () {
  92. isFirst = false;
  93. that.compareList();
  94. }, isFirst ? 6 * 1000 : 12 * 1000);
  95. },
  96. /// 对比蓝牙数据
  97. compareList() {
  98. var that = this;
  99. lexin_scan.compareList(function (list) {
  100. that.updateDeviceList(list, false, false);
  101. }, function (item) {
  102. that.addBlueDevice(item);
  103. });
  104. },
  105. // 回调
  106. mqttCallback(type, option) {
  107. // console.log("gadsfadsfadsfa==888===" + type);
  108. console.log("gadsfqwerqwerqr====22==" + type);
  109. var that = this;
  110. let payloads = null;
  111. if (option && option.payload) {
  112. payloads = JSON.parse(option.payload);
  113. };
  114. switch (type) {
  115. ///先走订阅
  116. case "connect":
  117. lexin_message.subscribeAllDevice();
  118. break;
  119. ///再走有在线
  120. case "message_onoffline":
  121. lexin_message.onlineDevice(payloads, function (list) {
  122. that.updateDeviceList(list, false, false);
  123. });
  124. break;
  125. // PlayState: ///播放状态:0默认状态,1播放状态,2暂停状态, 3停止状态,4缓冲状态。
  126. case "message":
  127. if (payloads) {
  128. // payloads.other: {"songInfoID":"843265795768623127","songFrom":"4","songAlbumID":"","artist":"巴洛克古典乐","title":"巴洛克古典乐","url":"http://speed.radio1964.cn/hls/1fmottosbaroquemusicradiozug.m3u8","albumURI":"http://airsmart-photo1.oss-cn-shanghai.aliyuncs.com/img/20211122/98101225b3cb4a0fb5728159ce4ca6d5.png","realUrl":"http://speed.radio1964.cn/hls/1fmottosbaroquemusicradiozug.m3u8","channel":5,"Duration":0,"RelTime":0,"PlayState":1,"PlayMode":0,"SongType":2,"userid":"1","channelType":1,"platformId":4,"platformName":"海外电台","isVip":0,"playable":1,"unplayableMsg":"","unplayableCode":0,"vipIconType":0,"vipIconTypeName":""}
  129. if (payloads.type === "get_position" && payloads.other) {
  130. let actionIndex = null;
  131. that.data.channelData.map((v, index) => {
  132. if (v.channelNum === payloads.other.channel) {
  133. actionIndex = index;
  134. }
  135. });
  136. that.setData({
  137. actionIndex,
  138. });
  139. }
  140. // payloads.other: {"Guid":"AIrSMArT_7cdfa1fd3af0","ProdModel":"MW-2AX(WIFI)","sim_iccid":"","esim_iccid":"","SoftVer":"3.0.7","Firmware":"RTOS","Power":50,"lowBatteryNtf":0,"Volume":27,"Mute":0,"pauseShutdown":0,"PlayState":1,"PlayMode":0,"devName":"猫王小王子OTR-X","deviceId":"","deviceSecret":"","is_tts_enable":1,"SignalQuality":100,"CardIndex":0,"CardSum":0,"NetModel":2,"is_netmode_auto":2,"Imei":"","mid":"","deviceDSN":"1044250776739909632","userid":"1","user_id_white":"1039072","alarm":[{"alarm_id":"0","alarm_name":"open","current_timestamp":"2024-12-12 00:00:00","on_off_timestamp":"16:19:25","operation":"on","song_uri":"","weekly_repeat":"0","enable":"0","action":"update","week_actives":[0,0,0,0,0,0,0]},{"alarm_id":"1","alarm_name":"open","current_timestamp":"2024-12-11 20:00:48","on_off_timestamp":"07:00:00","operation":"on","song_uri":"","weekly_repeat":"0","enable":"1","action":"update","week_actives":[0,0,0,0,0,0,0]}]}
  141. // 更新信息
  142. else if (payloads.type === "get_dev_info" && payloads.other) {
  143. console.log("gadsfqwerqwerqr====555==" + payloads.type);
  144. // 接收设备当前信息
  145. that.getchannelData(payloads.other.ProdModel);
  146. // 电量
  147. that.setData({
  148. battery: that._battery(payloads.other.Power),
  149. })
  150. // 当前设备木有设置定时
  151. that.setData({
  152. isSetWake: false,
  153. });
  154. payloads.other.alarm.map((v) => {
  155. if (v.enable === "1") {
  156. that.setData({
  157. isSetWake: true
  158. });
  159. }
  160. });
  161. ///连上就调用2次 payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  162. var isUpdate = false;
  163. var deviceList = that.getDeviceList();
  164. if (!strings.isEmpty(deviceList)) {
  165. var currentDeviceId = "";
  166. for (var i = 0; i < deviceList.length; i++) {
  167. if (payloads.SrcDeviceName) {
  168. var device = deviceList[i];
  169. var deviceId = device.deviceId;
  170. var splitDeviceId = deviceId.split("BLUFI_");
  171. if (splitDeviceId.length == 2) {
  172. var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
  173. ///彻底连接成功
  174. if (index !== -1) {
  175. currentDeviceId = deviceId;
  176. var ProdModel = payloads.other.ProdModel;
  177. var devName = payloads.other.devName;
  178. if (device.ProdModel != ProdModel || device.devName != devName) {
  179. isUpdate = true;
  180. device.ProdModel = ProdModel;
  181. device.devName = devName;
  182. }
  183. break;
  184. }
  185. }
  186. }
  187. }
  188. /// 去更新选中目标
  189. if (!strings.isEmpty(currentDeviceId)) {
  190. for (var i = 0; i < deviceList.length; i++) {
  191. var device = deviceList[i];
  192. var deviceId = device.deviceId;
  193. if (deviceId == currentDeviceId) {
  194. that.refreshCurrentDevice(device);
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. ///获取电量
  202. else if (payloads.type === "battery" && payloads.other) {
  203. that.setData({
  204. battery: that._battery(payloads.other.battery),
  205. })
  206. }
  207. ///获取播放状态
  208. else if (payloads.type === "play" || payloads.type === "play_state") {
  209. var deviceList = that.getDeviceList();
  210. var deviceListSelect = that.getDeviceListSelect();
  211. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  212. return;
  213. };
  214. var deviceId = deviceList[deviceListSelect].deviceId;
  215. // 接收设备当前播放状态
  216. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  217. const obj = {
  218. DstDeviceName: deviceMacId
  219. }
  220. app.PubMsg({
  221. type: "get_position",
  222. ...obj
  223. });
  224. }
  225. }
  226. break;
  227. default:
  228. }
  229. },
  230. ///去连接设备数据
  231. actionDevice(device) {
  232. var that = this;
  233. // 取消订阅
  234. var deviceMacId = that.data.deviceMacId;
  235. if (!strings.isEmpty(deviceMacId)) {
  236. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  237. app.unsubscribe(pubResponse);
  238. };
  239. var deviceId = device.deviceId;
  240. deviceMacId = lexin_util.getDeviceMacId(deviceId);
  241. that.setData({
  242. deviceMacId: deviceMacId,
  243. });
  244. that.subscribeCurrDevice(device);
  245. },
  246. ///数据处理
  247. subscribeCurrDevice(device) {
  248. var that = this;
  249. var isOK = app.globalData.client && app.globalData.client.connected;
  250. if (!isOK) {
  251. app.connect();
  252. setTimeout(() => {
  253. that.subscribeCurrDevice(device);
  254. }, 500);
  255. return;
  256. };
  257. console.log("gadsfqwerqwerqr====22==");
  258. var deviceId = device.deviceId;
  259. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  260. const obj = {
  261. DstDeviceName: deviceMacId
  262. };
  263. /// /AIrSMArT_7cdfa1fd3af0/user/pub_response
  264. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  265. app.subscribe(pubResponse);
  266. console.log("gadsfqwerqwerqr====333==");
  267. app.PubMsg({
  268. type: "get_dev_info",
  269. ...obj
  270. });
  271. },
  272. // 格式化电量
  273. _battery(battery) {
  274. let _battery = 0;
  275. if (battery < 20) {
  276. _battery = 0
  277. } else if (20 <= battery && battery < 40) {
  278. _battery = 1
  279. } else if (40 <= battery && battery < 60) {
  280. _battery = 2
  281. } else if (60 <= battery && battery < 80) {
  282. _battery = 3
  283. } else if (80 <= battery && battery <= 100) {
  284. _battery = 4
  285. } else if (battery > 100) {
  286. _battery = 5
  287. };
  288. return _battery
  289. },
  290. actionMusic(e) {
  291. var that = this;
  292. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  293. return;
  294. };
  295. var deviceList = that.getDeviceList();
  296. var deviceListSelect = that.getDeviceListSelect();
  297. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  298. wx.showToast({
  299. title: '请选择设备',
  300. icon: "none"
  301. });
  302. return;
  303. };
  304. that.setData({
  305. actionIndex: e.currentTarget.dataset.index,
  306. });
  307. var channelData = that.data.channelData;
  308. var index = e.currentTarget.dataset.index;
  309. var deviceId = deviceList[deviceListSelect].deviceId;
  310. const other = {
  311. "url": "",
  312. "media_data": "",
  313. "user_id": `${app.globalData.userInfo.deviceUid}`,
  314. "timestamp": `${Math.round(new Date() / 1000)}`,
  315. "channel_id": `${channelData[index].channelNum}`,
  316. "order": "",
  317. "resource_from": "",
  318. "songAlbumID": "",
  319. "version": 3,
  320. "is_debug": app.globalData.is_debug
  321. };
  322. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  323. app.PubMsg({
  324. type: "play",
  325. DstDeviceName: deviceMacId,
  326. other
  327. });
  328. },
  329. getchannelData(clientType) {
  330. var that = this;
  331. var deviceList = that.getDeviceList();
  332. var deviceListSelect = that.getDeviceListSelect();
  333. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  334. return;
  335. };
  336. var deviceId = deviceList[deviceListSelect].deviceId;
  337. listByDevice({
  338. clientType: clientType,
  339. unShowLoad: true,
  340. }).then((res) => {
  341. that.setData({
  342. channelData: res
  343. });
  344. // 接收设备当前播放状态
  345. var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  346. const obj = {
  347. DstDeviceName: deviceMacId
  348. }
  349. app.PubMsg({
  350. type: "get_position",
  351. ...obj
  352. });
  353. wx.setStorageSync("channelData", res);
  354. })
  355. },
  356. ///去频道详情
  357. onTapToChannel() {
  358. var that = this;
  359. if (that.data.channelData.length > that.data.actionIndex) {
  360. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  361. wx.navigateTo({
  362. url: './../channelDetails/channelDetails'
  363. });
  364. }
  365. },
  366. onTapIndex(e) {
  367. var that = this;
  368. var index = e.currentTarget.dataset.index;
  369. var indexPage = that.data.indexPage;
  370. if (indexPage != index) {
  371. that.setData({
  372. indexPage: index,
  373. });
  374. }
  375. },
  376. onTapLogin() {
  377. var that = this;
  378. ///退出登录
  379. var isLogin = that.getIsLogin();
  380. if (isLogin) {
  381. that.logOut();
  382. return;
  383. }
  384. route_util.jump(route_constant.login);
  385. },
  386. ///退出登录
  387. logOut() {
  388. var that = this;
  389. wx.removeStorageSync('userInfo');
  390. wx.removeStorageSync('token');
  391. var nickName = "未登录";
  392. var greeting = that.getGreetBuNickName(nickName);
  393. that.setData({
  394. isLogin: false,
  395. greeting: greeting,
  396. nickName: nickName,
  397. userPic: "./../../img/head_pic.png",
  398. });
  399. var deviceList = that.getDeviceList();
  400. var deviceListSelect = that.getDeviceListSelect();
  401. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  402. return;
  403. };
  404. ///有设备在线被选中,则让它不被选择
  405. if (deviceList[deviceListSelect].connectType == 3) {
  406. var deviceMacId = that.data.deviceMacId;
  407. if (!strings.isEmpty(deviceMacId)) {
  408. var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
  409. app.unsubscribe(pubResponse);
  410. that.setData({
  411. actionIndex: null,
  412. deviceListSelect: null,
  413. });
  414. };
  415. }
  416. },
  417. ///点击item
  418. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  419. onTapItem(e) {
  420. var that = this;
  421. var isLogin = that.getIsLogin();
  422. if (!isLogin) {
  423. route_util.jump(route_constant.login);
  424. return;
  425. }
  426. var index = e.currentTarget.dataset.index;
  427. var device = e.currentTarget.dataset.item;
  428. var connectType = device.connectType;
  429. var deviceListSelect = that.getDeviceListSelect();
  430. // wifi只支持在线点击
  431. if (connectType == 3 && device.state === "online") {
  432. if (index === deviceListSelect) {
  433. that.goWake();
  434. } else {
  435. that.addWifiDevice(device);
  436. }
  437. return;
  438. }
  439. ///去蓝牙连接处理
  440. if (index === deviceListSelect) {
  441. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  442. } else if (device.state === "offline") {
  443. console.log("去连接蓝牙")
  444. } else {
  445. // {"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 妙播收音机"}
  446. device.name = device.devName;
  447. BtHelper.getInstance().connect(device, function (data) {
  448. if (data) {
  449. that.addBlueDevice(device);
  450. }
  451. });
  452. }
  453. },
  454. // 去唤醒界面
  455. goWake() {
  456. var that = this;
  457. var deviceList = that.getDeviceList();
  458. var deviceListSelect = that.getDeviceListSelect();
  459. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  460. return
  461. };
  462. var device = deviceList[deviceListSelect];
  463. console.log("gadsfadsfadsfa==999===" + JSON.stringify(device));
  464. wx.navigateTo({
  465. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  466. });
  467. },
  468. ///删除当前设备
  469. deleteDevice(e) {
  470. var that = this;
  471. var index = e.currentTarget.dataset.index;
  472. wx.showModal({
  473. title: '确定删除?',
  474. success: function (res) {
  475. if (res.confirm) {
  476. var deviceList = that.getDeviceList();
  477. if (deviceList[index].connectType == 3) {
  478. that.cancelWifi(index, false);
  479. } else {
  480. that.cancelBlue(index, false);
  481. }
  482. }
  483. }
  484. });
  485. },
  486. ///是否是同一个蓝牙
  487. isTheSameBlue(connectDevice) {
  488. var that = this;
  489. var deviceList = that.getDeviceList();
  490. var deviceListSelect = that.getDeviceListSelect();
  491. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  492. return false;
  493. };
  494. if (deviceList[deviceListSelect].connectType == 3) {
  495. return false;
  496. } else {
  497. if (deviceList[deviceListSelect].deviceId == connectDevice.deviceId) {
  498. return true;
  499. } else {
  500. return false;
  501. }
  502. }
  503. },
  504. ///断开当前的
  505. async cancelCurrent() {
  506. var that = this;
  507. var deviceList = that.getDeviceList();
  508. var deviceListSelect = that.getDeviceListSelect();
  509. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  510. return;
  511. };
  512. if (deviceList[deviceListSelect].connectType == 3) {
  513. await that.cancelWifi(deviceListSelect, true);
  514. } else {
  515. await that.cancelBlue(deviceListSelect, true);
  516. }
  517. },
  518. /// 断开蓝牙连接
  519. async cancelBlue(index, onlyCancel) {
  520. var that = this;
  521. if (onlyCancel) {
  522. return;
  523. }
  524. var deviceList = that.getDeviceList();
  525. var deviceId = deviceList[index].deviceId;
  526. ///删除当前设备
  527. deviceList = deviceList.filter((item, i) => {
  528. return deviceId !== item.deviceId;
  529. });
  530. store.setStore("deviceList", deviceList);
  531. that.setData({
  532. deviceListSelect: null,
  533. deviceList: deviceList,
  534. });
  535. },
  536. /// 断开连接wifi
  537. async cancelWifi(index, onlyCancel) {
  538. var that = this;
  539. var deviceList = that.getDeviceList();
  540. if (index === null || deviceList.length <= index) {
  541. return;
  542. };
  543. var deviceId = deviceList[index].deviceId;
  544. // 取消订阅
  545. var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
  546. app.unsubscribe(pubResponse);
  547. if (onlyCancel) {
  548. return;
  549. }
  550. deviceList = deviceList.filter((item, i) => {
  551. return deviceId !== item.deviceId;
  552. });
  553. store.setStore("deviceList", deviceList);
  554. that.setData({
  555. deviceList: deviceList,
  556. deviceMacId: null,
  557. actionIndex: null,
  558. deviceListSelect: null,
  559. });
  560. },
  561. // 新添加Wifi设备
  562. addWifiDevice(device) {
  563. var that = this;
  564. var that = this;
  565. var isOK = app.globalData.client && app.globalData.client.connected;
  566. if (!isOK) {
  567. app.connect();
  568. setTimeout(() => {
  569. that.addWifiDevice(device);
  570. }, 500);
  571. return;
  572. };
  573. var deviceId = device.deviceId;
  574. var addDeviceList = lexin_add.addWifiDevice(device);
  575. that.updateDeviceList(addDeviceList, false, false);
  576. that.setData({
  577. deviceListSelect: null,
  578. connectDeviceIding: deviceId,
  579. });
  580. lexin_message.subscribeSingleDevice(deviceId);
  581. },
  582. refreshCurrentDevice(device) {
  583. var that = this;
  584. var deviceList = that.getDeviceList();
  585. var selectIndex = null;
  586. for (var i = 0; i < deviceList.length; i++) {
  587. if (device.deviceId == deviceList[i].deviceId) {
  588. selectIndex = i;
  589. break;
  590. }
  591. }
  592. if (selectIndex != null) {
  593. var addDeviceList = lexin_add.addWifiDevice(device);
  594. that.updateDeviceList(addDeviceList, false, false);
  595. that.setData({
  596. autoConnected: true,
  597. deviceListSelect: selectIndex,
  598. connectDeviceIding: "",
  599. });
  600. }
  601. },
  602. /// 新添加蓝牙设备
  603. addBlueDevice(device) {
  604. var that = this;
  605. var addDeviceList = lexin_add.addBlueDevice(device);
  606. that.updateDeviceList(addDeviceList, false, false);
  607. that.setData({
  608. deviceListSelect: 0,
  609. autoConnected: true,
  610. });
  611. },
  612. /// 手机关闭蓝牙,所有蓝牙设备离线
  613. closeBlueResetOffline(isInit, closeAllBlue) {
  614. var that = this;
  615. var deviceList = that.getDeviceList();
  616. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  617. },
  618. /// 更新列表排序
  619. updateDeviceList(deviceList, isInit, closeAllBlue) {
  620. var that = this;
  621. var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
  622. if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
  623. store.setStore("deviceList", finalList);
  624. that.setData({
  625. deviceList: finalList,
  626. });
  627. }
  628. },
  629. ///点击banner事件
  630. onTapBanner() {
  631. var that = this;
  632. var item = e.currentTarget.dataset.item;
  633. },
  634. ///添加设备
  635. jumpToAddDevice() {
  636. var that = this;
  637. var isLogin = that.getIsLogin();
  638. if (!isLogin) {
  639. route_util.jump(route_constant.login);
  640. return;
  641. }
  642. route_util.jump(route_constant.deviceList);
  643. },
  644. // 关于我们
  645. jumpToAboutUs() {
  646. lexin_jump.toAboutUs();
  647. },
  648. /// 是否已登录
  649. getIsLogin() {
  650. var that = this;
  651. var isLogin = that.data.isLogin;
  652. return isLogin;
  653. },
  654. /// 获取列表数据
  655. getDeviceList() {
  656. var that = this;
  657. var deviceList = that.data.deviceList;
  658. return deviceList;
  659. },
  660. ///获取当前选中的
  661. getDeviceListSelect() {
  662. var that = this;
  663. var deviceListSelect = that.data.deviceListSelect;
  664. return deviceListSelect;
  665. },
  666. ///获取是否已经第一次连接过
  667. getAutoConnected() {
  668. var that = this;
  669. var autoConnected = that.data.autoConnected;
  670. return autoConnected;
  671. },
  672. ///当前正在连接的设备id
  673. getConnectDeviceIding() {
  674. var that = this;
  675. var connectDeviceIding = that.data.connectDeviceIding
  676. return connectDeviceIding;
  677. },
  678. ///******************************* 可折叠 ********************************************///
  679. onUserInfoLoad() {
  680. var that = this;
  681. var userInfo = wx.getStorageSync("userInfo") || "";
  682. if (!strings.isEmpty(userInfo)) {
  683. var phone = userInfo.phone;
  684. if (!phone) {
  685. return;
  686. };
  687. var nickName = userInfo.nickname || "";
  688. var greeting = that.getGreetBuNickName(nickName);
  689. // 获取缓存的频道数据
  690. var channelData = wx.getStorageSync("channelData") || "";
  691. if (!strings.isEmpty(channelData)) {
  692. that.setData({
  693. channelData: channelData,
  694. greeting: greeting,
  695. nickName: nickName,
  696. userPic: userInfo.headUrl || "",
  697. isLogin: true,
  698. })
  699. } else {
  700. that.setData({
  701. greeting: greeting,
  702. nickName: nickName,
  703. userPic: userInfo.headUrl || "",
  704. isLogin: true,
  705. });
  706. }
  707. } else {
  708. var greeting = that.getGreetBuNickName("");
  709. that.setData({
  710. greeting: greeting,
  711. })
  712. }
  713. },
  714. getGreetBuNickName(nickName) {
  715. var greeting = timeUtil.getGreet();
  716. if (nickName != "" && nickName != "未登录") {
  717. greeting = greeting + ',' + nickName;;
  718. }
  719. return greeting;
  720. },
  721. onBannerLoad() {
  722. var that = this;
  723. // var bannerList = wx.getStorageSync("homeBanner") || [];
  724. // if (that.data.bannerList.length > 0) {
  725. // that.setData({
  726. // bannerList: bannerList
  727. // });
  728. // }
  729. },
  730. onBannerShow() {
  731. var that = this;
  732. // getBanner({}).then((res) => {
  733. // that.setData({
  734. // bannerList: res
  735. // });
  736. // wx.setStorageSync("homeBanner", res);
  737. // })
  738. },
  739. stopIntervalId1: function () {
  740. var that = this;
  741. if (!strings.isEmpty(that.data.intervalId1)) {
  742. clearInterval(that.data.intervalId1);
  743. that.data.intervalId1 = null;
  744. }
  745. },
  746. stopIntervalId2: function () {
  747. var that = this;
  748. if (!strings.isEmpty(that.data.intervalId2)) {
  749. clearInterval(that.data.intervalId2);
  750. that.data.intervalId2 = null;
  751. }
  752. },
  753. ///销毁蓝牙
  754. onUnload() {
  755. var that = this;
  756. that.stopIntervalId1();
  757. that.stopIntervalId2();
  758. if (!strings.isEmpty(that.data.intervalId)) {
  759. clearInterval(that.data.intervalId);
  760. that.data.intervalId = null;
  761. }
  762. BtHelper.getInstance().disconnect();
  763. if (app.globalData.client === null) {
  764. return;
  765. };
  766. app.globalData.client.end(true);
  767. app.globalData.client.end(true);
  768. app.globalData.client = null;
  769. },
  770. })