index.js 34 KB

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