index.js 35 KB

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