index.js 33 KB

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