index.js 35 KB

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