index.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 route_constant from '../../utils/route_constant.js';
  9. import route_util from '../../utils/route_util.js';
  10. import {
  11. BtHelper
  12. } from '../../devices/bt_helper.js';
  13. Page({
  14. data: {
  15. nvabarData: {
  16. showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
  17. title: 'OhPlay', //导航栏 中间的标题
  18. },
  19. ///下午好
  20. isLogin: false,
  21. greeting: "",
  22. bannerList: [],
  23. autoplay: true,
  24. interval: 3000, // 切换时间间隔
  25. duration: 500, // 滑动动画时长
  26. circular: true, // 衔接滑动
  27. indexPage: 0,
  28. ///是否展示频道
  29. showChannel: false,
  30. ////首页
  31. navBarHeight: app.globalData.navBarHeight,
  32. MenuButtonheight: app.globalData.MenuButtonheight,
  33. MenuButtonTop: app.globalData.MenuButtonTop,
  34. actionIndex: null,
  35. luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
  36. channelData: [],
  37. deviceList: [],
  38. deviceListIndex: null,
  39. isOneLoading: true,
  40. uid: null,
  41. isSetWake: false,
  42. thisDeviceMac: null,
  43. battery: 4, // 0≤电量<20,0格
  44. ////我的界面
  45. loginStatus: true,
  46. nickName: "未登录",
  47. userPic: './../../img/head_pic.png',
  48. intervalId: null,
  49. },
  50. onLoad(options) {
  51. var that = this;
  52. that.onDeviceLoad();
  53. that.onUserInfoLoad();
  54. that.onBannerLoad();
  55. },
  56. onShow() {
  57. var that = this;
  58. that.onHomeShow();
  59. that.onBannerShow();
  60. },
  61. onHomeShow() {
  62. var that = this;
  63. var userInfo = wx.getStorageSync("userInfo") || "";
  64. if (!strings.isEmpty(userInfo)) {
  65. // 获取设备本地数据
  66. if (app.globalData.newDeviceId) {
  67. that.addNewDeviceId();
  68. } else if (app.globalData.client == null) {
  69. app.connect();
  70. } else if (that.data.deviceListIndex == null && that.data.deviceList.length > 0) {
  71. that.actionDevice(0)
  72. }
  73. } else {
  74. if (app.globalData.client == null) {
  75. app.connect();
  76. }
  77. }
  78. },
  79. tryConnectBle() {
  80. var hasBle = false;
  81. var hasConnectBle = false;
  82. var hasWifi = false;
  83. var bleDevice;
  84. let that = this
  85. this.data.deviceList.forEach(device => {
  86. if (device.connectType == 1) {
  87. hasBle = true
  88. if (device.state == "online") {
  89. hasConnectBle = true
  90. }
  91. bleDevice = device
  92. } else if (device.connectType == 3 && device.state == "online") {
  93. // wifi
  94. hasWifi = true;
  95. }
  96. });
  97. if (!hasWifi && !hasConnectBle && hasBle) {
  98. // 没有wifi 没有连接的ble 有未连接的ble
  99. console.log("去连接蓝牙")
  100. let bt_helper = BtHelper.getInstance()
  101. bt_helper.initBluetooth(function (adapterState, hasPermission) {
  102. console.log("蓝牙状态", adapterState, hasPermission)
  103. if (adapterState && hasPermission) {
  104. bt_helper.connect(bleDevice, function (data) {
  105. console.log("连接成功:", data)
  106. if (data == true) {
  107. // 蓝牙模式
  108. bleDevice.connectType = 1
  109. // 在线
  110. bleDevice.state = 'online'
  111. bleDevice.ProdModel = bleDevice.clientType
  112. that.addConnectBlueDevice(connectDevice);
  113. }
  114. })
  115. } else {}
  116. })
  117. }
  118. },
  119. onDeviceLoad() {
  120. var that = this;
  121. BtHelper.getInstance().initBluetoothAdapter();
  122. var deviceList = wx.getStorageSync("deviceList") || "";
  123. if (!strings.isEmpty(deviceList)) {
  124. var list = JSON.parse(deviceList);
  125. that.updateDeviceList(list, true);
  126. // that.tryConnectBle()
  127. }
  128. ///监听蓝牙设备
  129. BtHelper.getInstance().getBluetoothDevices();
  130. ///3秒去处理一下
  131. BtHelper.getInstance().startScan(null, null, null);
  132. ///先接收数据
  133. setTimeout(() => {
  134. that.compareList();
  135. ///再秒再对比一次
  136. that.data.intervalId = setInterval(async function () {
  137. that.compareList();
  138. }, 5 * 1000);
  139. }, 3 * 1000);
  140. },
  141. async compareList() {
  142. var that = this;
  143. if (BtHelper.getInstance().getCallBackConnect() == null) {
  144. var compareList = BtHelper.getInstance().getCompareList();
  145. if (compareList.length > 0) {
  146. ///对比在线的蓝牙设备
  147. var isChanged = false;
  148. var tempList = that.data.deviceList;
  149. for (var i = 0; i < tempList.length; i++) {
  150. var tempItem = tempList[i];
  151. if (tempItem.connectType != 3) {
  152. var has = false;
  153. for (var j = 0; j < compareList.length; j++) {
  154. // "state":"online" "offline"
  155. if (tempItem.deviceId === compareList[j].deviceId) {
  156. has = true;
  157. break;
  158. }
  159. }
  160. if (has) {
  161. if (tempItem.state != "online") {
  162. isChanged = true;
  163. tempItem.state = "online";
  164. }
  165. } else {
  166. if (tempItem.state != "offline") {
  167. isChanged = true;
  168. tempItem.state = "offline";
  169. }
  170. }
  171. }
  172. }
  173. if (isChanged) {
  174. that.updateDeviceList(tempList, false);
  175. }
  176. }
  177. BtHelper.getInstance().startScan(null, null, null);
  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. case "message_onoffline":
  230. that.onlineDevice(payloads);
  231. break;
  232. case "message":
  233. // 接收设备播放信息
  234. // if (!that.data.isLogin) {
  235. // return;
  236. // }
  237. ///获取频道数据
  238. ///锁定播放哪一个频道
  239. if (payloads.type === "get_position" && payloads.other) {
  240. let actionIndex = null;
  241. that.data.channelData.map((v, index) => {
  242. if (v.channelNum === payloads.other.channel) {
  243. actionIndex = index;
  244. }
  245. });
  246. that.setData({
  247. actionIndex,
  248. });
  249. }
  250. ///获取播放状态
  251. else if (payloads.type === "play" || payloads.type === "play_state") {
  252. var deviceList = that.data.deviceList;
  253. var deviceListIndex = that.data.deviceListIndex;
  254. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  255. return
  256. };
  257. var deviceId = deviceList[deviceListIndex].deviceId;
  258. // 接收设备当前播放状态
  259. const obj = {
  260. DstDeviceName: that.getThisDeviceID(deviceId)
  261. }
  262. app.PubMsg({
  263. type: "get_position",
  264. ...obj
  265. });
  266. }
  267. ///获取频道列表数据
  268. else if (payloads.type === "get_dev_info") {
  269. // 接收设备当前信息
  270. that.getchannelData(payloads.other.ProdModel);
  271. // 电量
  272. that.setData({
  273. battery: that._battery(payloads.other.Power),
  274. })
  275. // 当前设备木有设置定时
  276. that.setData({
  277. isSetWake: false,
  278. });
  279. payloads.other.alarm.map((v) => {
  280. if (v.enable === "1") {
  281. that.setData({
  282. isSetWake: true
  283. });
  284. }
  285. });
  286. // 更新信息
  287. ///连上就调用2次
  288. var isUpdate = false;
  289. // payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  290. var deviceList = that.data.deviceList;
  291. if (!strings.isEmpty(deviceList)) {
  292. for (var i = 0; i < deviceList.length; i++) {
  293. if (payloads.SrcDeviceName) {
  294. var deviceId = deviceList[i].deviceId;
  295. var splitDeviceId = deviceId.split("BLUFI_");
  296. if (splitDeviceId.length == 2) {
  297. var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
  298. if (index !== -1 && deviceList[i].ProdModel != payloads.other.ProdModel && deviceList[i].devName != payloads.other.devName) {
  299. isUpdate = true;
  300. deviceList[i].ProdModel = payloads.other.ProdModel;
  301. deviceList[i].devName = payloads.other.devName;
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. ///数据有更新
  309. if (isUpdate) {
  310. that.updateDeviceList(deviceList, false);
  311. }
  312. } else if (payloads.type === "battery" && payloads.other) {
  313. that.setData({
  314. battery: that._battery(payloads.other.battery),
  315. })
  316. }
  317. break;
  318. default:
  319. }
  320. },
  321. // 格式化电量
  322. _battery(battery) {
  323. let _battery = 0;
  324. if (battery < 20) {
  325. _battery = 0
  326. } else if (20 <= battery && battery < 40) {
  327. _battery = 1
  328. } else if (40 <= battery && battery < 60) {
  329. _battery = 2
  330. } else if (60 <= battery && battery < 80) {
  331. _battery = 3
  332. } else if (80 <= battery && battery <= 100) {
  333. _battery = 4
  334. } else if (battery > 100) {
  335. _battery = 5
  336. };
  337. return _battery
  338. },
  339. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  340. ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
  341. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  342. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  343. onlineDevice(payloads) {
  344. // 设置在线状态
  345. var that = this;
  346. console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  347. ///是否更新过在线离线状态
  348. var isUpdate = false;
  349. var deviceList = that.data.deviceList;
  350. if (!strings.isEmpty(deviceList)) {
  351. for (var i = 0; i < deviceList.length; i++) {
  352. if (payloads.uuid) {
  353. var deviceId = deviceList[i].deviceId;
  354. var splitDeviceId = deviceId.split("BLUFI_");
  355. if (splitDeviceId.length == 2) {
  356. var index = payloads.uuid.indexOf(splitDeviceId[1]);
  357. if (index !== -1) {
  358. if (deviceList[i].state != payloads.state) {
  359. isUpdate = true;
  360. deviceList[i].state = payloads.state;
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. }
  368. ///数据有更新
  369. if (isUpdate) {
  370. that.updateDeviceList(deviceList, false);
  371. }
  372. ///当前没有连接设备,则去连接第一个wifi设备
  373. var deviceListIndex = that.data.deviceListIndex;
  374. if (deviceListIndex === null) {
  375. var list = that.data.deviceList;
  376. for (var i = 0; i < list.length; i++) {
  377. if (list[i].state === "online" && list[i].connectType == 3) {
  378. that.actionDevice(i);
  379. break;
  380. }
  381. }
  382. } else {
  383. // 当前播放设备离线
  384. if (that.data.deviceList.length > deviceListIndex && that.data.deviceList[deviceListIndex].state !== "online") {
  385. that.setData({
  386. actionIndex: null,
  387. deviceListIndex: null,
  388. });
  389. };
  390. }
  391. },
  392. // 订阅设备在线状态
  393. subscribeDevicesStatus() {
  394. var that = this;
  395. var deviceList = that.data.deviceList;
  396. if (!strings.isEmpty(deviceList)) {
  397. for (var i = 0; i < deviceList.length; i++) {
  398. if (deviceList[i].connectType == 3) {
  399. let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  400. app.subscribe(topic);
  401. }
  402. }
  403. }
  404. },
  405. subscribeCurrDevice() {
  406. var that = this;
  407. if (!(app.globalData.client && app.globalData.client.connected)) {
  408. console.log("未连接MQTT服务器");
  409. const str = setInterval(() => {
  410. clearInterval(str);
  411. that.subscribeCurrDevice();
  412. }, 500);
  413. return;
  414. };
  415. var deviceList = that.data.deviceList;
  416. var deviceListIndex = that.data.deviceListIndex;
  417. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  418. return
  419. };
  420. var tempDevice = that.data.deviceList[deviceListIndex];
  421. var tempDeviceId = tempDevice.deviceId;
  422. ///删除当前选中这个
  423. deviceList.splice(deviceListIndex, 1);
  424. ///添加到第一个去
  425. deviceList.unshift(tempDevice);
  426. that.setData({
  427. deviceListIndex: 0,
  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
  492. }).then((res) => {
  493. that.setData({
  494. channelData: res
  495. });
  496. // 接收设备当前播放状态
  497. const obj = {
  498. DstDeviceName: that.getThisDeviceID(deviceId)
  499. }
  500. app.PubMsg({
  501. type: "get_position",
  502. ...obj
  503. });
  504. wx.setStorageSync("channelData", res);
  505. // 有新设备
  506. if (app.globalData.newDeviceId) {
  507. app.globalData.newDeviceId = null;
  508. return;
  509. }
  510. })
  511. },
  512. ///去频道详情
  513. onTapToChannel() {
  514. var that = this;
  515. if (that.data.channelData.length > that.data.actionIndex) {
  516. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  517. wx.navigateTo({
  518. url: './../channelDetails/channelDetails'
  519. });
  520. }
  521. },
  522. onUnload() {
  523. var that = this;
  524. if (!strings.isEmpty(that.data.intervalId)) {
  525. clearInterval(that.data.intervalId);
  526. that.data.intervalId = null;
  527. }
  528. if (app.globalData.client === null) {
  529. return;
  530. };
  531. app.globalData.client.end(true);
  532. app.globalData.client.end(true);
  533. app.globalData.client = null;
  534. },
  535. onTapIndex(e) {
  536. var that = this;
  537. var index = e.currentTarget.dataset.index;
  538. var indexPage = that.data.indexPage;
  539. if (indexPage != index) {
  540. that.setData({
  541. indexPage: index,
  542. });
  543. }
  544. },
  545. goMeAbout() {
  546. wx.navigateTo({
  547. url: './../about/about',
  548. })
  549. },
  550. onTapLogin() {
  551. var that = this;
  552. ///退出登录
  553. if (that.data.isLogin) {
  554. that.logOut();
  555. }
  556. // 登录
  557. else {
  558. route_util.jump(route_constant.login);
  559. }
  560. },
  561. ///退出登录
  562. logOut() {
  563. var that = this;
  564. ///有设备在线被选中,则让它不被选择
  565. var deviceListIndex = that.data.deviceListIndex;
  566. if (deviceListIndex != null) {
  567. var list = that.data.deviceList;
  568. if (list.length > deviceListIndex) {
  569. if (list[deviceListIndex].connectType == 3) {
  570. if (that.data.thisDeviceMac !== null) {
  571. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  572. that.setData({
  573. actionIndex: null,
  574. deviceListIndex: null,
  575. });
  576. };
  577. }
  578. }
  579. }
  580. wx.removeStorageSync('userInfo');
  581. wx.removeStorageSync('token');
  582. var nickName = "未登录";
  583. var greeting = that.getGreetBuNickName(nickName);
  584. that.setData({
  585. isLogin: false,
  586. greeting: greeting,
  587. nickName: nickName,
  588. userPic: "./../../img/head_pic.png",
  589. });
  590. },
  591. ///添加设备
  592. addDevice() {
  593. var that = this;
  594. if (!that.data.isLogin) {
  595. route_util.jump(route_constant.login);
  596. return;
  597. }
  598. ///跳转设备列表
  599. route_util.jump(route_constant.deviceList);
  600. },
  601. ///点击item
  602. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  603. onTapItem(e) {
  604. var that = this;
  605. if (!that.data.isLogin) {
  606. route_util.jump(route_constant.login);
  607. return;
  608. }
  609. var item = e.currentTarget.dataset.item;
  610. console.log("点击item:", item)
  611. var connectType = item.connectType;
  612. // wifi只支持在线点击
  613. if (connectType == 3 && item.state === "online") {
  614. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  615. that.goWake();
  616. } else {
  617. that.actionDevice(e.currentTarget.dataset.index)
  618. }
  619. return;
  620. }
  621. ///去蓝牙连接处理
  622. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  623. console.log(item)
  624. route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
  625. } else if (item.state === "offline") {
  626. console.log("去连接蓝牙")
  627. } else {
  628. ///item
  629. // {"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 妙播收音机"}
  630. item.name = item.devName;
  631. BtHelper.getInstance().connect(item, function (data) {
  632. if (data) {
  633. that.addConnectBlueDevice(item);
  634. }
  635. });
  636. }
  637. },
  638. actionDevice(index) {
  639. var that = this;
  640. if (!that.data.isLogin) {
  641. return;
  642. }
  643. if (that.data.deviceList.length <= index) {
  644. return;
  645. };
  646. const device = that.data.deviceList[index];
  647. if (device.state !== "online") {
  648. return;
  649. };
  650. // 取消订阅
  651. if (that.data.thisDeviceMac !== null) {
  652. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  653. };
  654. that.setData({
  655. deviceListIndex: index,
  656. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
  657. });
  658. // app.PubMsg({
  659. // type: "get_dev_info",
  660. // DstDeviceName: that.getThisDeviceID()
  661. // });
  662. that.subscribeCurrDevice();
  663. },
  664. goWake() {
  665. var that = this;
  666. if (that.data.deviceListIndex === null) {
  667. return;
  668. };
  669. wx.navigateTo({
  670. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  671. });
  672. },
  673. ///点击banner事件
  674. onTapBanner() {
  675. var that = this;
  676. var item = e.currentTarget.dataset.item;
  677. },
  678. ///删除当前设备
  679. deleteDevice(e) {
  680. var that = this;
  681. wx.showModal({
  682. title: '确定删除?',
  683. success: function (res) {
  684. if (res.confirm) {
  685. if (that.data.deviceList[e.currentTarget.dataset.index].connectType != 3) {
  686. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  687. const deviceList = that.data.deviceList.filter((item, i) => {
  688. return id !== item.deviceId
  689. });
  690. wx.setStorageSync("deviceList", JSON.stringify(deviceList));
  691. that.setData({
  692. deviceList,
  693. });
  694. // 当前没有设备
  695. if (deviceList.length === 0) {
  696. that.setData({
  697. deviceListIndex: null,
  698. });
  699. }
  700. return;
  701. }
  702. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  703. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  704. const deviceList = that.data.deviceList.filter((item, i) => {
  705. return id !== item.deviceId
  706. });
  707. // 取消订阅
  708. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  709. wx.setStorageSync("deviceList", JSON.stringify(deviceList));
  710. that.setData({
  711. deviceList,
  712. thisDeviceMac: null
  713. });
  714. // 当前没有设备
  715. if (deviceList.length === 0) {
  716. that.setData({
  717. actionIndex: null,
  718. deviceListIndex: null,
  719. });
  720. }
  721. if (that.data.deviceListIndex === null) {
  722. return
  723. };
  724. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  725. let index_ = null;
  726. deviceList.map((v, index) => {
  727. if (v.state === "online" && index_ === null) {
  728. index_ = index;
  729. }
  730. });
  731. if (index_ !== null) {
  732. that.actionDevice(index_);
  733. } else {
  734. that.setData({
  735. actionIndex: null,
  736. deviceListIndex: null,
  737. });
  738. };
  739. } else {
  740. deviceList.map((v, index) => {
  741. if (v.name === name) {
  742. that.setData({
  743. deviceListIndex: index,
  744. });
  745. }
  746. });
  747. }
  748. }
  749. }
  750. });
  751. },
  752. // {"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",
  753. /// deviceList
  754. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  755. /// clientType
  756. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  757. ///新添加蓝牙设备
  758. addConnectBlueDevice(newDevice) {
  759. var that = this;
  760. // 同一个设备
  761. var deviceList = that.data.deviceList;
  762. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  763. if (tempList && tempList.length > 0) {
  764. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  765. };
  766. console.log("添加蓝牙设备:", newDevice);
  767. deviceList.unshift({
  768. /// 蓝牙ble连接
  769. connectType: 1,
  770. deviceId: newDevice.deviceId,
  771. name: newDevice.name,
  772. state: "online",
  773. ProdModel: newDevice.clientType,
  774. devName: newDevice.name,
  775. mac: newDevice.mac,
  776. });
  777. ///在线排序前面,wifi设备排序前面
  778. that.updateDeviceList(deviceList, false);
  779. var indexPage = that.data.indexPage;
  780. if (indexPage != 0) {
  781. that.setData({
  782. indexPage: 0,
  783. deviceListIndex: 0,
  784. });
  785. } else {
  786. that.setData({
  787. deviceListIndex: 0,
  788. });
  789. }
  790. },
  791. ///新添加wifi设备
  792. addConnectWifiDevice(deviceList) {
  793. var that = this;
  794. ///在线排序前面,wifi设备排序前面
  795. that.updateDeviceList(deviceList, false);
  796. var indexPage = that.data.indexPage;
  797. if (indexPage != 0) {
  798. that.setData({
  799. indexPage: 0,
  800. deviceListIndex: 0,
  801. });
  802. } else {
  803. that.setData({
  804. deviceListIndex: 0,
  805. });
  806. }
  807. },
  808. ///更新列表排序
  809. updateDeviceList(deviceList, isInit) {
  810. if (deviceList.length == 0) {
  811. return;
  812. }
  813. var that = this;
  814. var finalList = [];
  815. finalList.push(deviceList[0]);
  816. console.log("online", deviceList[0].deviceId, deviceList[0].state);
  817. var onLineList = [];
  818. var onNoLineList = [];
  819. ///区分在线和离线
  820. for (var i = 0; i < deviceList.length; i++) {
  821. if (isInit) {
  822. deviceList[i].state = "offline";
  823. } else {
  824. if (i > 0) {
  825. if (deviceList[i].state == "online") {
  826. onLineList.push(deviceList[i])
  827. } else {
  828. onNoLineList.push(deviceList[i])
  829. }
  830. }
  831. }
  832. }
  833. // 区分在线wifi和蓝牙 wifi在前 离线在后
  834. var onLineWifiList = [];
  835. var onLineNoWifiList = [];
  836. onLineList.forEach(element => {
  837. if (element.connectType == 3) {
  838. onLineWifiList.push(element)
  839. } else {
  840. onLineNoWifiList.push(element)
  841. }
  842. });
  843. finalList = finalList.concat(onLineWifiList);
  844. finalList = finalList.concat(onLineNoWifiList);
  845. ///区分离线wifi和蓝牙 wifi在前 离线在后
  846. var onNoLineWifiList = [];
  847. var onNoLineNoWifiList = [];
  848. onNoLineList.forEach(element => {
  849. if (element.connectType == 3) {
  850. onNoLineWifiList.push(element)
  851. } else {
  852. onNoLineNoWifiList.push(element)
  853. }
  854. });
  855. finalList = finalList.concat(onNoLineWifiList);
  856. finalList = finalList.concat(onNoLineNoWifiList);
  857. // 更新缓存
  858. wx.setStorageSync("deviceList", JSON.stringify(finalList));
  859. that.setData({
  860. deviceList: finalList
  861. });
  862. getApp().globalData.deviceList = finalList;
  863. },
  864. ///******************************* 可折叠 ********************************************///
  865. onUserInfoLoad() {
  866. var that = this;
  867. var userInfo = wx.getStorageSync("userInfo") || "";
  868. if (!strings.isEmpty(userInfo)) {
  869. var phone = userInfo.phone;
  870. if (!phone) {
  871. return;
  872. };
  873. var nickName = userInfo.nickname || "";
  874. var greeting = that.getGreetBuNickName(nickName);
  875. // 获取缓存的频道数据
  876. var channelData = wx.getStorageSync("channelData") || "";
  877. if (!strings.isEmpty(channelData)) {
  878. that.setData({
  879. channelData: channelData,
  880. greeting: greeting,
  881. nickName: nickName,
  882. userPic: userInfo.headUrl || "",
  883. isLogin: true,
  884. })
  885. } else {
  886. that.setData({
  887. greeting: greeting,
  888. nickName: nickName,
  889. userPic: userInfo.headUrl || "",
  890. isLogin: true,
  891. });
  892. }
  893. } else {
  894. var greeting = that.getGreetBuNickName("");
  895. that.setData({
  896. greeting: greeting,
  897. })
  898. }
  899. },
  900. getGreetBuNickName(nickName) {
  901. var greeting = timeUtil.getGreet();
  902. if (nickName != "" && nickName != "未登录") {
  903. greeting = greeting + ',' + nickName;;
  904. }
  905. return greeting;
  906. },
  907. onBannerLoad() {
  908. var that = this;
  909. var bannerList = wx.getStorageSync("homeBanner") || [];
  910. if (that.data.bannerList.length == 0) {
  911. that.setData({
  912. bannerList: bannerList
  913. });
  914. }
  915. },
  916. onBannerShow() {
  917. var that = this;
  918. getBanner({}).then((res) => {
  919. that.setData({
  920. bannerList: res
  921. });
  922. wx.setStorageSync("homeBanner", res);
  923. })
  924. },
  925. ///销毁蓝牙
  926. onUnload() {
  927. BtHelper.getInstance().disconnect();
  928. }
  929. })
  930. // 去掉此功能,先留着吧
  931. // const other= {
  932. // "url": "",
  933. // "media_data": "",
  934. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  935. // "timestamp": `${Math.round(new Date() / 1000)}`,
  936. // "channel_id": `${res[1].channelNum}`,
  937. // "order": "",
  938. // "resource_from": "",
  939. // "songAlbumID":"",
  940. // "version":3,
  941. // "is_debug": app.globalData.is_debug
  942. // };
  943. // app.PubMsg({
  944. // type: "play",
  945. // DstDeviceName: that.getThisDeviceID(),
  946. // other
  947. // });