index.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. const app = getApp();
  2. const {
  3. getBanner,
  4. listByDevice
  5. } = require('../../utils/api.js');
  6. import timeUtil from '../../utils/time_util.js';
  7. import strings from '../../utils/strings.js';
  8. import store from '../../utils/store.js';
  9. import route_constant from '../../utils/route_constant.js';
  10. import route_util from '../../utils/route_util.js';
  11. import {
  12. BtHelper
  13. } from '../../devices/bt_helper.js';
  14. Page({
  15. data: {
  16. nvabarData: {
  17. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  18. title: 'OhPlay', //导航栏 中间的标题
  19. },
  20. ///下午好
  21. isLogin: false,
  22. greeting: "",
  23. bannerList: [{
  24. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
  25. },
  26. {
  27. "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
  28. }
  29. ],
  30. autoplay: true,
  31. interval: 3000, // 切换时间间隔
  32. duration: 500, // 滑动动画时长
  33. circular: true, // 衔接滑动
  34. indexPage: 0,
  35. ///是否展示频道
  36. showChannel: false,
  37. ////首页
  38. navBarHeight: app.globalData.navBarHeight,
  39. MenuButtonheight: app.globalData.MenuButtonheight,
  40. MenuButtonTop: app.globalData.MenuButtonTop,
  41. actionIndex: null,
  42. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  43. channelData: [],
  44. deviceList: [],
  45. deviceListIndex: null,
  46. isOneLoading: true,
  47. uid: null,
  48. isSetWake: false,
  49. thisDeviceMac: null,
  50. battery: 4, // 0≤电量<20,0格
  51. ////我的界面
  52. loginStatus: true,
  53. nickName: "未登录",
  54. userPic: './../../img/head_pic.png',
  55. intervalId: null,
  56. autoConnected: false,
  57. },
  58. onLoad(options) {
  59. var that = this;
  60. that.onDeviceLoad();
  61. that.onUserInfoLoad();
  62. that.onBannerLoad();
  63. },
  64. onShow() {
  65. var that = this;
  66. that.onHomeShow();
  67. that.onBannerShow();
  68. },
  69. onHomeShow() {
  70. var that = this;
  71. var userInfo = wx.getStorageSync("userInfo") || "";
  72. if (!strings.isEmpty(userInfo)) {
  73. // 获取设备本地数据
  74. if (app.globalData.newDeviceId) {
  75. that.addNewDeviceId();
  76. } else if (app.globalData.client == null) {
  77. app.connect();
  78. } else if (that.data.deviceListIndex == null && that.data.deviceList.length > 0) {
  79. that.actionDevice(0)
  80. }
  81. } else {
  82. if (app.globalData.client == null) {
  83. app.connect();
  84. }
  85. }
  86. },
  87. onDeviceLoad() {
  88. var that = this;
  89. BtHelper.getInstance().initBluetoothAdapter();
  90. var l = store.getStore("deviceList");
  91. if (!strings.isEmpty(l)) {
  92. that.updateDeviceList(l, true);
  93. }
  94. ///监听蓝牙设备
  95. BtHelper.getInstance().getBluetoothDevices();
  96. ///3秒去处理一下
  97. BtHelper.getInstance().startScan(null, null, null);
  98. var isFirst = true;
  99. ///再秒再对比一次
  100. that.data.intervalId = setInterval(async function () {
  101. isFirst = false;
  102. that.compareList();
  103. }, isFirst ? 6 * 1000 : 12 * 1000);
  104. },
  105. async compareList() {
  106. var that = this;
  107. var deviceList = that.data.deviceList;
  108. if (deviceList.length > 0) {
  109. if (BtHelper.getInstance().getCallBackConnect() == null) {
  110. var compareList = BtHelper.getInstance().getCompareList();
  111. var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
  112. ///去掉未连接的离线的
  113. for (var i = 0; i < dissmissDevice.length; i++) {
  114. for (var j = 0; j < compareList.length; j++) {
  115. if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
  116. compareList.splice(j, 1);
  117. break;
  118. }
  119. }
  120. }
  121. if (compareList.length > 0) {
  122. ///对比在线的蓝牙设备
  123. var isChanged = false;
  124. for (var i = 0; i < deviceList.length; i++) {
  125. var tempItem = deviceList[i];
  126. if (tempItem.connectType != 3) {
  127. var has = false;
  128. for (var j = 0; j < compareList.length; j++) {
  129. // "state":"online" "offline" MW-SR1(4G_WIFI)
  130. if (tempItem.deviceId === compareList[j].deviceId) {
  131. has = true;
  132. break;
  133. }
  134. }
  135. if (has) {
  136. if (tempItem.state != "online") {
  137. isChanged = true;
  138. tempItem.state = "online";
  139. }
  140. } else {
  141. if (tempItem.state != "offline") {
  142. isChanged = true;
  143. tempItem.state = "offline";
  144. }
  145. }
  146. }
  147. }
  148. if (isChanged) {
  149. that.updateDeviceList(deviceList, false);
  150. }
  151. ///没有连接则连接第一个在线的蓝牙
  152. if (!that.data.autoConnected) {
  153. var dList = that.data.deviceList;
  154. if (dList.length > 0) {
  155. for (var i = 0; i < dList.length; i++) {
  156. var item = dList[i];
  157. if (item.connectType == 1 && item.state == "online") {
  158. item.name = item.devName;
  159. if (that.data.deviceListIndex == null) {
  160. BtHelper.getInstance().connect(item, function (data) {
  161. if (data) {
  162. that.addConnectBlueDevice(item);
  163. }
  164. });
  165. }
  166. break;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. BtHelper.getInstance().startScan(null, null, null);
  173. }
  174. }
  175. },
  176. addNewDeviceId() {
  177. // 连接mqtt
  178. var that = this;
  179. if (app.globalData.client === null) {
  180. app.connect();
  181. } else if (app.globalData.newDeviceId) {
  182. //监听
  183. var deviceList = that.data.deviceList;
  184. if (deviceList.length == 0) {
  185. return;
  186. }
  187. ///连接新添加的设备
  188. var deviceListIndex = that.data.deviceListIndex;
  189. if (deviceListIndex != null) {
  190. if (deviceList.length > deviceListIndex) {
  191. if (deviceList[deviceListIndex].deviceId != deviceList[0].deviceId) {
  192. that.setData({
  193. actionIndex: null,
  194. deviceListIndex: null,
  195. });
  196. }
  197. } else {
  198. that.setData({
  199. actionIndex: null,
  200. deviceListIndex: null,
  201. });
  202. }
  203. }
  204. var topic = `/AIrSMArT_${deviceList[0].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  205. app.subscribe(topic);
  206. const Timeout = setTimeout(() => {
  207. clearTimeout(Timeout);
  208. that.actionDevice(0);
  209. }, 500);
  210. }
  211. },
  212. // 回调
  213. mqttCallback(type, option) {
  214. // console.log("gadsfadsfadsfa==888===" + type);
  215. var that = this;
  216. let payloads = null;
  217. if (option) {
  218. payloads = JSON.parse(option.payload);
  219. };
  220. switch (type) {
  221. ///先走订阅
  222. case "connect":
  223. that.subscribeDevicesStatus();
  224. break;
  225. ///再走有在线
  226. case "message_onoffline":
  227. that.onlineDevice(payloads);
  228. break;
  229. case "message":
  230. ///获取频道数据
  231. ///锁定播放哪一个频道
  232. if (payloads.type === "get_position" && payloads.other) {
  233. let actionIndex = null;
  234. that.data.channelData.map((v, index) => {
  235. if (v.channelNum === payloads.other.channel) {
  236. actionIndex = index;
  237. }
  238. });
  239. that.setData({
  240. actionIndex,
  241. });
  242. }
  243. ///获取播放状态
  244. else if (payloads.type === "play" || payloads.type === "play_state") {
  245. var deviceList = that.data.deviceList;
  246. var deviceListIndex = that.data.deviceListIndex;
  247. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  248. return
  249. };
  250. var deviceId = deviceList[deviceListIndex].deviceId;
  251. // 接收设备当前播放状态
  252. const obj = {
  253. DstDeviceName: that.getThisDeviceID(deviceId)
  254. }
  255. app.PubMsg({
  256. type: "get_position",
  257. ...obj
  258. });
  259. }
  260. ///获取频道列表数据
  261. else if (payloads.type === "get_dev_info") {
  262. // 接收设备当前信息
  263. that.getchannelData(payloads.other.ProdModel);
  264. // 电量
  265. that.setData({
  266. battery: that._battery(payloads.other.Power),
  267. })
  268. // 当前设备木有设置定时
  269. that.setData({
  270. isSetWake: false,
  271. });
  272. payloads.other.alarm.map((v) => {
  273. if (v.enable === "1") {
  274. that.setData({
  275. isSetWake: true
  276. });
  277. }
  278. });
  279. // 更新信息
  280. ///连上就调用2次
  281. var isUpdate = false;
  282. // payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  283. var deviceList = that.data.deviceList;
  284. if (!strings.isEmpty(deviceList)) {
  285. for (var i = 0; i < deviceList.length; i++) {
  286. if (payloads.SrcDeviceName) {
  287. var deviceId = deviceList[i].deviceId;
  288. var splitDeviceId = deviceId.split("BLUFI_");
  289. if (splitDeviceId.length == 2) {
  290. var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
  291. if (index !== -1 && deviceList[i].ProdModel != payloads.other.ProdModel && deviceList[i].devName != payloads.other.devName) {
  292. isUpdate = true;
  293. deviceList[i].ProdModel = payloads.other.ProdModel;
  294. deviceList[i].devName = payloads.other.devName;
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. }
  301. ///数据有更新
  302. if (isUpdate) {
  303. that.updateDeviceList(deviceList, false);
  304. }
  305. } else if (payloads.type === "battery" && payloads.other) {
  306. that.setData({
  307. battery: that._battery(payloads.other.battery),
  308. })
  309. }
  310. break;
  311. default:
  312. }
  313. },
  314. // 格式化电量
  315. _battery(battery) {
  316. let _battery = 0;
  317. if (battery < 20) {
  318. _battery = 0
  319. } else if (20 <= battery && battery < 40) {
  320. _battery = 1
  321. } else if (40 <= battery && battery < 60) {
  322. _battery = 2
  323. } else if (60 <= battery && battery < 80) {
  324. _battery = 3
  325. } else if (80 <= battery && battery <= 100) {
  326. _battery = 4
  327. } else if (battery > 100) {
  328. _battery = 5
  329. };
  330. return _battery
  331. },
  332. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  333. ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
  334. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  335. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  336. onlineDevice(payloads) {
  337. // 设置在线状态
  338. var that = this;
  339. // console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  340. ///是否更新过在线离线状态
  341. var isUpdate = false;
  342. var deviceList = that.data.deviceList;
  343. if (!strings.isEmpty(deviceList)) {
  344. for (var i = 0; i < deviceList.length; i++) {
  345. if (payloads.uuid) {
  346. var deviceId = deviceList[i].deviceId;
  347. var splitDeviceId = deviceId.split("BLUFI_");
  348. if (splitDeviceId.length == 2) {
  349. var index = payloads.uuid.indexOf(splitDeviceId[1]);
  350. if (index !== -1) {
  351. if (deviceList[i].state != payloads.state) {
  352. isUpdate = true;
  353. deviceList[i].state = payloads.state;
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. ///数据有更新
  362. if (isUpdate) {
  363. that.updateDeviceList(deviceList, false);
  364. }
  365. ///当前没有连接设备,则去连接第一个wifi设备
  366. var deviceListIndex = that.data.deviceListIndex;
  367. if (deviceListIndex === null) {
  368. var list = that.data.deviceList;
  369. for (var i = 0; i < list.length; i++) {
  370. if (list[i].state === "online" && list[i].connectType == 3) {
  371. if (that.data.isLogin) {
  372. that.actionDevice(i);
  373. }
  374. break;
  375. }
  376. }
  377. } else {
  378. // 当前播放设备离线
  379. if (that.data.deviceList.length > deviceListIndex && that.data.deviceList[deviceListIndex].state !== "online") {
  380. that.setData({
  381. actionIndex: null,
  382. deviceListIndex: null,
  383. });
  384. };
  385. }
  386. },
  387. // 订阅设备在线状态
  388. subscribeDevicesStatus() {
  389. var that = this;
  390. var deviceList = that.data.deviceList;
  391. if (!strings.isEmpty(deviceList)) {
  392. for (var i = 0; i < deviceList.length; i++) {
  393. if (deviceList[i].connectType == 3) {
  394. let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  395. app.subscribe(topic);
  396. }
  397. }
  398. }
  399. },
  400. subscribeCurrDevice() {
  401. var that = this;
  402. if (!(app.globalData.client && app.globalData.client.connected)) {
  403. console.log("未连接MQTT服务器");
  404. const str = setInterval(() => {
  405. clearInterval(str);
  406. that.subscribeCurrDevice();
  407. }, 500);
  408. return;
  409. };
  410. var deviceList = that.data.deviceList;
  411. var deviceListIndex = that.data.deviceListIndex;
  412. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  413. return
  414. };
  415. var device = that.data.deviceList[deviceListIndex];
  416. var tempDeviceId = device.deviceId;
  417. ///删除当前选中这个
  418. deviceList.splice(deviceListIndex, 1);
  419. ///添加到第一个去
  420. deviceList.unshift(device);
  421. that.setData({
  422. deviceListIndex: 0,
  423. autoConnected: true,
  424. deviceList: deviceList,
  425. });
  426. let topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
  427. app.subscribe(topic);
  428. const obj = {
  429. DstDeviceName: that.getThisDeviceID(tempDeviceId)
  430. };
  431. app.PubMsg({
  432. type: "get_dev_info",
  433. ...obj
  434. });
  435. },
  436. actionMusic(e) {
  437. var that = this;
  438. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  439. return;
  440. };
  441. if (that.data.deviceListIndex === null) {
  442. wx.showToast({
  443. title: '请选择设备',
  444. icon: "none"
  445. })
  446. return;
  447. };
  448. that.setData({
  449. actionIndex: e.currentTarget.dataset.index
  450. });
  451. var deviceList = that.data.deviceList;
  452. var deviceListIndex = that.data.deviceListIndex;
  453. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  454. return
  455. };
  456. var deviceId = deviceList[deviceListIndex].deviceId;
  457. const other = {
  458. "url": "",
  459. "media_data": "",
  460. "user_id": `${app.globalData.userInfo.deviceUid}`,
  461. "timestamp": `${Math.round(new Date() / 1000)}`,
  462. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  463. "order": "",
  464. "resource_from": "",
  465. "songAlbumID": "",
  466. "version": 3,
  467. "is_debug": app.globalData.is_debug
  468. };
  469. app.PubMsg({
  470. type: "play",
  471. DstDeviceName: that.getThisDeviceID(deviceId),
  472. other
  473. });
  474. },
  475. getThisDeviceID(deviceId) {
  476. return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
  477. },
  478. getchannelData(clientType) {
  479. var that = this;
  480. var deviceList = that.data.deviceList;
  481. var deviceListIndex = that.data.deviceListIndex;
  482. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  483. return;
  484. };
  485. var deviceId = deviceList[deviceListIndex].deviceId;
  486. listByDevice({
  487. clientType
  488. }).then((res) => {
  489. that.setData({
  490. channelData: res
  491. });
  492. // 接收设备当前播放状态
  493. const obj = {
  494. DstDeviceName: that.getThisDeviceID(deviceId)
  495. }
  496. app.PubMsg({
  497. type: "get_position",
  498. ...obj
  499. });
  500. wx.setStorageSync("channelData", res);
  501. // 有新设备
  502. if (app.globalData.newDeviceId) {
  503. app.globalData.newDeviceId = null;
  504. return;
  505. }
  506. })
  507. },
  508. ///去频道详情
  509. onTapToChannel() {
  510. var that = this;
  511. if (that.data.channelData.length > that.data.actionIndex) {
  512. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  513. wx.navigateTo({
  514. url: './../channelDetails/channelDetails'
  515. });
  516. }
  517. },
  518. onTapIndex(e) {
  519. var that = this;
  520. var index = e.currentTarget.dataset.index;
  521. var indexPage = that.data.indexPage;
  522. if (indexPage != index) {
  523. that.setData({
  524. indexPage: index,
  525. });
  526. }
  527. },
  528. goMeAbout() {
  529. wx.navigateTo({
  530. url: './../about/about',
  531. })
  532. },
  533. onTapLogin() {
  534. var that = this;
  535. ///退出登录
  536. if (that.data.isLogin) {
  537. that.logOut();
  538. }
  539. // 登录
  540. else {
  541. route_util.jump(route_constant.login);
  542. }
  543. },
  544. ///退出登录
  545. logOut() {
  546. var that = this;
  547. ///有设备在线被选中,则让它不被选择
  548. var deviceListIndex = that.data.deviceListIndex;
  549. if (deviceListIndex != null) {
  550. var deviceList = that.data.deviceList;
  551. if (deviceList.length > deviceListIndex) {
  552. if (deviceList[deviceListIndex].connectType == 3) {
  553. if (that.data.thisDeviceMac !== null) {
  554. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  555. that.setData({
  556. actionIndex: null,
  557. deviceListIndex: null,
  558. });
  559. };
  560. }
  561. }
  562. }
  563. wx.removeStorageSync('userInfo');
  564. wx.removeStorageSync('token');
  565. var nickName = "未登录";
  566. var greeting = that.getGreetBuNickName(nickName);
  567. that.setData({
  568. isLogin: false,
  569. greeting: greeting,
  570. nickName: nickName,
  571. userPic: "./../../img/head_pic.png",
  572. });
  573. },
  574. ///添加设备
  575. addDevice() {
  576. var that = this;
  577. if (!that.data.isLogin) {
  578. route_util.jump(route_constant.login);
  579. return;
  580. }
  581. ///跳转设备列表
  582. route_util.jump(route_constant.deviceList);
  583. },
  584. ///点击item
  585. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  586. onTapItem(e) {
  587. var that = this;
  588. if (!that.data.isLogin) {
  589. route_util.jump(route_constant.login);
  590. return;
  591. }
  592. var item = e.currentTarget.dataset.item;
  593. console.log("点击item:", item)
  594. var connectType = item.connectType;
  595. // wifi只支持在线点击
  596. if (connectType == 3 && item.state === "online") {
  597. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  598. that.goWake();
  599. } else {
  600. that.actionDevice(e.currentTarget.dataset.index)
  601. }
  602. return;
  603. }
  604. if (item.connectType != "1") {
  605. return
  606. }
  607. ///去蓝牙连接处理
  608. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  609. // 蓝牙才能连接
  610. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  611. } else if (item.state === "offline") {
  612. console.log("去连接蓝牙")
  613. } else {
  614. ///item
  615. // {"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 妙播收音机"}
  616. item.name = item.devName;
  617. BtHelper.getInstance().connect(item, function (data) {
  618. if (data) {
  619. that.addConnectBlueDevice(item);
  620. }
  621. });
  622. }
  623. },
  624. actionDevice(index) {
  625. var that = this;
  626. if (!that.data.isLogin) {
  627. return;
  628. }
  629. if (that.data.deviceList.length <= index) {
  630. return;
  631. };
  632. const device = that.data.deviceList[index];
  633. if (device.state !== "online") {
  634. return;
  635. };
  636. // 取消订阅
  637. if (that.data.thisDeviceMac !== null) {
  638. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  639. };
  640. that.setData({
  641. deviceListIndex: index,
  642. autoConnected: true,
  643. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
  644. });
  645. // app.PubMsg({
  646. // type: "get_dev_info",
  647. // DstDeviceName: that.getThisDeviceID()
  648. // });
  649. that.subscribeCurrDevice();
  650. },
  651. goWake() {
  652. var that = this;
  653. if (that.data.deviceListIndex === null) {
  654. return;
  655. };
  656. wx.navigateTo({
  657. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  658. });
  659. },
  660. ///点击banner事件
  661. onTapBanner() {
  662. var that = this;
  663. var item = e.currentTarget.dataset.item;
  664. },
  665. ///删除当前设备
  666. deleteDevice(e) {
  667. var that = this;
  668. var index = e.currentTarget.dataset.index;
  669. wx.showModal({
  670. title: '确定删除?',
  671. success: function (res) {
  672. if (res.confirm) {
  673. if (that.data.deviceList[index].connectType == 3) {
  674. that.cancelWifi(index, false);
  675. } else {
  676. that.cancelBlue(index, false);
  677. }
  678. }
  679. }
  680. });
  681. },
  682. ///是否是同一个蓝牙
  683. isTheSameBlue(connectDevice) {
  684. var that = this;
  685. var deviceListIndex = that.data.deviceListIndex;
  686. if (deviceListIndex == null) {
  687. return false;
  688. }
  689. var deviceList = that.data.deviceList;
  690. if (deviceList.length > deviceListIndex) {
  691. if (deviceList[deviceListIndex].connectType == 3) {
  692. return false;
  693. } else {
  694. if (deviceList[deviceListIndex].deviceId == connectDevice.deviceId) {
  695. return true;
  696. } else {
  697. return false;
  698. }
  699. }
  700. } else {
  701. return false;
  702. }
  703. },
  704. ///断开当前的
  705. async cancelCurrent() {
  706. var that = this;
  707. var deviceListIndex = that.data.deviceListIndex;
  708. if (deviceListIndex == null) {
  709. return;
  710. }
  711. var deviceList = that.data.deviceList;
  712. if (deviceList.length > deviceListIndex) {
  713. if (deviceList[deviceListIndex].connectType == 3) {
  714. await that.cancelWifi(deviceListIndex, true);
  715. } else {
  716. await that.cancelBlue(deviceListIndex, true);
  717. }
  718. }
  719. },
  720. /// 断开蓝牙连接
  721. async cancelBlue(index, onlyCancel) {
  722. var that = this;
  723. // await BtHelper.getInstance().disconnect();
  724. if (onlyCancel) {
  725. return;
  726. }
  727. const id = that.data.deviceList[index].deviceId;
  728. const deviceList = that.data.deviceList.filter((item, i) => {
  729. return id !== item.deviceId
  730. });
  731. store.setStore("deviceList", deviceList);
  732. that.setData({
  733. deviceList,
  734. });
  735. // 当前没有设备
  736. // if (deviceList.length === 0) {
  737. that.setData({
  738. deviceListIndex: null,
  739. });
  740. // }
  741. },
  742. /// 断开连接wifi
  743. async cancelWifi(index, onlyCancel) {
  744. var that = this;
  745. // 取消订阅
  746. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}/user/pub_response`);
  747. if (onlyCancel) {
  748. return;
  749. }
  750. const id = that.data.deviceList[index].deviceId;
  751. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  752. const deviceList = that.data.deviceList.filter((item, i) => {
  753. return id !== item.deviceId
  754. });
  755. store.setStore("deviceList", deviceList);
  756. that.setData({
  757. deviceList,
  758. thisDeviceMac: null
  759. });
  760. // 当前没有设备
  761. // if (deviceList.length === 0) {
  762. that.setData({
  763. actionIndex: null,
  764. deviceListIndex: null,
  765. });
  766. // }
  767. if (that.data.deviceListIndex === null) {
  768. return
  769. };
  770. if (index === that.data.deviceListIndex) {
  771. let index_ = null;
  772. deviceList.map((v, index) => {
  773. if (v.state === "online" && index_ === null) {
  774. index_ = index;
  775. }
  776. });
  777. if (index_ !== null) {
  778. that.actionDevice(index_);
  779. } else {
  780. that.setData({
  781. actionIndex: null,
  782. deviceListIndex: null,
  783. });
  784. };
  785. } else {
  786. deviceList.map((v, index) => {
  787. if (v.name === name) {
  788. that.setData({
  789. deviceListIndex: index,
  790. autoConnected: true,
  791. });
  792. }
  793. });
  794. }
  795. },
  796. // {"applicationType":"[0, 1]","deviceId":"DB:45:DD:76:42:15","img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100711728016597.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100714667384264.png","connectImg":null,"name":"猫王音响·小王子 OTR-X","bluetoothName":"猫王音响·小王子 OTR-X","bluetoothNames":["猫王音响·小王子 OTR-X"],"isChannelsPlatforms":0,"platform":-1,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[1,3,6],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100644913162836.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100648938942906.png","icon":null,"guideUrl":null}},{"is5g":0,"type":2,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155903515728925.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155910706032704.png","icon":null,"guideUrl":null}}],"clientType":"MW-2AX(WIFI)","firstVersion":"0.0.1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0,"mac":"9b45dd76e2150000",
  797. /// deviceList
  798. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  799. /// clientType
  800. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  801. ///新添加蓝牙设备
  802. addConnectBlueDevice(newDevice) {
  803. var that = this;
  804. // 同一个设备
  805. var deviceList = that.data.deviceList;
  806. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  807. if (tempList && tempList.length > 0) {
  808. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  809. };
  810. console.log("添加蓝牙设备:", newDevice);
  811. deviceList.unshift({
  812. /// 蓝牙ble连接
  813. connectType: 1,
  814. deviceId: newDevice.deviceId,
  815. name: newDevice.name,
  816. state: "online",
  817. ProdModel: newDevice.ProdModel || newDevice.clientType,
  818. clientType: newDevice.clientType || newDevice.ProdModel,
  819. devName: newDevice.name,
  820. mac: newDevice.mac,
  821. img: newDevice.img,
  822. });
  823. ///在线排序前面,wifi设备排序前面
  824. that.updateDeviceList(deviceList, false);
  825. var indexPage = that.data.indexPage;
  826. if (indexPage != 0) {
  827. that.setData({
  828. indexPage: 0,
  829. deviceListIndex: 0,
  830. autoConnected: true,
  831. });
  832. } else {
  833. that.setData({
  834. deviceListIndex: 0,
  835. autoConnected: true,
  836. });
  837. }
  838. },
  839. ///新添加wifi设备
  840. async addConnectWifiDevice(newDevice) {
  841. var that = this;
  842. // 同一个设备
  843. var deviceList = that.data.deviceList;
  844. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  845. if (tempList && tempList.length > 0) {
  846. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  847. };
  848. console.log("添加Wifi设备:" + JSON.stringify(newDevice));
  849. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  850. deviceList.unshift({
  851. /// Wifi连接
  852. connectType: 3,
  853. deviceId: newDevice.deviceId,
  854. name: "猫王小王子OTR-X",
  855. state: "online",
  856. ProdModel: 3,
  857. devName: "猫王小王子OTR-X",
  858. mac: newDevice.deviceId,
  859. image: "./../../img/min.png",
  860. });
  861. ///在线排序前面,wifi设备排序前面
  862. that.updateDeviceList(deviceList, false);
  863. var indexPage = that.data.indexPage;
  864. if (indexPage != 0) {
  865. that.setData({
  866. indexPage: 0,
  867. });
  868. }
  869. ///断开蓝牙连接
  870. // await BtHelper.getInstance().disconnect();
  871. that.actionDevice(0);
  872. },
  873. ///更新列表排序
  874. updateDeviceList(deviceList, isInit) {
  875. if (deviceList.length <= 0) {
  876. return;
  877. }
  878. var that = this;
  879. var finalList = [];
  880. if (isInit) {
  881. deviceList[0].state = "offline";
  882. }
  883. var isFirstOnline = false;
  884. if (deviceList[0].state == "online") {
  885. isFirstOnline = true;
  886. finalList.push(deviceList[0]);
  887. }
  888. ///区分在线和离线
  889. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  890. if (isInit) {
  891. deviceList[i].state = "offline";
  892. }
  893. }
  894. var onLineList = [];
  895. var onNoLineList = [];
  896. ///添加在线的
  897. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  898. var device = deviceList[i];
  899. if (device.state == "online") {
  900. onLineList.push(device)
  901. }
  902. }
  903. ///添加离线的
  904. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  905. var device = deviceList[i];
  906. if (device.state != "online") {
  907. onNoLineList.push(device)
  908. }
  909. }
  910. // 区分在线wifi和蓝牙 wifi在前 离线在后
  911. var onLineWifiList = [];
  912. var onLineBlueList = [];
  913. ///添加在线wifi
  914. onLineList.forEach(element => {
  915. if (element.connectType == 3) {
  916. onLineWifiList.push(element);
  917. }
  918. });
  919. ///添加在线蓝牙
  920. onLineList.forEach(element => {
  921. if (element.connectType != 3) {
  922. onLineBlueList.push(element)
  923. }
  924. });
  925. finalList = finalList.concat(onLineWifiList);
  926. finalList = finalList.concat(onLineBlueList);
  927. ///只需要蓝牙和wifi在线的
  928. let mDeviceList = []
  929. mDeviceList = mDeviceList.concat(onLineWifiList);
  930. mDeviceList = mDeviceList.concat(onLineBlueList);
  931. getApp().globalData.mDeviceList = mDeviceList;
  932. ///区分离线wifi和蓝牙 wifi在前 离线在后
  933. var onNoLineWifiList = [];
  934. var onNoLineBlueList = [];
  935. ///添加离线wifi
  936. onNoLineList.forEach(element => {
  937. if (element.connectType == 3) {
  938. onNoLineWifiList.push(element)
  939. }
  940. });
  941. ///添加离线蓝牙
  942. onNoLineList.forEach(element => {
  943. if (element.connectType != 3) {
  944. onNoLineBlueList.push(element)
  945. }
  946. });
  947. finalList = finalList.concat(onNoLineWifiList);
  948. finalList = finalList.concat(onNoLineBlueList);
  949. store.setStore("deviceList", finalList);
  950. that.setData({
  951. deviceList: finalList
  952. });
  953. },
  954. ///******************************* 可折叠 ********************************************///
  955. onUserInfoLoad() {
  956. var that = this;
  957. var userInfo = wx.getStorageSync("userInfo") || "";
  958. if (!strings.isEmpty(userInfo)) {
  959. var phone = userInfo.phone;
  960. if (!phone) {
  961. return;
  962. };
  963. var nickName = userInfo.nickname || "";
  964. var greeting = that.getGreetBuNickName(nickName);
  965. // 获取缓存的频道数据
  966. var channelData = wx.getStorageSync("channelData") || "";
  967. if (!strings.isEmpty(channelData)) {
  968. that.setData({
  969. channelData: channelData,
  970. greeting: greeting,
  971. nickName: nickName,
  972. userPic: userInfo.headUrl || "",
  973. isLogin: true,
  974. })
  975. } else {
  976. that.setData({
  977. greeting: greeting,
  978. nickName: nickName,
  979. userPic: userInfo.headUrl || "",
  980. isLogin: true,
  981. });
  982. }
  983. } else {
  984. var greeting = that.getGreetBuNickName("");
  985. that.setData({
  986. greeting: greeting,
  987. })
  988. }
  989. },
  990. getGreetBuNickName(nickName) {
  991. var greeting = timeUtil.getGreet();
  992. if (nickName != "" && nickName != "未登录") {
  993. greeting = greeting + ',' + nickName;;
  994. }
  995. return greeting;
  996. },
  997. onBannerLoad() {
  998. var that = this;
  999. // var bannerList = wx.getStorageSync("homeBanner") || [];
  1000. // if (that.data.bannerList.length > 0) {
  1001. // that.setData({
  1002. // bannerList: bannerList
  1003. // });
  1004. // }
  1005. },
  1006. onBannerShow() {
  1007. var that = this;
  1008. // getBanner({}).then((res) => {
  1009. // that.setData({
  1010. // bannerList: res
  1011. // });
  1012. // wx.setStorageSync("homeBanner", res);
  1013. // })
  1014. },
  1015. ///销毁蓝牙
  1016. onUnload() {
  1017. var that = this;
  1018. if (!strings.isEmpty(that.data.intervalId)) {
  1019. clearInterval(that.data.intervalId);
  1020. that.data.intervalId = null;
  1021. }
  1022. BtHelper.getInstance().disconnect();
  1023. if (app.globalData.client === null) {
  1024. return;
  1025. };
  1026. app.globalData.client.end(true);
  1027. app.globalData.client.end(true);
  1028. app.globalData.client = null;
  1029. },
  1030. })
  1031. // tryConnectBle() {
  1032. // var hasBle = false;
  1033. // var hasConnectBle = false;
  1034. // var hasWifi = false;
  1035. // var bleDevice;
  1036. // let that = this
  1037. // that.data.deviceList.forEach(device => {
  1038. // if (device.connectType == 1) {
  1039. // hasBle = true
  1040. // if (device.state == "online") {
  1041. // hasConnectBle = true
  1042. // }
  1043. // bleDevice = device
  1044. // } else if (device.connectType == 3 && device.state == "online") {
  1045. // // wifi
  1046. // hasWifi = true;
  1047. // }
  1048. // });
  1049. // if (!hasWifi && !hasConnectBle && hasBle) {
  1050. // // 没有wifi 没有连接的ble 有未连接的ble
  1051. // console.log("去连接蓝牙")
  1052. // let bt_helper = BtHelper.getInstance()
  1053. // bt_helper.initBluetooth(function (adapterState, hasPermission) {
  1054. // console.log("蓝牙状态", adapterState, hasPermission)
  1055. // if (adapterState && hasPermission) {
  1056. // bt_helper.connect(bleDevice, function (data) {
  1057. // console.log("连接成功:", data)
  1058. // if (data == true) {
  1059. // // 蓝牙模式
  1060. // bleDevice.connectType = 1
  1061. // // 在线
  1062. // bleDevice.state = 'online'
  1063. // bleDevice.ProdModel = bleDevice.clientType
  1064. // that.addConnectBlueDevice(connectDevice);
  1065. // }
  1066. // })
  1067. // } else {}
  1068. // })
  1069. // }
  1070. // },
  1071. // 去掉此功能,先留着吧
  1072. // const other= {
  1073. // "url": "",
  1074. // "media_data": "",
  1075. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  1076. // "timestamp": `${Math.round(new Date() / 1000)}`,
  1077. // "channel_id": `${res[1].channelNum}`,
  1078. // "order": "",
  1079. // "resource_from": "",
  1080. // "songAlbumID":"",
  1081. // "version":3,
  1082. // "is_debug": app.globalData.is_debug
  1083. // };
  1084. // app.PubMsg({
  1085. // type: "play",
  1086. // DstDeviceName: that.getThisDeviceID(),
  1087. // other
  1088. // });