index.js 32 KB

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