index.js 32 KB

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