index.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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, false);
  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, 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 (deviceListSelect === null || 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, 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, 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. // 关于我们
  444. goMeAbout() {
  445. wx.navigateTo({
  446. url: './../about/about',
  447. })
  448. },
  449. onTapLogin() {
  450. var that = this;
  451. ///退出登录
  452. if (that.data.isLogin) {
  453. that.logOut();
  454. }
  455. // 登录
  456. else {
  457. route_util.jump(route_constant.login);
  458. }
  459. },
  460. ///退出登录
  461. logOut() {
  462. var that = this;
  463. wx.removeStorageSync('userInfo');
  464. wx.removeStorageSync('token');
  465. var nickName = "未登录";
  466. var greeting = that.getGreetBuNickName(nickName);
  467. that.setData({
  468. isLogin: false,
  469. greeting: greeting,
  470. nickName: nickName,
  471. userPic: "./../../img/head_pic.png",
  472. });
  473. var deviceList = that.data.deviceList;
  474. var deviceListSelect = that.data.deviceListSelect;
  475. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  476. return;
  477. };
  478. ///有设备在线被选中,则让它不被选择
  479. var deviceList = that.data.deviceList;
  480. if (deviceList[deviceListSelect].connectType == 3) {
  481. var thisDeviceMac = that.data.thisDeviceMac;
  482. if (thisDeviceMac !== null) {
  483. var response = `/${thisDeviceMac}/user/pub_response`;
  484. app.unsubscribe(response);
  485. that.setData({
  486. actionIndex: null,
  487. deviceListSelect: null,
  488. });
  489. };
  490. }
  491. },
  492. ///添加设备
  493. addDevice() {
  494. var that = this;
  495. if (!that.data.isLogin) {
  496. route_util.jump(route_constant.login);
  497. return;
  498. }
  499. ///跳转设备列表
  500. route_util.jump(route_constant.deviceList);
  501. },
  502. ///点击item
  503. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  504. onTapItem(e) {
  505. var that = this;
  506. var isLogin = that.data.isLogin;
  507. if (!isLogin) {
  508. route_util.jump(route_constant.login);
  509. return;
  510. }
  511. var item = e.currentTarget.dataset.item;
  512. var index = e.currentTarget.dataset.index;
  513. var connectType = item.connectType;
  514. var deviceListSelect = that.data.deviceListSelect;
  515. // wifi只支持在线点击
  516. if (connectType == 3 && item.state === "online") {
  517. if (index === deviceListSelect) {
  518. that.goWake();
  519. } else {
  520. that.actionDevice(e.currentTarget.dataset.index)
  521. }
  522. return;
  523. }
  524. ///去蓝牙连接处理
  525. if (index === deviceListSelect) {
  526. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  527. } else if (item.state === "offline") {
  528. console.log("去连接蓝牙")
  529. } else {
  530. ///item
  531. // {"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 妙播收音机"}
  532. item.name = item.devName;
  533. BtHelper.getInstance().connect(item, function (data) {
  534. if (data) {
  535. that.addConnectBlueDevice(item);
  536. }
  537. });
  538. }
  539. },
  540. ///去连接设备数据
  541. actionDevice(index) {
  542. var that = this;
  543. var isLogin = that.data.isLogin;
  544. if (!isLogin) {
  545. return;
  546. }
  547. var deviceList = that.data.deviceList;
  548. if (deviceList.length <= index) {
  549. return;
  550. };
  551. ///限制蓝牙设备和不在线wifi设备
  552. var device = deviceList[index];
  553. if (device.connectType != 3 || device.state != "online") {
  554. return;
  555. }
  556. // 取消订阅
  557. var thisDeviceMac = that.data.thisDeviceMac;
  558. if (thisDeviceMac !== null) {
  559. app.unsubscribe(`/${thisDeviceMac}/user/pub_response`);
  560. };
  561. thisDeviceMac = `AIrSMArT_${device.deviceId.split("BLUFI_")[1]}`;
  562. that.setData({
  563. deviceListSelect: index,
  564. thisDeviceMac: thisDeviceMac,
  565. });
  566. that.subscribeCurrDevice();
  567. },
  568. ///数据处理
  569. subscribeCurrDevice() {
  570. var that = this;
  571. if (!(app.globalData.client && app.globalData.client.connected)) {
  572. app.connect();
  573. setTimeout(() => {
  574. that.subscribeCurrDevice();
  575. }, 500);
  576. return;
  577. };
  578. var deviceList = that.data.deviceList;
  579. var deviceListSelect = that.data.deviceListSelect;
  580. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  581. return
  582. };
  583. var device = deviceList[deviceListSelect];
  584. var tempDeviceId = device.deviceId;
  585. ///删除当前选中这个
  586. deviceList.splice(deviceListSelect, 1);
  587. ///添加到第一个去
  588. deviceList.unshift(device);
  589. that.setData({
  590. deviceListSelect: 0,
  591. autoConnected: true,
  592. deviceList: deviceList,
  593. });
  594. /// /AIrSMArT_7cdfa1fd3af0/user/pub_response
  595. var topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
  596. app.subscribe(topic);
  597. const obj = {
  598. DstDeviceName: that.getThisDeviceID(tempDeviceId)
  599. };
  600. app.PubMsg({
  601. type: "get_dev_info",
  602. ...obj
  603. });
  604. },
  605. // 去唤醒界面
  606. goWake() {
  607. var that = this;
  608. var deviceList = that.data.deviceList;
  609. var deviceListSelect = that.data.deviceListSelect;
  610. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  611. return
  612. };
  613. var device = deviceList[deviceListSelect];
  614. wx.navigateTo({
  615. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  616. });
  617. },
  618. ///点击banner事件
  619. onTapBanner() {
  620. var that = this;
  621. var item = e.currentTarget.dataset.item;
  622. },
  623. ///删除当前设备
  624. deleteDevice(e) {
  625. var that = this;
  626. var index = e.currentTarget.dataset.index;
  627. wx.showModal({
  628. title: '确定删除?',
  629. success: function (res) {
  630. if (res.confirm) {
  631. if (that.data.deviceList[index].connectType == 3) {
  632. that.cancelWifi(index, false);
  633. } else {
  634. that.cancelBlue(index, false);
  635. }
  636. }
  637. }
  638. });
  639. },
  640. ///是否是同一个蓝牙
  641. isTheSameBlue(connectDevice) {
  642. var that = this;
  643. var deviceList = that.data.deviceList;
  644. var deviceListSelect = that.data.deviceListSelect;
  645. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  646. return false;
  647. };
  648. if (deviceList[deviceListSelect].connectType == 3) {
  649. return false;
  650. } else {
  651. if (deviceList[deviceListSelect].deviceId == connectDevice.deviceId) {
  652. return true;
  653. } else {
  654. return false;
  655. }
  656. }
  657. },
  658. ///断开当前的
  659. async cancelCurrent() {
  660. var that = this;
  661. var deviceList = that.data.deviceList;
  662. var deviceListSelect = that.data.deviceListSelect;
  663. if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
  664. return;
  665. };
  666. if (deviceList[deviceListSelect].connectType == 3) {
  667. await that.cancelWifi(deviceListSelect, true);
  668. } else {
  669. await that.cancelBlue(deviceListSelect, true);
  670. }
  671. },
  672. /// 断开蓝牙连接
  673. async cancelBlue(index, onlyCancel) {
  674. var that = this;
  675. if (onlyCancel) {
  676. return;
  677. }
  678. var deviceList = that.data.deviceList;
  679. var deviceId = deviceList[index].deviceId;
  680. ///删除当前设备
  681. deviceList = deviceList.filter((item, i) => {
  682. return deviceId !== item.deviceId;
  683. });
  684. store.setStore("deviceList", deviceList);
  685. that.setData({
  686. deviceListSelect: null,
  687. deviceList: deviceList,
  688. });
  689. },
  690. /// 断开连接wifi
  691. async cancelWifi(index, onlyCancel) {
  692. var that = this;
  693. var deviceList = that.data.deviceList;
  694. if (index === null || deviceList.length <= index) {
  695. return;
  696. };
  697. var deviceId = deviceList[index].deviceId;
  698. // 取消订阅
  699. app.unsubscribe(`/AIrSMArT_${deviceId.split("BLUFI_")[1]}/user/pub_response`);
  700. if (onlyCancel) {
  701. return;
  702. }
  703. deviceList = deviceList.filter((item, i) => {
  704. return deviceId !== item.deviceId;
  705. });
  706. store.setStore("deviceList", deviceList);
  707. that.setData({
  708. deviceList: deviceList,
  709. thisDeviceMac: null,
  710. actionIndex: null,
  711. deviceListSelect: null,
  712. });
  713. },
  714. // {"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",
  715. /// deviceList
  716. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  717. /// clientType
  718. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  719. ///新添加蓝牙设备
  720. addConnectBlueDevice(newDevice) {
  721. var that = this;
  722. // 同一个设备
  723. var deviceList = that.data.deviceList;
  724. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  725. if (tempList && tempList.length > 0) {
  726. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  727. };
  728. console.log("添加蓝牙设备:", newDevice);
  729. deviceList.unshift({
  730. /// 蓝牙ble连接
  731. connectType: 1,
  732. deviceId: newDevice.deviceId,
  733. name: newDevice.name,
  734. state: "online",
  735. ProdModel: newDevice.ProdModel || newDevice.clientType,
  736. clientType: newDevice.clientType || newDevice.ProdModel,
  737. devName: newDevice.name,
  738. mac: newDevice.mac,
  739. img: newDevice.img,
  740. });
  741. ///在线排序前面,wifi设备排序前面
  742. that.updateDeviceList(deviceList, false, false);
  743. var indexPage = that.data.indexPage;
  744. console.log("gadsfqwerqwerqrqr==ccc==");
  745. if (indexPage != 0) {
  746. that.setData({
  747. indexPage: 0,
  748. deviceListSelect: 0,
  749. autoConnected: true,
  750. });
  751. } else {
  752. that.setData({
  753. deviceListSelect: 0,
  754. autoConnected: true,
  755. });
  756. }
  757. },
  758. ///新添加wifi设备
  759. async addConnectWifiDevice(newDevice) {
  760. var that = this;
  761. // 同一个设备
  762. var deviceList = that.data.deviceList;
  763. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  764. if (tempList && tempList.length > 0) {
  765. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  766. };
  767. console.log("添加Wifi设备:" + JSON.stringify(newDevice));
  768. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  769. deviceList.unshift({
  770. /// Wifi连接
  771. connectType: 3,
  772. deviceId: newDevice.deviceId,
  773. name: "猫王小王子OTR-X",
  774. state: "online",
  775. devName: "",
  776. // devName: "猫王小王子OTR-X",
  777. mac: newDevice.deviceId,
  778. image: "./../../img/min.png",
  779. });
  780. ///在线排序前面,wifi设备排序前面
  781. that.updateDeviceList(deviceList, false, false);
  782. var indexPage = that.data.indexPage;
  783. if (indexPage != 0) {
  784. that.setData({
  785. indexPage: 0,
  786. });
  787. }
  788. ///断开蓝牙连接
  789. // await BtHelper.getInstance().disconnect();
  790. that.actionDevice(0);
  791. },
  792. ///更新列表排序
  793. updateDeviceList(deviceList, isInit, closeAllBlue) {
  794. var that = this;
  795. if (strings.isEmpty(deviceList)) {
  796. deviceList = that.data.deviceList;
  797. }
  798. if (deviceList.length <= 0) {
  799. return;
  800. }
  801. var finalList = [];
  802. if (isInit) {
  803. deviceList[0].state = "offline";
  804. }
  805. /// 让所有蓝牙设备离线
  806. if (closeAllBlue) {
  807. deviceList.forEach(element => {
  808. if (element.connectType != 3) {
  809. element.state = "offline";
  810. }
  811. });
  812. }
  813. var isFirstOnline = false;
  814. if (deviceList[0].state == "online") {
  815. isFirstOnline = true;
  816. finalList.push(deviceList[0]);
  817. }
  818. ///区分在线和离线
  819. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  820. if (isInit) {
  821. deviceList[i].state = "offline";
  822. }
  823. }
  824. var onLineList = [];
  825. var onNoLineList = [];
  826. ///添加在线的
  827. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  828. var device = deviceList[i];
  829. if (device.state == "online") {
  830. onLineList.push(device)
  831. }
  832. }
  833. ///添加离线的
  834. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  835. var device = deviceList[i];
  836. if (device.state != "online") {
  837. onNoLineList.push(device)
  838. }
  839. }
  840. // 区分在线wifi和蓝牙 wifi在前 离线在后
  841. var onLineWifiList = [];
  842. var onLineBlueList = [];
  843. ///添加在线wifi
  844. onLineList.forEach(element => {
  845. if (element.connectType == 3) {
  846. onLineWifiList.push(element);
  847. }
  848. });
  849. ///添加在线蓝牙
  850. onLineList.forEach(element => {
  851. if (element.connectType != 3) {
  852. onLineBlueList.push(element)
  853. }
  854. });
  855. finalList = finalList.concat(onLineWifiList);
  856. finalList = finalList.concat(onLineBlueList);
  857. ///只需要蓝牙和wifi在线的
  858. let mDeviceList = []
  859. mDeviceList = mDeviceList.concat(onLineWifiList);
  860. mDeviceList = mDeviceList.concat(onLineBlueList);
  861. getApp().globalData.mDeviceList = mDeviceList;
  862. ///区分离线wifi和蓝牙 wifi在前 离线在后
  863. var onNoLineWifiList = [];
  864. var onNoLineBlueList = [];
  865. ///添加离线wifi
  866. onNoLineList.forEach(element => {
  867. if (element.connectType == 3) {
  868. onNoLineWifiList.push(element)
  869. }
  870. });
  871. ///添加离线蓝牙
  872. onNoLineList.forEach(element => {
  873. if (element.connectType != 3) {
  874. onNoLineBlueList.push(element)
  875. }
  876. });
  877. finalList = finalList.concat(onNoLineWifiList);
  878. finalList = finalList.concat(onNoLineBlueList);
  879. store.setStore("deviceList", finalList);
  880. that.setData({
  881. deviceList: finalList
  882. });
  883. },
  884. ///******************************* 可折叠 ********************************************///
  885. onUserInfoLoad() {
  886. var that = this;
  887. var userInfo = wx.getStorageSync("userInfo") || "";
  888. if (!strings.isEmpty(userInfo)) {
  889. var phone = userInfo.phone;
  890. if (!phone) {
  891. return;
  892. };
  893. var nickName = userInfo.nickname || "";
  894. var greeting = that.getGreetBuNickName(nickName);
  895. // 获取缓存的频道数据
  896. var channelData = wx.getStorageSync("channelData") || "";
  897. if (!strings.isEmpty(channelData)) {
  898. that.setData({
  899. channelData: channelData,
  900. greeting: greeting,
  901. nickName: nickName,
  902. userPic: userInfo.headUrl || "",
  903. isLogin: true,
  904. })
  905. } else {
  906. that.setData({
  907. greeting: greeting,
  908. nickName: nickName,
  909. userPic: userInfo.headUrl || "",
  910. isLogin: true,
  911. });
  912. }
  913. } else {
  914. var greeting = that.getGreetBuNickName("");
  915. that.setData({
  916. greeting: greeting,
  917. })
  918. }
  919. },
  920. getGreetBuNickName(nickName) {
  921. var greeting = timeUtil.getGreet();
  922. if (nickName != "" && nickName != "未登录") {
  923. greeting = greeting + ',' + nickName;;
  924. }
  925. return greeting;
  926. },
  927. onBannerLoad() {
  928. var that = this;
  929. // var bannerList = wx.getStorageSync("homeBanner") || [];
  930. // if (that.data.bannerList.length > 0) {
  931. // that.setData({
  932. // bannerList: bannerList
  933. // });
  934. // }
  935. },
  936. onBannerShow() {
  937. var that = this;
  938. // getBanner({}).then((res) => {
  939. // that.setData({
  940. // bannerList: res
  941. // });
  942. // wx.setStorageSync("homeBanner", res);
  943. // })
  944. },
  945. stopIntervalId1: function () {
  946. var that = this;
  947. if (!strings.isEmpty(that.data.intervalId1)) {
  948. clearInterval(that.data.intervalId1);
  949. that.data.intervalId1 = null;
  950. }
  951. },
  952. stopIntervalId2: function () {
  953. var that = this;
  954. if (!strings.isEmpty(that.data.intervalId2)) {
  955. clearInterval(that.data.intervalId2);
  956. that.data.intervalId2 = null;
  957. }
  958. },
  959. ///销毁蓝牙
  960. onUnload() {
  961. var that = this;
  962. that.stopIntervalId1();
  963. that.stopIntervalId2();
  964. if (!strings.isEmpty(that.data.intervalId)) {
  965. clearInterval(that.data.intervalId);
  966. that.data.intervalId = null;
  967. }
  968. BtHelper.getInstance().disconnect();
  969. if (app.globalData.client === null) {
  970. return;
  971. };
  972. app.globalData.client.end(true);
  973. app.globalData.client.end(true);
  974. app.globalData.client = null;
  975. },
  976. })
  977. // that.stopIntervalId2();
  978. // that.data.intervalId2 = setInterval(() => {
  979. // clearInterval(str);
  980. // that.subscribeCurrDevice();
  981. // }, 500);
  982. // tryConnectBle() {
  983. // var hasBle = false;
  984. // var hasConnectBle = false;
  985. // var hasWifi = false;
  986. // var bleDevice;
  987. // let that = this
  988. // that.data.deviceList.forEach(device => {
  989. // if (device.connectType == 1) {
  990. // hasBle = true
  991. // if (device.state == "online") {
  992. // hasConnectBle = true
  993. // }
  994. // bleDevice = device
  995. // } else if (device.connectType == 3 && device.state == "online") {
  996. // // wifi
  997. // hasWifi = true;
  998. // }
  999. // });
  1000. // if (!hasWifi && !hasConnectBle && hasBle) {
  1001. // // 没有wifi 没有连接的ble 有未连接的ble
  1002. // console.log("去连接蓝牙")
  1003. // let bt_helper = BtHelper.getInstance()
  1004. // bt_helper.initBluetooth(function (adapterState, hasPermission) {
  1005. // console.log("蓝牙状态", adapterState, hasPermission)
  1006. // if (adapterState && hasPermission) {
  1007. // bt_helper.connect(bleDevice, function (data) {
  1008. // console.log("连接成功:", data)
  1009. // if (data == true) {
  1010. // // 蓝牙模式
  1011. // bleDevice.connectType = 1
  1012. // // 在线
  1013. // bleDevice.state = 'online'
  1014. // bleDevice.ProdModel = bleDevice.clientType
  1015. // that.addConnectBlueDevice(connectDevice);
  1016. // }
  1017. // })
  1018. // } else {}
  1019. // })
  1020. // }
  1021. // },
  1022. // 去掉此功能,先留着吧
  1023. // const other= {
  1024. // "url": "",
  1025. // "media_data": "",
  1026. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  1027. // "timestamp": `${Math.round(new Date() / 1000)}`,
  1028. // "channel_id": `${res[1].channelNum}`,
  1029. // "order": "",
  1030. // "resource_from": "",
  1031. // "songAlbumID":"",
  1032. // "version":3,
  1033. // "is_debug": app.globalData.is_debug
  1034. // };
  1035. // app.PubMsg({
  1036. // type: "play",
  1037. // DstDeviceName: that.getThisDeviceID(),
  1038. // other
  1039. // });