index.js 36 KB

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