index.js 29 KB

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