index.js 34 KB

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