index.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. const constant = require('../../../utils/constant.js');
  2. import {
  3. BtHelper
  4. } from '../../../devices/bt_helper.js';
  5. import {
  6. EnumCmdEvent,
  7. CmdEvent
  8. } from '../../../devices/cmd_key_event';
  9. Page({
  10. data: {
  11. navBarHeight: constant.app.globalData.navBarHeight,
  12. safeBottomHeight: constant.app.globalData.safeBottomHeight,
  13. bannerList: constant.app.globalData.bannerList,
  14. nvabarData: {
  15. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  16. title: 'OhPlay', //导航栏 中间的标题
  17. },
  18. ///下午好
  19. isLogin: false,
  20. greeting: "",
  21. // bannerList: [{
  22. // "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  23. // },
  24. // {
  25. // "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  26. // }
  27. // ],
  28. autoplay: true,
  29. interval: 3000, // 切换时间间隔
  30. duration: 500, // 滑动动画时长
  31. circular: true, // 衔接滑动
  32. indexPage: 0,
  33. ///是否展示频道
  34. showChannel: false,
  35. ////首页
  36. navBarHeight: constant.app.globalData.navBarHeight,
  37. MenuButtonheight: constant.app.globalData.MenuButtonheight,
  38. MenuButtonTop: constant.app.globalData.MenuButtonTop,
  39. actionIndex: null,
  40. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  41. channelData: [],
  42. isOneLoading: true,
  43. uid: null,
  44. isSetWake: false,
  45. battery: 4, // 0≤电量<20,0格
  46. ////我的界面
  47. loginStatus: true,
  48. nickName: "未登录",
  49. userPic: './../../../img/head_pic.png',
  50. intervalId: null,
  51. /// 是否第一次自动连接过
  52. autoConnected: false,
  53. intervalId1: null,
  54. intervalId2: null,
  55. deviceMacId: null,
  56. deviceListSelect: null,
  57. deviceList: [],
  58. // deviceList: [{
  59. // "connectType": 3,
  60. // // "mac": "AIrSMArT_861210052356337",
  61. // // "deviceId": "D0:31:10:86:AC:9A"
  62. // "name": "MW-X4(BZK)",
  63. // "deviceId": "AIrSMArT_861210052356337"
  64. // }],
  65. _willConnectBle: null,
  66. _hasShowModal: null,
  67. _clickOtherDevice: false,
  68. // deviceList: [{
  69. // "connectType": 3,
  70. // "devName": "猫王小王子OTR-X",
  71. // "state": "online",
  72. // "name": "BLUFI_7cdfa1fcbb24",
  73. // "deviceId": "BLUFI_7cdfa1fcbb24",
  74. // "mac": "BLUFI_7cdfa1fcbb24",
  75. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  76. // "ProdModel": "MW-2AX(WIFI-N)",
  77. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  78. // }, {
  79. // "connectType": 3,
  80. // "devName": "猫王小王子OTR-X",
  81. // "state": "online",
  82. // "name": "BLUFI_7cdfa1fcbb24",
  83. // "deviceId": "BLUFI_7cdfa1fcbb24",
  84. // "mac": "BLUFI_7cdfa1fcbb24",
  85. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  86. // "ProdModel": "MW-2AX(WIFI-N)",
  87. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  88. // }, {
  89. // "connectType": 3,
  90. // "devName": "猫王小王子OTR-X",
  91. // "state": "online",
  92. // "name": "BLUFI_7cdfa1fcbb24",
  93. // "deviceId": "BLUFI_7cdfa1fcbb24",
  94. // "mac": "BLUFI_7cdfa1fcbb24",
  95. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  96. // "ProdModel": "MW-2AX(WIFI-N)",
  97. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  98. // }, {
  99. // "connectType": 3,
  100. // "devName": "猫王小王子OTR-X",
  101. // "state": "online",
  102. // "name": "BLUFI_7cdfa1fcbb24",
  103. // "deviceId": "BLUFI_7cdfa1fcbb24",
  104. // "mac": "BLUFI_7cdfa1fcbb24",
  105. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  106. // "ProdModel": "MW-2AX(WIFI-N)",
  107. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  108. // }, {
  109. // "connectType": 3,
  110. // "devName": "猫王小王子OTR-X",
  111. // "state": "online",
  112. // "name": "BLUFI_7cdfa1fcbb24",
  113. // "deviceId": "BLUFI_7cdfa1fcbb24",
  114. // "mac": "BLUFI_7cdfa1fcbb24",
  115. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  116. // "ProdModel": "MW-2AX(WIFI-N)",
  117. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  118. // }, {
  119. // "connectType": 3,
  120. // "devName": "猫王小王子OTR-X",
  121. // "state": "online",
  122. // "name": "BLUFI_7cdfa1fcbb24",
  123. // "deviceId": "BLUFI_7cdfa1fcbb24",
  124. // "mac": "BLUFI_7cdfa1fcbb24",
  125. // "imageUrl": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100319641863705.png",
  126. // "ProdModel": "MW-2AX(WIFI-N)",
  127. // "offlineImg": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230310100323674404211.png"
  128. // }],
  129. connectDeviceIding: "",
  130. },
  131. onLoad(options) {
  132. var that = this;
  133. that.onLoginLoad();
  134. that.onDeviceLoad();
  135. that.onUserInfoLoad();
  136. that.onBannerLoad();
  137. // var device = {
  138. // "mac": "AIrSMArT_861210052356337",
  139. // "deviceId": "D0:31:10:86:AC:9A"
  140. // };
  141. // that.data.deviceList.unshift(device);
  142. // that.data.deviceMacId = device.mac;
  143. // that.actionDevice();
  144. },
  145. onShow() {},
  146. onLoginLoad() {
  147. var that = this;
  148. var userInfo = wx.getStorageSync("userInfo") || "";
  149. if (!constant.strings.isEmpty(userInfo)) {
  150. var phone = userInfo.phone;
  151. if (!constant.strings.isEmpty(phone)) {
  152. that.loginReset(phone, null, null, null);
  153. };
  154. }
  155. },
  156. ///重新登录
  157. loginReset(phone, succeedCall1, succeedCall2, failCall) {
  158. // {"openid":"oZHPJ5JcNem2och3ToaocggmUENw","userId":933625,"deviceUid":1,"accessToken":"PUK8WVnLbIFsWYjSteitmg3OhllpbUMvIPKUtfJgwhRtpl1jx4Fm1NkHQIiZYhHz","phone":"15915390284","headUrl":"https://music-play.oss-cn-shenzhen.aliyuncs.com/user/headurl/20220316194115703366576.png","nickname":"儿子的爸爸叫啥"}
  159. var that = this;
  160. wx.login({
  161. success: res0 => {
  162. // {"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}
  163. constant.apiUtil.login({
  164. code: res0.code,
  165. phone: phone,
  166. }).then((res1) => {
  167. constant.app.globalData.userInfo = res1;
  168. constant.store.setStore("userInfo", res1);
  169. constant.store.setStore("token", JSON.stringify({
  170. id: res1.userId,
  171. token: res1.accessToken,
  172. }));
  173. that.onUserInfoLoad();
  174. if (succeedCall1 != null) {
  175. succeedCall1();
  176. }
  177. if (succeedCall2 != null) {
  178. succeedCall2();
  179. }
  180. });
  181. },
  182. fail(fail4) {
  183. if (failCall != null) {
  184. failCall();
  185. }
  186. },
  187. });
  188. },
  189. testData() {
  190. return {
  191. "deviceId": "xxxxx",
  192. "deviceName": "xxxxxx",
  193. "deviceType": "xxxx",
  194. "type": 1,
  195. "deviceIcon": "xxxx",
  196. "deviceMacId": "xxxx",
  197. "deviceBattery": 4,
  198. "state": "online",
  199. "connectType": 1,
  200. "clientType": "猫王妙播黑胶音箱(BLE)",
  201. "deviceConnectStatus": 1,
  202. "deviceConnectStatusText": "xxxx",
  203. }
  204. },
  205. onDeviceLoad() {
  206. var that = this;
  207. var deviceList = constant.store.getStore("deviceList");
  208. if (!constant.strings.isEmpty(deviceList)) {
  209. // deviceList = deviceList.concat(deviceList);
  210. that.updateDeviceList(deviceList, true, false);
  211. }
  212. // that.addBlueDevice(that.testData())
  213. let hasBle = that.data.deviceList.find(item => {
  214. return item.connectType == 1;
  215. })
  216. if (hasBle) {
  217. ///监听蓝牙设备
  218. BtHelper.getInstance().initBluetoothAdapter();
  219. BtHelper.getInstance().getBluetoothDevices();
  220. BtHelper.getInstance().startScan(null, null, function (res) {});
  221. var isFirst = true;
  222. console.log("开始监听蓝牙设备");
  223. that.startBleTimer(isFirst);
  224. that.addNotification()
  225. }
  226. },
  227. startBleTimer(isFirst) {
  228. let that = this;
  229. let deviceList = that.getDeviceList()
  230. let findWifi = deviceList.find(item => {
  231. return item.connectType != 1
  232. })
  233. if (findWifi) {
  234. return;
  235. }
  236. ///再秒再对比一次
  237. that.stopIntervalId1();
  238. let count = 0;
  239. that.data.intervalId1 = setInterval(async function () {
  240. console.log("开始扫描蓝牙设备");
  241. that.compareList();
  242. // if (count > 4) {
  243. if (isFirst) {
  244. that.stopIntervalId1();
  245. var newIsFirst = false
  246. that.startBleTimer(newIsFirst);
  247. }
  248. // } else {
  249. // count++;
  250. // }
  251. isFirst = false;
  252. }, isFirst ? 3 * 1000 : 10 * 1000);
  253. },
  254. /// 对比蓝牙数据
  255. compareList() {
  256. let that = this;
  257. constant.lexinScan.compareList2(that.data.deviceList, function (device) {
  258. if (device) {
  259. that.data._willConnectBle = device;
  260. getApp().getBluetoothStatus();
  261. }
  262. });
  263. // constant.lexinScan.compareList(function (list) {
  264. // console.log("对比蓝牙数据", list);
  265. // that.updateDeviceList(list, false, false);
  266. // }, function (item) {
  267. // that.addBlueDevice(item);
  268. // });
  269. },
  270. getBluetoothStatusCallck(v) {
  271. var that = this;
  272. if (!that.data._willConnectBle) {
  273. // true 走下面
  274. return;
  275. }
  276. if (v) {
  277. var device = that.data._willConnectBle;
  278. console.log("开始连接设备0", device);
  279. // if (device.state === 'online' || device.connectType != 1) {
  280. // return;
  281. // }
  282. BtHelper.getInstance().connect(device, function (isConnected, d) {
  283. console.log("自动连接成功", isConnected)
  284. if (isConnected) {
  285. device.state = 'online';
  286. that.addBlueDevice(device);
  287. that.stopIntervalId1();
  288. } else {
  289. device.state = 'offline';
  290. }
  291. });
  292. } else {
  293. // wx.showModal({
  294. // title: '温馨提示',
  295. // content: '请检查手机蓝牙是否打开',
  296. // showCancel: false,
  297. // success: function (res) { }
  298. // });
  299. }
  300. that.data._willConnectBle = null;
  301. },
  302. // 回调
  303. mqttCallback(type, option) {
  304. // console.log("gadsfadsfadsfa==999===" + type + "====" + JSON.stringify(option));
  305. var that = this;
  306. let payloads = null;
  307. try {
  308. if (option && option.payload) {
  309. payloads = JSON.parse(option.payload);
  310. };
  311. switch (type) {
  312. ///先走订阅
  313. case "connect":
  314. constant.lexinSubscribe.subscribeAllDevice();
  315. break;
  316. ///再走有在线
  317. case "message_onoffline":
  318. constant.lexinConnect.searchOnlineDevice(payloads, function (list) {
  319. that.updateDeviceList(list, false, false);
  320. }, function () {
  321. var indexPage = that.data.indexPage;
  322. that.setData({
  323. actionIndex: null,
  324. deviceListSelect: null,
  325. indexPage: indexPage == 1 ? 0 : indexPage,
  326. });
  327. }, function () {
  328. var indexPage = that.data.indexPage;
  329. that.setData({
  330. actionIndex: null,
  331. deviceListSelect: null,
  332. indexPage: indexPage == 1 ? 0 : indexPage,
  333. });
  334. });
  335. break;
  336. case "message":
  337. if (payloads) {
  338. var payloadType = payloads.type;
  339. var other = payloads.other;
  340. ///获取频道位置
  341. if (payloadType === "get_position" && other) {
  342. constant.lexinMessage.getPosition(other, function (actionIndex) {
  343. that.setData({
  344. actionIndex: actionIndex,
  345. });
  346. });
  347. }
  348. // 获取歌曲信息
  349. else if (payloadType === "get_dev_info" && other) {
  350. that.setDeviceInfor(other);
  351. constant.lexinMessage.getDeviceInfo(payloads);
  352. }
  353. ///获取电量
  354. else if (payloadType === "battery" && other) {
  355. if (other.battery) {
  356. var battery = constant.lexinMessage.getBattery(other.battery);
  357. that.setData({
  358. battery: battery,
  359. });
  360. }
  361. }
  362. ///获取播放状态
  363. else if (payloadType === "play" || payloadType === "play_state") {
  364. constant.lexinMessage.getPlay();
  365. }
  366. }
  367. break;
  368. default:
  369. }
  370. } catch (e) {}
  371. },
  372. ///去连接设备数据
  373. actionDevice(device) {
  374. var that = this;
  375. // 取消订阅
  376. var deviceMacId = that.data.deviceMacId;
  377. if (!constant.strings.isEmpty(deviceMacId)) {
  378. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  379. constant.app.unsubscribe(pubResponse);
  380. };
  381. that.subscribeCurrDevice(device);
  382. },
  383. ///数据处理
  384. subscribeCurrDevice(device) {
  385. var that = this;
  386. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  387. if (!isOK) {
  388. constant.app.connect();
  389. setTimeout(() => {
  390. that.subscribeCurrDevice(device);
  391. }, 500);
  392. return;
  393. };
  394. var deviceId = device.deviceId;
  395. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  396. that.setData({
  397. deviceMacId: deviceMacId,
  398. });
  399. /// 订阅设备
  400. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  401. constant.app.subscribe(pubResponse);
  402. /// 获取设备信息
  403. const obj = {
  404. DstDeviceName: deviceMacId
  405. };
  406. constant.app.PubMsg({
  407. type: "get_dev_info",
  408. ...obj
  409. });
  410. },
  411. ///选择对应的频道
  412. onTapChannelIndex(e) {
  413. var that = this;
  414. var index = e.currentTarget.dataset.index;
  415. constant.lexinJump.toChannelIndex(index, function (i) {
  416. that.setData({
  417. actionIndex: i,
  418. });
  419. });
  420. },
  421. ///去频道详情
  422. onTapToChannel() {
  423. var that = this;
  424. var actionIndex = that.data.actionIndex;
  425. if (actionIndex == null) {
  426. return;
  427. }
  428. var channelData = that.getChannelData();
  429. if (channelData.length > actionIndex) {
  430. constant.store.setStore("channelDeta", channelData[actionIndex]);
  431. constant.routeUtil.jump(constant.routePath.channelDetail);
  432. }
  433. },
  434. ///选择对应的频道
  435. getchannelData(clientType) {
  436. var that = this;
  437. constant.apiUtil.listByDevice({
  438. clientType: clientType,
  439. unShowLoad: true,
  440. }).then((res) => {
  441. constant.store.setStore("channelData", res);
  442. that.setData({
  443. channelData: res,
  444. });
  445. // 接收设备当前播放状态
  446. var deviceList = that.getDeviceList();
  447. var deviceListSelect = that.getDeviceListSelect();
  448. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  449. return;
  450. };
  451. var deviceId = deviceList[deviceListSelect].deviceId;
  452. var deviceMacId = constant.lexinUtil.getDeviceMacId(deviceId);
  453. const obj = {
  454. DstDeviceName: deviceMacId
  455. }
  456. constant.app.PubMsg({
  457. type: "get_position",
  458. ...obj
  459. });
  460. })
  461. },
  462. onTapIndex(e) {
  463. var that = this;
  464. var index = e.currentTarget.dataset.index;
  465. var indexPage = that.data.indexPage;
  466. if (indexPage != index) {
  467. that.setData({
  468. indexPage: index,
  469. });
  470. }
  471. },
  472. onTapLogin() {
  473. var that = this;
  474. ///退出登录
  475. var isLogin = that.getIsLogin();
  476. if (isLogin) {
  477. that.logOut();
  478. return;
  479. }
  480. constant.routeUtil.jump(constant.routePath.login);
  481. },
  482. ///退出登录
  483. logOut() {
  484. var that = this;
  485. wx.removeStorageSync('userInfo');
  486. wx.removeStorageSync('token');
  487. var nickName = "未登录";
  488. var greeting = that.getGreetBuNickName(nickName);
  489. that.setData({
  490. isLogin: false,
  491. greeting: greeting,
  492. nickName: nickName,
  493. userPic: "./../../../img/head_pic.png",
  494. });
  495. var device = that.getCurrentDevice();
  496. if (device == null) {
  497. return;
  498. }
  499. ///有设备在线被选中,则让它不被选择
  500. if (device.connectType == 3) {
  501. var deviceMacId = that.data.deviceMacId;
  502. if (!constant.strings.isEmpty(deviceMacId)) {
  503. var pubResponse = constant.lexinUtil.getResponseByDeviceMacId(deviceMacId);
  504. constant.app.unsubscribe(pubResponse);
  505. var indexPage = that.data.indexPage;
  506. that.setData({
  507. deviceMacId: null,
  508. actionIndex: null,
  509. deviceListSelect: null,
  510. indexPage: indexPage == 1 ? 0 : indexPage,
  511. });
  512. };
  513. } else {
  514. // console.log("退出登录,断开连接", deviceList);
  515. // BtHelper.getInstance().disconnect(deviceList[deviceListSelect]);
  516. // var indexPage = that.data.indexPage;
  517. // that.setData({
  518. // deviceMacId: null,
  519. // actionIndex: null,
  520. // deviceListSelect: null,
  521. // indexPage: indexPage == 1 ? 0 : indexPage,
  522. // });
  523. }
  524. },
  525. ///点击item
  526. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  527. async onTapItem(e) {
  528. var that = this;
  529. var isLogin = that.getIsLogin();
  530. if (!isLogin) {
  531. constant.routeUtil.jump(constant.routePath.login);
  532. return;
  533. }
  534. var index = e.currentTarget.dataset.index;
  535. var device = e.currentTarget.dataset.item;
  536. var connectType = device.connectType;
  537. var deviceListSelect = that.getDeviceListSelect();
  538. // wifi只支持在线点击
  539. if (connectType == 3 && device.state === "online") {
  540. if (index === deviceListSelect) {
  541. that.goWake();
  542. } else {
  543. // 现断线第一个
  544. // that.disconnectFirst()
  545. that.addWifiDevice(device);
  546. }
  547. return;
  548. }
  549. ///去蓝牙连接处理
  550. // {"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 妙播收音机"}
  551. var deviceList = that.getDeviceList();
  552. let curDevice = deviceList[index]
  553. if (index === deviceListSelect && device.state === "online") {
  554. var param = "?param=" + JSON.stringify(curDevice);
  555. constant.routeUtil.jumpParam(constant.routePath.deviceDetail, param);
  556. } else if (device.state === "offline") {
  557. var deviceListSelect = that.getDeviceListSelect();
  558. if (deviceListSelect != null) {
  559. if (deviceList.length > deviceListSelect) {
  560. var currentDevice = deviceList[deviceListSelect];
  561. console.log("断开连接0", currentDevice)
  562. // 现断线第一个
  563. // that.disconnectFirst()
  564. }
  565. that.setData({
  566. deviceListSelect: null,
  567. });
  568. }
  569. console.log("断开连接2", device)
  570. device.name = device.devName;
  571. that.data._clickOtherDevice = true;
  572. BtHelper.getInstance().connect(device, function (isConnected, d) {
  573. that.data._clickOtherDevice = false;
  574. if (isConnected) {
  575. device.state = "online";
  576. that.addBlueDevice(device);
  577. }
  578. });
  579. } else {
  580. var deviceList = that.getDeviceList();
  581. var deviceListSelect = that.getDeviceListSelect();
  582. if (deviceListSelect != null) {
  583. if (deviceList.length > deviceListSelect) {
  584. var currentDevice = deviceList[deviceListSelect];
  585. if (currentDevice.connectType === 1) {
  586. console.log("断开连接1", currentDevice)
  587. await BtHelper.getInstance().disconnect(currentDevice);
  588. }
  589. // await time_util.delayMills(500);
  590. }
  591. that.setData({
  592. deviceListSelect: null,
  593. });
  594. }
  595. that.data._clickOtherDevice = true;
  596. device.name = device.devName;
  597. BtHelper.getInstance().connect(device, function (isConnected, d) {
  598. that.data._clickOtherDevice = false;
  599. if (isConnected) {
  600. device.state = "online";
  601. that.addBlueDevice(device);
  602. }
  603. });
  604. }
  605. },
  606. disconnectFirst() {
  607. let that = this;
  608. let deviceList = that.getDeviceList()
  609. let first = deviceList[0] ?? {}
  610. if (first.state == "online") {
  611. if (first.connectType === 1) {
  612. BtHelper.getInstance().disconnect(first)
  613. first.state == "offline"
  614. } else {
  615. // 取消订阅
  616. // var pubResponse = constant.lexinUtil.getResponseByDeviceId(first.deviceId);
  617. //constant.app.unsubscribe(pubResponse);
  618. }
  619. }
  620. },
  621. // 去唤醒界面
  622. goWake() {
  623. var that = this;
  624. var deviceList = that.getDeviceList();
  625. var deviceListSelect = that.getDeviceListSelect();
  626. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  627. return
  628. };
  629. var device = deviceList[deviceListSelect];
  630. var param = '?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel;
  631. constant.routeUtil.jumpParam(constant.routePath.deviceWake, param);
  632. },
  633. ///删除当前设备
  634. deleteDevice(e) {
  635. var that = this;
  636. var index = e.currentTarget.dataset.index;
  637. wx.showModal({
  638. title: '确定删除?',
  639. success: function (res) {
  640. if (res.confirm) {
  641. var deviceList = that.getDeviceList();
  642. if (deviceList[index].connectType == 3) {
  643. that.cancelWifi(index, false);
  644. } else {
  645. console.log("确定删除0", index, deviceList[index].deviceId)
  646. that.cancelBlue(index, false);
  647. }
  648. }
  649. }
  650. });
  651. },
  652. ///是否是同一个蓝牙
  653. isTheSameBlue(device) {
  654. var that = this;
  655. var deviceList = that.getDeviceList();
  656. var deviceListSelect = that.getDeviceListSelect();
  657. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  658. return false;
  659. };
  660. if (deviceList[deviceListSelect].connectType == 3) {
  661. return false;
  662. } else {
  663. if (deviceList[deviceListSelect].deviceId == device.deviceId) {
  664. return true;
  665. } else {
  666. return false;
  667. }
  668. }
  669. },
  670. ///断开当前的
  671. cancelCurrent() {
  672. var that = this;
  673. var deviceList = that.getDeviceList();
  674. var deviceListSelect = that.getDeviceListSelect();
  675. if (deviceList[deviceListSelect].connectType == 3) {
  676. that.cancelWifi(deviceListSelect, true);
  677. } else {
  678. that.cancelBlue(deviceListSelect, true);
  679. }
  680. },
  681. /// 断开蓝牙连接
  682. cancelBlue(index, onlyCancel) {
  683. var that = this;
  684. if (onlyCancel) {
  685. return;
  686. }
  687. var deviceList = that.getDeviceList();
  688. var deviceListSelect = that.getDeviceListSelect();
  689. ///是否删除的当前播放这个
  690. var isCurrentIndex = false;
  691. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  692. isCurrentIndex = (deviceListSelect == index);
  693. };
  694. let device = deviceList[index];
  695. ///删除当前设备
  696. var deviceId = device.deviceId;
  697. deviceList = deviceList.filter((item, i) => {
  698. return deviceId !== item.deviceId;
  699. });
  700. constant.store.setStore("deviceList", deviceList);
  701. console.log("确定删除1", isCurrentIndex, deviceListSelect)
  702. var indexPage = that.data.indexPage;
  703. that.setData({
  704. deviceList: deviceList,
  705. indexPage: indexPage == 1 ? 0 : indexPage,
  706. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  707. });
  708. console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
  709. if (device.state === "online") {
  710. BtHelper.getInstance().disconnect({
  711. "deviceId": deviceId
  712. });
  713. }
  714. },
  715. /// 断开连接wifi
  716. cancelWifi(index, onlyCancel) {
  717. var that = this;
  718. var deviceList = that.getDeviceList();
  719. if (index === null || deviceList.length <= index) {
  720. return;
  721. };
  722. var deviceId = deviceList[index].deviceId;
  723. // 取消订阅
  724. var pubResponse = constant.lexinUtil.getResponseByDeviceId(deviceId);
  725. constant.app.unsubscribe(pubResponse);
  726. if (onlyCancel) {
  727. return;
  728. }
  729. ///是否删除的当前播放这个
  730. var isCurrentIndex = false;
  731. var deviceListSelect = that.getDeviceListSelect();
  732. if (deviceListSelect != null && deviceList.length > deviceListSelect) {
  733. isCurrentIndex = (deviceListSelect == index);
  734. };
  735. deviceList = deviceList.filter((item, i) => {
  736. return deviceId !== item.deviceId;
  737. });
  738. constant.store.setStore("deviceList", deviceList);
  739. var indexPage = that.data.indexPage;
  740. var deviceMacId = that.data.deviceMacId;
  741. var actionIndex = that.data.actionIndex;
  742. that.setData({
  743. deviceList: deviceList,
  744. deviceMacId: isCurrentIndex ? null : deviceMacId,
  745. actionIndex: isCurrentIndex ? null : actionIndex,
  746. deviceListSelect: isCurrentIndex ? null : deviceListSelect,
  747. indexPage: indexPage == 1 ? 0 : indexPage,
  748. });
  749. },
  750. /// 新添加蓝牙设备
  751. addBlueDevice(device) {
  752. var that = this;
  753. var addDeviceList = constant.lexinAdd.addBlueDevice(device);
  754. let hasOnline = 0
  755. addDeviceList.forEach(item => {
  756. // 蓝牙设备下线。wifi不动
  757. if (item.deviceId != device.deviceId && item.connectType === 1) {
  758. item.state = "offline"
  759. }
  760. })
  761. console.log("新添加蓝牙设备", addDeviceList)
  762. that.updateDeviceList(addDeviceList, false, false);
  763. that.setData({
  764. deviceListSelect: 0,
  765. autoConnected: true,
  766. });
  767. that.addNotification()
  768. },
  769. disconnectDev(event, device) {
  770. let _this = this;
  771. if (_this.data._clickOtherDevice) {
  772. _this.data._clickOtherDevice = false
  773. return;
  774. }
  775. console.log("断开连接--", event.commonValue, event.deviceId);
  776. if (event.deviceId === device.deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
  777. _this.data._hasShowModal = true
  778. wx.showModal({
  779. title: '提示',
  780. content: '设备断开连接,请重新连接设备',
  781. showCancel: false,
  782. success: function (res) {
  783. _this.data._hasShowModal = false;
  784. if (res.confirm) {
  785. if (getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback) {
  786. getCurrentPages()[getCurrentPages().length - 1].disconnectDevCallback();
  787. }
  788. }
  789. }
  790. })
  791. }
  792. // 设备断开连接,请重新连接设备
  793. },
  794. addNotification() {
  795. let that = this;
  796. constant.eventBus.removeNotification(CmdEvent.eventName, that);
  797. constant.eventBus.addNotification(CmdEvent.eventName, function (event) {
  798. let deviceList = that.getDeviceList()
  799. if (event.cmdEvent == EnumCmdEvent.btMac) {
  800. let disDevice = deviceList.find(item => {
  801. return item.state == "online" && item.connectType === 1
  802. })
  803. if (disDevice) {
  804. disDevice.mac = event.btMac
  805. console.log("设备mac:", disDevice.mac)
  806. }
  807. } else if (event.cmdEvent == EnumCmdEvent.onoffline) {
  808. let isFirst = false;
  809. // 有下线就开启监听扫描
  810. if (event.commonValue == "online") {
  811. console.log("有蓝牙设备上线")
  812. that.stopIntervalId1();
  813. } else {
  814. // 有wifi设备在线
  815. let wifiDevice = deviceList.find(item => {
  816. return item.state == "online" && item.connectType === 3
  817. })
  818. if (!wifiDevice) {
  819. that.startBleTimer(isFirst);
  820. }
  821. }
  822. console.log("首页ble通知:", event.deviceId, event.commonValue)
  823. let hasOnleBle = deviceList.find(item => {
  824. return item.deviceId == event.deviceId && item.connectType == 1 && item.state == "online"
  825. })
  826. let disDevice = deviceList.find(item => {
  827. return item.deviceId == event.deviceId && item.connectType == 1
  828. })
  829. // that.data.deviceList.forEach(element => {
  830. // if (item.deviceId == tmp.deviceId) {
  831. // }
  832. // });
  833. if (!disDevice) {
  834. console.log("没有找到在线的对应的蓝牙设备")
  835. return;
  836. }
  837. disDevice.state = event.commonValue;
  838. console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
  839. if (event.commonValue == "online") {
  840. // BtHelper.getInstance().connect()
  841. console.log("首页连接上线", disDevice)
  842. } else {
  843. if (hasOnleBle) {
  844. that.disconnectDev(event, disDevice)
  845. }
  846. console.log("首页连接断开", disDevice)
  847. that.updateDeviceList(deviceList, false, false);
  848. }
  849. }
  850. }, that);
  851. },
  852. // 新添加Wifi设备
  853. addWifiDevice(device) {
  854. var that = this;
  855. var isOK = constant.app.globalData.client && constant.app.globalData.client.connected;
  856. if (!isOK) {
  857. constant.app.connect();
  858. setTimeout(() => {
  859. that.addWifiDevice(device);
  860. }, 500);
  861. return;
  862. };
  863. console.log("有新的wifi设备0", device)
  864. var deviceId = device.deviceId;
  865. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  866. console.log("有新的wifi设备", addDeviceList)
  867. that.updateDeviceList(addDeviceList, false, false);
  868. var indexPage = that.data.indexPage;
  869. that.setData({
  870. deviceListSelect: null,
  871. connectDeviceIding: deviceId,
  872. indexPage: indexPage == 1 ? 0 : indexPage,
  873. });
  874. constant.lexinSubscribe.subscribeSingleDevice(device);
  875. },
  876. refreshCurrentDevice(device) {
  877. var that = this;
  878. console.log("刷新列表0:", device)
  879. var addDeviceList = constant.lexinAdd.addWifiDevice(device);
  880. console.log("刷新列表1:", addDeviceList)
  881. that.updateDeviceList(addDeviceList, false, false);
  882. that.setData({
  883. autoConnected: true,
  884. deviceListSelect: 0,
  885. connectDeviceIding: "",
  886. });
  887. },
  888. /// 手机关闭蓝牙,所有蓝牙设备离线
  889. closeBlueResetOffline(isInit, closeAllBlue) {
  890. var that = this;
  891. var deviceList = that.getDeviceList();
  892. that.updateDeviceList(deviceList, isInit, closeAllBlue);
  893. },
  894. /// 更新列表排序
  895. updateDeviceList(deviceList, isInit, closeAllBlue) {
  896. var that = this;
  897. var finalList = constant.lexinAdd.updateDeviceList(deviceList, isInit, closeAllBlue);
  898. constant.store.setStore("deviceList", finalList);
  899. that.setData({
  900. deviceList: finalList,
  901. });
  902. },
  903. ///点击banner事件
  904. onTapBanner(e) {
  905. var item = e.currentTarget.dataset.item;
  906. try {
  907. if (constant.strings.isEmpty(item.forwardUrl)) {
  908. return;
  909. }
  910. // 内链,暂时不支持
  911. if (item.forwardType == 0) {
  912. constant.routeUtil.jump(item.forwardUrl, item.forwardUrl);
  913. }
  914. // 外链
  915. else if (item.forwardType == 1) {
  916. var param = "?param=" + item.forwardUrl;
  917. constant.routeUtil.jumpParam(constant.routePath.webview, param);
  918. } else {
  919. }
  920. } catch (err) {
  921. console.log("跳转失败", err)
  922. }
  923. },
  924. ///去充值内容会员
  925. toRecharge(e) {
  926. var device = e.currentTarget.dataset.item;
  927. },
  928. ///添加设备
  929. jumpToAddDevice() {
  930. var that = this;
  931. var isLogin = that.getIsLogin();
  932. if (!isLogin) {
  933. var param="?toDeviceList="+true;
  934. constant.routeUtil.jumpParam(constant.routePath.login,param);
  935. return;
  936. }
  937. constant.routeUtil.jump(constant.routePath.deviceList);
  938. },
  939. // 关于我们
  940. jumpToAboutUs() {
  941. constant.lexinJump.toAboutUs();
  942. },
  943. /// 是否已登录
  944. getIsLogin() {
  945. var that = this;
  946. var isLogin = that.data.isLogin;
  947. return isLogin;
  948. },
  949. /// 获取列表数据
  950. getDeviceList() {
  951. var that = this;
  952. var deviceList = that.data.deviceList;
  953. return deviceList;
  954. },
  955. ///获取当前选中的
  956. getDeviceListSelect() {
  957. var that = this;
  958. var deviceListSelect = that.data.deviceListSelect;
  959. return deviceListSelect;
  960. },
  961. ///获取是否已经第一次连接过
  962. getAutoConnected() {
  963. var that = this;
  964. var autoConnected = that.data.autoConnected;
  965. return autoConnected;
  966. },
  967. ///当前正在连接的设备id
  968. getConnectDeviceIding() {
  969. var that = this;
  970. var connectDeviceIding = that.data.connectDeviceIding
  971. return connectDeviceIding;
  972. },
  973. ///获取频道数据
  974. getChannelData() {
  975. var that = this;
  976. var channelData = that.data.channelData;
  977. return channelData;
  978. },
  979. /// 当前播放频道
  980. getActionIndex() {
  981. var that = this;
  982. var actionIndex = that.data.actionIndex;
  983. return actionIndex;
  984. },
  985. /// 获取当前的device
  986. getCurrentDevice() {
  987. var that = this;
  988. var deviceList = that.getDeviceList();
  989. var deviceListSelect = that.getDeviceListSelect();
  990. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  991. return null;
  992. };
  993. var device = deviceList[deviceListSelect];
  994. return device;
  995. },
  996. /// 获取当前的deviceId
  997. getCurrentDeviceId() {
  998. var that = this;
  999. var deviceList = that.getDeviceList();
  1000. var deviceListSelect = that.getDeviceListSelect();
  1001. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  1002. return "";
  1003. };
  1004. var deviceId = deviceList[deviceListSelect].deviceId;
  1005. return deviceId;
  1006. },
  1007. ///设置设备相关信息
  1008. setDeviceInfor(other) {
  1009. var that = this;
  1010. // 接收设备当前信息
  1011. that.getchannelData(other.ProdModel);
  1012. // 电量
  1013. if (other.Power) {
  1014. var battery = constant.lexinMessage.getBattery(other.Power);
  1015. that.setData({
  1016. battery: battery,
  1017. });
  1018. }
  1019. // 当前设备木有设置定时
  1020. that.setData({
  1021. isSetWake: false,
  1022. });
  1023. other.alarm.map((v) => {
  1024. if (v.enable === "1") {
  1025. that.setData({
  1026. isSetWake: true
  1027. });
  1028. }
  1029. });
  1030. },
  1031. ///******************************* 可折叠 ********************************************///
  1032. onUserInfoLoad() {
  1033. var that = this;
  1034. var userInfo = wx.getStorageSync("userInfo") || "";
  1035. if (!constant.strings.isEmpty(userInfo)) {
  1036. var phone = userInfo.phone;
  1037. if (!phone) {
  1038. return;
  1039. };
  1040. var nickName = userInfo.nickname || "";
  1041. var greeting = that.getGreetBuNickName(nickName);
  1042. // 获取缓存的频道数据
  1043. var channelData = wx.getStorageSync("channelData") || "";
  1044. if (!constant.strings.isEmpty(channelData)) {
  1045. that.setData({
  1046. channelData: channelData,
  1047. greeting: greeting,
  1048. nickName: nickName,
  1049. userPic: userInfo.headUrl || "",
  1050. isLogin: true,
  1051. })
  1052. } else {
  1053. that.setData({
  1054. greeting: greeting,
  1055. nickName: nickName,
  1056. userPic: userInfo.headUrl || "",
  1057. isLogin: true,
  1058. });
  1059. }
  1060. } else {
  1061. var greeting = that.getGreetBuNickName("");
  1062. that.setData({
  1063. greeting: greeting,
  1064. })
  1065. }
  1066. },
  1067. getGreetBuNickName(nickName) {
  1068. var greeting = constant.timeUtil.getGreet();
  1069. if (nickName != "" && nickName != "未登录") {
  1070. greeting = greeting + ',' + nickName;;
  1071. }
  1072. return greeting;
  1073. },
  1074. onBannerLoad() {
  1075. var that = this;
  1076. constant.apiUtil.getBanner({}).then((res) => {
  1077. that.setData({
  1078. bannerList: res,
  1079. });
  1080. constant.store.setStore("homeBanner", res);
  1081. });
  1082. },
  1083. stopIntervalId1: function () {
  1084. var that = this;
  1085. if (!constant.strings.isEmpty(that.data.intervalId1)) {
  1086. clearInterval(that.data.intervalId1);
  1087. that.data.intervalId1 = null;
  1088. }
  1089. },
  1090. stopIntervalId2: function () {
  1091. var that = this;
  1092. if (!constant.strings.isEmpty(that.data.intervalId2)) {
  1093. clearInterval(that.data.intervalId2);
  1094. that.data.intervalId2 = null;
  1095. }
  1096. },
  1097. ///销毁蓝牙
  1098. onUnload() {
  1099. var that = this;
  1100. constant.eventBus.removeNotification(CmdEvent.eventName, that)
  1101. console.log("界面onUnload")
  1102. that.stopIntervalId1();
  1103. that.stopIntervalId2();
  1104. if (!constant.strings.isEmpty(that.data.intervalId)) {
  1105. clearInterval(that.data.intervalId);
  1106. that.data.intervalId = null;
  1107. }
  1108. BtHelper.getInstance().disconnect();
  1109. if (constant.app.globalData.client === null) {
  1110. return;
  1111. };
  1112. constant.app.globalData.client.end(true);
  1113. constant.app.globalData.client.end(true);
  1114. constant.app.globalData.client = null;
  1115. },
  1116. })