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