index.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 lexin_util from '../../utils/lexin/util.js';
  12. import lexin_jump 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 = lexin_util.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 = lexin_util.getResponseByDeviceMacId(deviceMacId);
  366. app.unsubscribe(pubResponse);
  367. };
  368. var deviceId = device.deviceId;
  369. deviceMacId = lexin_util.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 = lexin_util.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 = lexin_util.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 = lexin_util.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 = lexin_util.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. closeBlueResetOffline(isInit, closeAllBlue) {
  806. var that = this;
  807. var deviceList = that.data.deviceList;
  808. updateDeviceList(deviceList, isInit, closeAllBlue);
  809. },
  810. /// 更新列表排序
  811. updateDeviceList(deviceList, isInit, closeAllBlue) {
  812. var finalList = store.updateDeviceList(deviceList, isInit, closeAllBlue);
  813. if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
  814. that.setData({
  815. deviceList: finalList,
  816. });
  817. }
  818. },
  819. // 关于我们
  820. jumpToAboutUs() {
  821. lexin_jump.toAboutUs();
  822. },
  823. ///******************************* 可折叠 ********************************************///
  824. onUserInfoLoad() {
  825. var that = this;
  826. var userInfo = wx.getStorageSync("userInfo") || "";
  827. if (!strings.isEmpty(userInfo)) {
  828. var phone = userInfo.phone;
  829. if (!phone) {
  830. return;
  831. };
  832. var nickName = userInfo.nickname || "";
  833. var greeting = that.getGreetBuNickName(nickName);
  834. // 获取缓存的频道数据
  835. var channelData = wx.getStorageSync("channelData") || "";
  836. if (!strings.isEmpty(channelData)) {
  837. that.setData({
  838. channelData: channelData,
  839. greeting: greeting,
  840. nickName: nickName,
  841. userPic: userInfo.headUrl || "",
  842. isLogin: true,
  843. })
  844. } else {
  845. that.setData({
  846. greeting: greeting,
  847. nickName: nickName,
  848. userPic: userInfo.headUrl || "",
  849. isLogin: true,
  850. });
  851. }
  852. } else {
  853. var greeting = that.getGreetBuNickName("");
  854. that.setData({
  855. greeting: greeting,
  856. })
  857. }
  858. },
  859. getGreetBuNickName(nickName) {
  860. var greeting = timeUtil.getGreet();
  861. if (nickName != "" && nickName != "未登录") {
  862. greeting = greeting + ',' + nickName;;
  863. }
  864. return greeting;
  865. },
  866. onBannerLoad() {
  867. var that = this;
  868. // var bannerList = wx.getStorageSync("homeBanner") || [];
  869. // if (that.data.bannerList.length > 0) {
  870. // that.setData({
  871. // bannerList: bannerList
  872. // });
  873. // }
  874. },
  875. onBannerShow() {
  876. var that = this;
  877. // getBanner({}).then((res) => {
  878. // that.setData({
  879. // bannerList: res
  880. // });
  881. // wx.setStorageSync("homeBanner", res);
  882. // })
  883. },
  884. stopIntervalId1: function () {
  885. var that = this;
  886. if (!strings.isEmpty(that.data.intervalId1)) {
  887. clearInterval(that.data.intervalId1);
  888. that.data.intervalId1 = null;
  889. }
  890. },
  891. stopIntervalId2: function () {
  892. var that = this;
  893. if (!strings.isEmpty(that.data.intervalId2)) {
  894. clearInterval(that.data.intervalId2);
  895. that.data.intervalId2 = null;
  896. }
  897. },
  898. ///销毁蓝牙
  899. onUnload() {
  900. var that = this;
  901. that.stopIntervalId1();
  902. that.stopIntervalId2();
  903. if (!strings.isEmpty(that.data.intervalId)) {
  904. clearInterval(that.data.intervalId);
  905. that.data.intervalId = null;
  906. }
  907. BtHelper.getInstance().disconnect();
  908. if (app.globalData.client === null) {
  909. return;
  910. };
  911. app.globalData.client.end(true);
  912. app.globalData.client.end(true);
  913. app.globalData.client = null;
  914. },
  915. })
  916. // that.stopIntervalId2();
  917. // that.data.intervalId2 = setInterval(() => {
  918. // clearInterval(str);
  919. // that.subscribeCurrDevice();
  920. // }, 500);
  921. // tryConnectBle() {
  922. // var hasBle = false;
  923. // var hasConnectBle = false;
  924. // var hasWifi = false;
  925. // var bleDevice;
  926. // let that = this
  927. // that.data.deviceList.forEach(device => {
  928. // if (device.connectType == 1) {
  929. // hasBle = true
  930. // if (device.state == "online") {
  931. // hasConnectBle = true
  932. // }
  933. // bleDevice = device
  934. // } else if (device.connectType == 3 && device.state == "online") {
  935. // // wifi
  936. // hasWifi = true;
  937. // }
  938. // });
  939. // if (!hasWifi && !hasConnectBle && hasBle) {
  940. // // 没有wifi 没有连接的ble 有未连接的ble
  941. // console.log("去连接蓝牙")
  942. // let bt_helper = BtHelper.getInstance()
  943. // bt_helper.initBluetooth(function (adapterState, hasPermission) {
  944. // console.log("蓝牙状态", adapterState, hasPermission)
  945. // if (adapterState && hasPermission) {
  946. // bt_helper.connect(bleDevice, function (data) {
  947. // console.log("连接成功:", data)
  948. // if (data == true) {
  949. // // 蓝牙模式
  950. // bleDevice.connectType = 1
  951. // // 在线
  952. // bleDevice.state = 'online'
  953. // bleDevice.ProdModel = bleDevice.clientType
  954. // that.addConnectBlueDevice(connectDevice);
  955. // }
  956. // })
  957. // } else {}
  958. // })
  959. // }
  960. // },
  961. // 去掉此功能,先留着吧
  962. // const other= {
  963. // "url": "",
  964. // "media_data": "",
  965. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  966. // "timestamp": `${Math.round(new Date() / 1000)}`,
  967. // "channel_id": `${res[1].channelNum}`,
  968. // "order": "",
  969. // "resource_from": "",
  970. // "songAlbumID":"",
  971. // "version":3,
  972. // "is_debug": app.globalData.is_debug
  973. // };
  974. // app.PubMsg({
  975. // type: "play",
  976. // DstDeviceName: that.getThisDeviceID(),
  977. // other
  978. // });