index.js 34 KB

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