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次 payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  243. var isUpdate = false;
  244. var deviceList = that.data.deviceList;
  245. if (!strings.isEmpty(deviceList)) {
  246. for (var i = 0; i < deviceList.length; i++) {
  247. if (payloads.SrcDeviceName) {
  248. var deviceId = deviceList[i].deviceId;
  249. var splitDeviceId = deviceId.split("BLUFI_");
  250. if (splitDeviceId.length == 2) {
  251. var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
  252. if (index !== -1 && (deviceList[i].ProdModel != payloads.other.ProdModel || deviceList[i].devName != payloads.other.devName)) {
  253. isUpdate = true;
  254. deviceList[i].ProdModel = payloads.other.ProdModel;
  255. deviceList[i].devName = payloads.other.devName;
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. ///数据有更新
  263. if (isUpdate) {
  264. that.updateDeviceList(deviceList, false);
  265. }
  266. } else if (payloads.type === "battery" && payloads.other) {
  267. that.setData({
  268. battery: that._battery(payloads.other.battery),
  269. })
  270. }
  271. break;
  272. default:
  273. }
  274. },
  275. // 格式化电量
  276. _battery(battery) {
  277. let _battery = 0;
  278. if (battery < 20) {
  279. _battery = 0
  280. } else if (20 <= battery && battery < 40) {
  281. _battery = 1
  282. } else if (40 <= battery && battery < 60) {
  283. _battery = 2
  284. } else if (60 <= battery && battery < 80) {
  285. _battery = 3
  286. } else if (80 <= battery && battery <= 100) {
  287. _battery = 4
  288. } else if (battery > 100) {
  289. _battery = 5
  290. };
  291. return _battery
  292. },
  293. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  294. ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
  295. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  296. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  297. onlineDevice(payloads) {
  298. // 设置在线状态
  299. var that = this;
  300. // console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  301. ///是否更新过在线离线状态
  302. var isUpdate = false;
  303. var deviceList = that.data.deviceList;
  304. if (!strings.isEmpty(deviceList)) {
  305. for (var i = 0; i < deviceList.length; i++) {
  306. if (payloads.uuid) {
  307. var deviceId = deviceList[i].deviceId;
  308. var splitDeviceId = deviceId.split("BLUFI_");
  309. if (splitDeviceId.length == 2) {
  310. var index = payloads.uuid.indexOf(splitDeviceId[1]);
  311. if (index !== -1) {
  312. if (deviceList[i].state != payloads.state) {
  313. isUpdate = true;
  314. deviceList[i].state = payloads.state;
  315. break;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. ///数据有更新
  323. if (isUpdate) {
  324. that.updateDeviceList(deviceList, false);
  325. }
  326. ///当前没有连接设备,则去连接第一个wifi设备
  327. var deviceListIndex = that.data.deviceListIndex;
  328. if (deviceListIndex === null) {
  329. var list = that.data.deviceList;
  330. for (var i = 0; i < list.length; i++) {
  331. if (list[i].state === "online" && list[i].connectType == 3) {
  332. if (that.data.isLogin) {
  333. that.actionDevice(i);
  334. }
  335. break;
  336. }
  337. }
  338. } else {
  339. // 当前播放设备离线
  340. if (that.data.deviceList.length > deviceListIndex && that.data.deviceList[deviceListIndex].state !== "online") {
  341. that.setData({
  342. actionIndex: null,
  343. deviceListIndex: null,
  344. });
  345. };
  346. }
  347. },
  348. // 订阅设备在线状态
  349. subscribeDevicesStatus() {
  350. var that = this;
  351. var deviceList = that.data.deviceList;
  352. if (!strings.isEmpty(deviceList)) {
  353. for (var i = 0; i < deviceList.length; i++) {
  354. if (deviceList[i].connectType == 3) {
  355. let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  356. app.subscribe(topic);
  357. }
  358. }
  359. }
  360. },
  361. subscribeCurrDevice() {
  362. var that = this;
  363. if (!(app.globalData.client && app.globalData.client.connected)) {
  364. console.log("未连接MQTT服务器");
  365. const str = setInterval(() => {
  366. clearInterval(str);
  367. that.subscribeCurrDevice();
  368. }, 500);
  369. return;
  370. };
  371. var deviceList = that.data.deviceList;
  372. var deviceListIndex = that.data.deviceListIndex;
  373. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  374. return
  375. };
  376. var device = that.data.deviceList[deviceListIndex];
  377. var tempDeviceId = device.deviceId;
  378. ///删除当前选中这个
  379. deviceList.splice(deviceListIndex, 1);
  380. ///添加到第一个去
  381. deviceList.unshift(device);
  382. that.setData({
  383. deviceListIndex: 0,
  384. autoConnected: true,
  385. deviceList: deviceList,
  386. });
  387. var topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
  388. app.subscribe(topic);
  389. const obj = {
  390. DstDeviceName: that.getThisDeviceID(tempDeviceId)
  391. };
  392. app.PubMsg({
  393. type: "get_dev_info",
  394. ...obj
  395. });
  396. },
  397. actionMusic(e) {
  398. var that = this;
  399. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  400. return;
  401. };
  402. if (that.data.deviceListIndex === null) {
  403. wx.showToast({
  404. title: '请选择设备',
  405. icon: "none"
  406. })
  407. return;
  408. };
  409. that.setData({
  410. actionIndex: e.currentTarget.dataset.index
  411. });
  412. var deviceList = that.data.deviceList;
  413. var deviceListIndex = that.data.deviceListIndex;
  414. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  415. return
  416. };
  417. var deviceId = deviceList[deviceListIndex].deviceId;
  418. const other = {
  419. "url": "",
  420. "media_data": "",
  421. "user_id": `${app.globalData.userInfo.deviceUid}`,
  422. "timestamp": `${Math.round(new Date() / 1000)}`,
  423. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  424. "order": "",
  425. "resource_from": "",
  426. "songAlbumID": "",
  427. "version": 3,
  428. "is_debug": app.globalData.is_debug
  429. };
  430. app.PubMsg({
  431. type: "play",
  432. DstDeviceName: that.getThisDeviceID(deviceId),
  433. other
  434. });
  435. },
  436. getThisDeviceID(deviceId) {
  437. return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
  438. },
  439. getchannelData(clientType) {
  440. var that = this;
  441. var deviceList = that.data.deviceList;
  442. var deviceListIndex = that.data.deviceListIndex;
  443. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  444. return;
  445. };
  446. var deviceId = deviceList[deviceListIndex].deviceId;
  447. listByDevice({
  448. clientType: clientType,
  449. unShowLoad: true,
  450. }).then((res) => {
  451. that.setData({
  452. channelData: res
  453. });
  454. // 接收设备当前播放状态
  455. const obj = {
  456. DstDeviceName: that.getThisDeviceID(deviceId)
  457. }
  458. app.PubMsg({
  459. type: "get_position",
  460. ...obj
  461. });
  462. wx.setStorageSync("channelData", res);
  463. })
  464. },
  465. ///去频道详情
  466. onTapToChannel() {
  467. var that = this;
  468. if (that.data.channelData.length > that.data.actionIndex) {
  469. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  470. wx.navigateTo({
  471. url: './../channelDetails/channelDetails'
  472. });
  473. }
  474. },
  475. onTapIndex(e) {
  476. var that = this;
  477. var index = e.currentTarget.dataset.index;
  478. var indexPage = that.data.indexPage;
  479. if (indexPage != index) {
  480. that.setData({
  481. indexPage: index,
  482. });
  483. }
  484. },
  485. goMeAbout() {
  486. wx.navigateTo({
  487. url: './../about/about',
  488. })
  489. },
  490. onTapLogin() {
  491. var that = this;
  492. ///退出登录
  493. if (that.data.isLogin) {
  494. that.logOut();
  495. }
  496. // 登录
  497. else {
  498. route_util.jump(route_constant.login);
  499. }
  500. },
  501. ///退出登录
  502. logOut() {
  503. var that = this;
  504. ///有设备在线被选中,则让它不被选择
  505. var deviceListIndex = that.data.deviceListIndex;
  506. if (deviceListIndex != null) {
  507. var deviceList = that.data.deviceList;
  508. if (deviceList.length > deviceListIndex) {
  509. if (deviceList[deviceListIndex].connectType == 3) {
  510. if (that.data.thisDeviceMac !== null) {
  511. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  512. that.setData({
  513. actionIndex: null,
  514. deviceListIndex: null,
  515. });
  516. };
  517. }
  518. }
  519. }
  520. wx.removeStorageSync('userInfo');
  521. wx.removeStorageSync('token');
  522. var nickName = "未登录";
  523. var greeting = that.getGreetBuNickName(nickName);
  524. that.setData({
  525. isLogin: false,
  526. greeting: greeting,
  527. nickName: nickName,
  528. userPic: "./../../img/head_pic.png",
  529. });
  530. },
  531. ///添加设备
  532. addDevice() {
  533. var that = this;
  534. if (!that.data.isLogin) {
  535. route_util.jump(route_constant.login);
  536. return;
  537. }
  538. ///跳转设备列表
  539. route_util.jump(route_constant.deviceList);
  540. },
  541. ///点击item
  542. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  543. onTapItem(e) {
  544. var that = this;
  545. if (!that.data.isLogin) {
  546. route_util.jump(route_constant.login);
  547. return;
  548. }
  549. var item = e.currentTarget.dataset.item;
  550. console.log("点击item:", item)
  551. var connectType = item.connectType;
  552. // wifi只支持在线点击
  553. if (connectType == 3 && item.state === "online") {
  554. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  555. that.goWake();
  556. } else {
  557. that.actionDevice(e.currentTarget.dataset.index)
  558. }
  559. return;
  560. }
  561. if (item.connectType != "1") {
  562. return
  563. }
  564. ///去蓝牙连接处理
  565. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  566. // 蓝牙才能连接
  567. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  568. } else if (item.state === "offline") {
  569. console.log("去连接蓝牙")
  570. } else {
  571. ///item
  572. // {"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 妙播收音机"}
  573. item.name = item.devName;
  574. BtHelper.getInstance().connect(item, function (data) {
  575. if (data) {
  576. that.addConnectBlueDevice(item);
  577. }
  578. });
  579. }
  580. },
  581. actionDevice(index) {
  582. var that = this;
  583. if (!that.data.isLogin) {
  584. return;
  585. }
  586. if (that.data.deviceList.length <= index) {
  587. return;
  588. };
  589. const device = that.data.deviceList[index];
  590. if (device.state !== "online") {
  591. return;
  592. };
  593. // 取消订阅
  594. if (that.data.thisDeviceMac !== null) {
  595. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  596. };
  597. that.setData({
  598. deviceListIndex: index,
  599. autoConnected: true,
  600. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
  601. });
  602. // app.PubMsg({
  603. // type: "get_dev_info",
  604. // DstDeviceName: that.getThisDeviceID()
  605. // });
  606. that.subscribeCurrDevice();
  607. },
  608. goWake() {
  609. var that = this;
  610. if (that.data.deviceListIndex === null) {
  611. return;
  612. };
  613. var deviceList = that.data.deviceList;
  614. var deviceListIndex = that.data.deviceListIndex;
  615. var device = deviceList[deviceListIndex];
  616. wx.navigateTo({
  617. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  618. });
  619. },
  620. ///点击banner事件
  621. onTapBanner() {
  622. var that = this;
  623. var item = e.currentTarget.dataset.item;
  624. },
  625. ///删除当前设备
  626. deleteDevice(e) {
  627. var that = this;
  628. var index = e.currentTarget.dataset.index;
  629. wx.showModal({
  630. title: '确定删除?',
  631. success: function (res) {
  632. if (res.confirm) {
  633. if (that.data.deviceList[index].connectType == 3) {
  634. that.cancelWifi(index, false);
  635. } else {
  636. that.cancelBlue(index, false);
  637. }
  638. }
  639. }
  640. });
  641. },
  642. ///是否是同一个蓝牙
  643. isTheSameBlue(connectDevice) {
  644. var that = this;
  645. var deviceListIndex = that.data.deviceListIndex;
  646. if (deviceListIndex == null) {
  647. return false;
  648. }
  649. var deviceList = that.data.deviceList;
  650. if (deviceList.length > deviceListIndex) {
  651. if (deviceList[deviceListIndex].connectType == 3) {
  652. return false;
  653. } else {
  654. if (deviceList[deviceListIndex].deviceId == connectDevice.deviceId) {
  655. return true;
  656. } else {
  657. return false;
  658. }
  659. }
  660. } else {
  661. return false;
  662. }
  663. },
  664. ///断开当前的
  665. async cancelCurrent() {
  666. var that = this;
  667. var deviceListIndex = that.data.deviceListIndex;
  668. if (deviceListIndex == null) {
  669. return;
  670. }
  671. var deviceList = that.data.deviceList;
  672. if (deviceList.length > deviceListIndex) {
  673. if (deviceList[deviceListIndex].connectType == 3) {
  674. await that.cancelWifi(deviceListIndex, true);
  675. } else {
  676. await that.cancelBlue(deviceListIndex, true);
  677. }
  678. }
  679. },
  680. /// 断开蓝牙连接
  681. async cancelBlue(index, onlyCancel) {
  682. var that = this;
  683. // await BtHelper.getInstance().disconnect();
  684. if (onlyCancel) {
  685. return;
  686. }
  687. const id = that.data.deviceList[index].deviceId;
  688. const deviceList = that.data.deviceList.filter((item, i) => {
  689. return id !== item.deviceId
  690. });
  691. store.setStore("deviceList", deviceList);
  692. that.setData({
  693. deviceList,
  694. });
  695. // 当前没有设备
  696. // if (deviceList.length === 0) {
  697. that.setData({
  698. deviceListIndex: null,
  699. });
  700. // }
  701. },
  702. /// 断开连接wifi
  703. async cancelWifi(index, onlyCancel) {
  704. var that = this;
  705. // 取消订阅
  706. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}/user/pub_response`);
  707. if (onlyCancel) {
  708. return;
  709. }
  710. const id = that.data.deviceList[index].deviceId;
  711. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  712. const deviceList = that.data.deviceList.filter((item, i) => {
  713. return id !== item.deviceId
  714. });
  715. store.setStore("deviceList", deviceList);
  716. that.setData({
  717. deviceList,
  718. thisDeviceMac: null
  719. });
  720. // 当前没有设备
  721. // if (deviceList.length === 0) {
  722. that.setData({
  723. actionIndex: null,
  724. deviceListIndex: null,
  725. });
  726. // }
  727. if (that.data.deviceListIndex === null) {
  728. return
  729. };
  730. if (index === that.data.deviceListIndex) {
  731. let index_ = null;
  732. deviceList.map((v, index) => {
  733. if (v.state === "online" && index_ === null) {
  734. index_ = index;
  735. }
  736. });
  737. if (index_ !== null) {
  738. that.actionDevice(index_);
  739. } else {
  740. that.setData({
  741. actionIndex: null,
  742. deviceListIndex: null,
  743. });
  744. };
  745. } else {
  746. deviceList.map((v, index) => {
  747. if (v.name === name) {
  748. that.setData({
  749. deviceListIndex: index,
  750. autoConnected: true,
  751. });
  752. }
  753. });
  754. }
  755. },
  756. // {"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",
  757. /// deviceList
  758. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  759. /// clientType
  760. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  761. ///新添加蓝牙设备
  762. addConnectBlueDevice(newDevice) {
  763. var that = this;
  764. // 同一个设备
  765. var deviceList = that.data.deviceList;
  766. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  767. if (tempList && tempList.length > 0) {
  768. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  769. };
  770. console.log("添加蓝牙设备:", newDevice);
  771. deviceList.unshift({
  772. /// 蓝牙ble连接
  773. connectType: 1,
  774. deviceId: newDevice.deviceId,
  775. name: newDevice.name,
  776. state: "online",
  777. ProdModel: newDevice.ProdModel || newDevice.clientType,
  778. clientType: newDevice.clientType || newDevice.ProdModel,
  779. devName: newDevice.name,
  780. mac: newDevice.mac,
  781. img: newDevice.img,
  782. });
  783. ///在线排序前面,wifi设备排序前面
  784. that.updateDeviceList(deviceList, false);
  785. var indexPage = that.data.indexPage;
  786. if (indexPage != 0) {
  787. that.setData({
  788. indexPage: 0,
  789. deviceListIndex: 0,
  790. autoConnected: true,
  791. });
  792. } else {
  793. that.setData({
  794. deviceListIndex: 0,
  795. autoConnected: true,
  796. });
  797. }
  798. },
  799. ///新添加wifi设备
  800. async addConnectWifiDevice(newDevice) {
  801. var that = this;
  802. // 同一个设备
  803. var deviceList = that.data.deviceList;
  804. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  805. if (tempList && tempList.length > 0) {
  806. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  807. };
  808. console.log("添加Wifi设备:" + JSON.stringify(newDevice));
  809. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  810. deviceList.unshift({
  811. /// Wifi连接
  812. connectType: 3,
  813. deviceId: newDevice.deviceId,
  814. name: "猫王小王子OTR-X",
  815. state: "online",
  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. // });