index.js 35 KB

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