index.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. }, 10 * 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"
  155. if (tempItem.deviceId === compareList[j].deviceId) {
  156. has = true;
  157. isChanged = true;
  158. break;
  159. }
  160. }
  161. tempItem.state = has ? "online" : "offline";
  162. }
  163. }
  164. if (isChanged) {
  165. that.updateDeviceList(tempList, false);
  166. }
  167. }
  168. BtHelper.getInstance().startScan(null, null, null);
  169. }
  170. },
  171. addNewDeviceId() {
  172. // 连接mqtt
  173. var that = this;
  174. if (app.globalData.client === null) {
  175. app.connect();
  176. } else if (app.globalData.newDeviceId) {
  177. //监听
  178. var deviceList = that.data.deviceList;
  179. if (deviceList.length == 0) {
  180. return;
  181. }
  182. ///连接新添加的设备
  183. var deviceListIndex = that.data.deviceListIndex;
  184. if (deviceListIndex != null) {
  185. if (deviceList.length > deviceListIndex) {
  186. if (deviceList[deviceListIndex].deviceId != deviceList[0].deviceId) {
  187. that.setData({
  188. actionIndex: null,
  189. deviceListIndex: null,
  190. });
  191. }
  192. } else {
  193. that.setData({
  194. actionIndex: null,
  195. deviceListIndex: null,
  196. });
  197. }
  198. }
  199. var topic = `/AIrSMArT_${deviceList[0].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  200. app.subscribe(topic);
  201. const Timeout = setTimeout(() => {
  202. clearTimeout(Timeout);
  203. that.actionDevice(0);
  204. }, 500);
  205. }
  206. },
  207. // 回调
  208. mqttCallback(type, option) {
  209. console.log("gadsfadsfadsfa==888===" + type);
  210. var that = this;
  211. let payloads = null;
  212. if (option) {
  213. payloads = JSON.parse(option.payload);
  214. };
  215. switch (type) {
  216. ///连接成功订阅
  217. case "connect":
  218. that.subscribeDevicesStatus();
  219. break;
  220. case "message_onoffline":
  221. that.onlineDevice(payloads);
  222. break;
  223. case "message":
  224. // 接收设备播放信息
  225. // if (!that.data.isLogin) {
  226. // return;
  227. // }
  228. ///获取频道数据
  229. ///锁定播放哪一个频道
  230. if (payloads.type === "get_position" && payloads.other) {
  231. let actionIndex = null;
  232. that.data.channelData.map((v, index) => {
  233. if (v.channelNum === payloads.other.channel) {
  234. actionIndex = index;
  235. }
  236. });
  237. that.setData({
  238. actionIndex,
  239. });
  240. }
  241. ///获取播放状态
  242. else if (payloads.type === "play" || payloads.type === "play_state") {
  243. var deviceList = that.data.deviceList;
  244. var deviceListIndex = that.data.deviceListIndex;
  245. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  246. return
  247. };
  248. var deviceId = deviceList[deviceListIndex].deviceId;
  249. // 接收设备当前播放状态
  250. const obj = {
  251. DstDeviceName: that.getThisDeviceID(deviceId)
  252. }
  253. app.PubMsg({
  254. type: "get_position",
  255. ...obj
  256. });
  257. }
  258. ///获取频道列表数据
  259. else if (payloads.type === "get_dev_info") {
  260. // 接收设备当前信息
  261. that.getchannelData(payloads.other.ProdModel);
  262. // 电量
  263. that.setData({
  264. battery: that._battery(payloads.other.Power),
  265. })
  266. // 当前设备木有设置定时
  267. that.setData({
  268. isSetWake: false,
  269. });
  270. payloads.other.alarm.map((v) => {
  271. if (v.enable === "1") {
  272. that.setData({
  273. isSetWake: true
  274. });
  275. }
  276. });
  277. // 更新信息
  278. ///连上就调用2次
  279. var isUpdate = false;
  280. // payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  281. var deviceList = that.data.deviceList;
  282. if (!strings.isEmpty(deviceList)) {
  283. for (var i = 0; i < deviceList.length; i++) {
  284. if (payloads.SrcDeviceName) {
  285. var deviceId = deviceList[i].deviceId;
  286. var splitDeviceId = deviceId.split("BLUFI_");
  287. if (splitDeviceId.length == 2) {
  288. var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
  289. if (index !== -1 && deviceList[i].ProdModel != payloads.other.ProdModel && deviceList[i].devName != payloads.other.devName) {
  290. isUpdate = true;
  291. deviceList[i].ProdModel = payloads.other.ProdModel;
  292. deviceList[i].devName = payloads.other.devName;
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. ///数据有更新
  300. if (isUpdate) {
  301. that.updateDeviceList(deviceList, false);
  302. }
  303. } else if (payloads.type === "battery" && payloads.other) {
  304. that.setData({
  305. battery: that._battery(payloads.other.battery),
  306. })
  307. }
  308. break;
  309. default:
  310. }
  311. },
  312. // 格式化电量
  313. _battery(battery) {
  314. let _battery = 0;
  315. if (battery < 20) {
  316. _battery = 0
  317. } else if (20 <= battery && battery < 40) {
  318. _battery = 1
  319. } else if (40 <= battery && battery < 60) {
  320. _battery = 2
  321. } else if (60 <= battery && battery < 80) {
  322. _battery = 3
  323. } else if (80 <= battery && battery <= 100) {
  324. _battery = 4
  325. } else if (battery > 100) {
  326. _battery = 5
  327. };
  328. return _battery
  329. },
  330. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  331. ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
  332. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  333. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  334. onlineDevice(payloads) {
  335. // 设置在线状态
  336. var that = this;
  337. console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  338. ///是否更新过在线离线状态
  339. var isUpdate = false;
  340. var deviceList = that.data.deviceList;
  341. if (!strings.isEmpty(deviceList)) {
  342. for (var i = 0; i < deviceList.length; i++) {
  343. if (payloads.uuid) {
  344. var deviceId = deviceList[i].deviceId;
  345. var splitDeviceId = deviceId.split("BLUFI_");
  346. if (splitDeviceId.length == 2) {
  347. var index = payloads.uuid.indexOf(splitDeviceId[1]);
  348. if (index !== -1) {
  349. if (deviceList[i].state != payloads.state) {
  350. isUpdate = true;
  351. deviceList[i].state = payloads.state;
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. ///数据有更新
  360. if (isUpdate) {
  361. that.updateDeviceList(deviceList, false);
  362. }
  363. ///当前没有连接设备,则去连接第一个wifi设备
  364. var deviceListIndex = that.data.deviceListIndex;
  365. if (deviceListIndex === null) {
  366. var list = that.data.deviceList;
  367. for (var i = 0; i < list.length; i++) {
  368. if (list[i].state === "online" && list[i].connectType == 3) {
  369. that.actionDevice(i);
  370. break;
  371. }
  372. }
  373. } else {
  374. // 当前播放设备离线
  375. if (that.data.deviceList.length > deviceListIndex && that.data.deviceList[deviceListIndex].state !== "online") {
  376. that.setData({
  377. actionIndex: null,
  378. deviceListIndex: null,
  379. });
  380. };
  381. }
  382. },
  383. // 订阅设备在线状态
  384. subscribeDevicesStatus() {
  385. var that = this;
  386. var deviceList = that.data.deviceList;
  387. if (!strings.isEmpty(deviceList)) {
  388. for (var i = 0; i < deviceList.length; i++) {
  389. if (deviceList[i].connectType == 3) {
  390. let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  391. app.subscribe(topic);
  392. }
  393. }
  394. }
  395. },
  396. subscribeCurrDevice() {
  397. var that = this;
  398. if (!(app.globalData.client && app.globalData.client.connected)) {
  399. console.log("未连接MQTT服务器");
  400. const str = setInterval(() => {
  401. clearInterval(str);
  402. that.subscribeCurrDevice();
  403. }, 500);
  404. return;
  405. };
  406. var deviceList = that.data.deviceList;
  407. var deviceListIndex = that.data.deviceListIndex;
  408. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  409. return
  410. };
  411. var tempDevice = that.data.deviceList[deviceListIndex];
  412. var tempDeviceId = tempDevice.deviceId;
  413. ///删除当前选中这个
  414. deviceList.splice(deviceListIndex, 1);
  415. ///添加到第一个去
  416. deviceList.unshift(tempDevice);
  417. that.setData({
  418. deviceListIndex: 0,
  419. deviceList: deviceList,
  420. });
  421. let topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
  422. app.subscribe(topic);
  423. const obj = {
  424. DstDeviceName: that.getThisDeviceID(tempDeviceId)
  425. };
  426. app.PubMsg({
  427. type: "get_dev_info",
  428. ...obj
  429. });
  430. },
  431. actionMusic(e) {
  432. var that = this;
  433. if (e.currentTarget.dataset.index === that.data.actionIndex) {
  434. return;
  435. };
  436. if (that.data.deviceListIndex === null) {
  437. wx.showToast({
  438. title: '请选择设备',
  439. icon: "none"
  440. })
  441. return;
  442. };
  443. that.setData({
  444. actionIndex: e.currentTarget.dataset.index
  445. });
  446. var deviceList = that.data.deviceList;
  447. var deviceListIndex = that.data.deviceListIndex;
  448. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  449. return
  450. };
  451. var deviceId = deviceList[deviceListIndex].deviceId;
  452. const other = {
  453. "url": "",
  454. "media_data": "",
  455. "user_id": `${app.globalData.userInfo.deviceUid}`,
  456. "timestamp": `${Math.round(new Date() / 1000)}`,
  457. "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
  458. "order": "",
  459. "resource_from": "",
  460. "songAlbumID": "",
  461. "version": 3,
  462. "is_debug": app.globalData.is_debug
  463. };
  464. app.PubMsg({
  465. type: "play",
  466. DstDeviceName: that.getThisDeviceID(deviceId),
  467. other
  468. });
  469. },
  470. getThisDeviceID(deviceId) {
  471. return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
  472. },
  473. getchannelData(clientType) {
  474. var that = this;
  475. var deviceList = that.data.deviceList;
  476. var deviceListIndex = that.data.deviceListIndex;
  477. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  478. return;
  479. };
  480. var deviceId = deviceList[deviceListIndex].deviceId;
  481. listByDevice({
  482. clientType
  483. }).then((res) => {
  484. that.setData({
  485. channelData: res
  486. });
  487. // 接收设备当前播放状态
  488. const obj = {
  489. DstDeviceName: that.getThisDeviceID(deviceId)
  490. }
  491. app.PubMsg({
  492. type: "get_position",
  493. ...obj
  494. });
  495. wx.setStorageSync("channelData", res);
  496. // 有新设备
  497. if (app.globalData.newDeviceId) {
  498. app.globalData.newDeviceId = null;
  499. return;
  500. }
  501. })
  502. },
  503. ///去频道详情
  504. onTapToChannel() {
  505. var that = this;
  506. if (that.data.channelData.length > that.data.actionIndex) {
  507. wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
  508. wx.navigateTo({
  509. url: './../channelDetails/channelDetails'
  510. });
  511. }
  512. },
  513. onUnload() {
  514. var that = this;
  515. if (!strings.isEmpty(that.data.intervalId)) {
  516. clearInterval(that.data.intervalId);
  517. that.data.intervalId = null;
  518. }
  519. if (app.globalData.client === null) {
  520. return;
  521. };
  522. app.globalData.client.end(true);
  523. app.globalData.client.end(true);
  524. app.globalData.client = null;
  525. },
  526. onTapIndex(e) {
  527. var that = this;
  528. var index = e.currentTarget.dataset.index;
  529. var indexPage = that.data.indexPage;
  530. if (indexPage != index) {
  531. that.setData({
  532. indexPage: index,
  533. });
  534. }
  535. },
  536. goMeAbout() {
  537. wx.navigateTo({
  538. url: './../about/about',
  539. })
  540. },
  541. onTapLogin() {
  542. var that = this;
  543. ///退出登录
  544. if (that.data.isLogin) {
  545. that.logOut();
  546. }
  547. // 登录
  548. else {
  549. route_util.jump(route_constant.login);
  550. }
  551. },
  552. ///退出登录
  553. logOut() {
  554. var that = this;
  555. ///有设备在线被选中,则让它不被选择
  556. var deviceListIndex = that.data.deviceListIndex;
  557. if (deviceListIndex != null) {
  558. var list = that.data.deviceList;
  559. if (list.length > deviceListIndex) {
  560. if (list[deviceListIndex].connectType == 3) {
  561. if (that.data.thisDeviceMac !== null) {
  562. app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
  563. that.setData({
  564. actionIndex: null,
  565. deviceListIndex: null,
  566. });
  567. };
  568. }
  569. }
  570. }
  571. wx.removeStorageSync('userInfo');
  572. wx.removeStorageSync('token');
  573. var nickName = "未登录";
  574. var greeting = that.getGreetBuNickName(nickName);
  575. that.setData({
  576. isLogin: false,
  577. greeting: greeting,
  578. nickName: nickName,
  579. userPic: "./../../img/head_pic.png",
  580. });
  581. },
  582. ///添加设备
  583. addDevice() {
  584. var that = this;
  585. if (!that.data.isLogin) {
  586. route_util.jump(route_constant.login);
  587. return;
  588. }
  589. ///跳转设备列表
  590. route_util.jump(route_constant.deviceList);
  591. },
  592. ///点击item
  593. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  594. onTapItem(e) {
  595. var that = this;
  596. if (!that.data.isLogin) {
  597. route_util.jump(route_constant.login);
  598. return;
  599. }
  600. var item = e.currentTarget.dataset.item;
  601. console.log("点击item:", item)
  602. var connectType = item.connectType;
  603. // wifi只支持在线点击
  604. if (connectType == 3 && item.state === "online") {
  605. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  606. that.goWake();
  607. } else {
  608. that.actionDevice(e.currentTarget.dataset.index)
  609. }
  610. return;
  611. }
  612. ///去蓝牙连接处理
  613. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  614. console.log(item)
  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. thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
  648. });
  649. // app.PubMsg({
  650. // type: "get_dev_info",
  651. // DstDeviceName: that.getThisDeviceID()
  652. // });
  653. that.subscribeCurrDevice();
  654. },
  655. goWake() {
  656. var that = this;
  657. if (that.data.deviceListIndex === null) {
  658. return;
  659. };
  660. wx.navigateTo({
  661. url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
  662. });
  663. },
  664. ///点击banner事件
  665. onTapBanner() {
  666. var that = this;
  667. var item = e.currentTarget.dataset.item;
  668. },
  669. ///删除当前设备
  670. deleteDevice(e) {
  671. var that = this;
  672. wx.showModal({
  673. title: '确定删除?',
  674. success: function (res) {
  675. if (res.confirm) {
  676. if (that.data.deviceList[e.currentTarget.dataset.index].connectType != 3) {
  677. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  678. const deviceList = that.data.deviceList.filter((item, i) => {
  679. return id !== item.deviceId
  680. });
  681. wx.setStorageSync("deviceList", JSON.stringify(deviceList));
  682. that.setData({
  683. deviceList,
  684. });
  685. // 当前没有设备
  686. if (deviceList.length === 0) {
  687. that.setData({
  688. deviceListIndex: null,
  689. });
  690. }
  691. return;
  692. }
  693. const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
  694. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  695. const deviceList = that.data.deviceList.filter((item, i) => {
  696. return id !== item.deviceId
  697. });
  698. // 取消订阅
  699. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
  700. wx.setStorageSync("deviceList", JSON.stringify(deviceList));
  701. that.setData({
  702. deviceList,
  703. thisDeviceMac: null
  704. });
  705. // 当前没有设备
  706. if (deviceList.length === 0) {
  707. that.setData({
  708. actionIndex: null,
  709. deviceListIndex: null,
  710. });
  711. }
  712. if (that.data.deviceListIndex === null) {
  713. return
  714. };
  715. if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
  716. let index_ = null;
  717. deviceList.map((v, index) => {
  718. if (v.state === "online" && index_ === null) {
  719. index_ = index;
  720. }
  721. });
  722. if (index_ !== null) {
  723. that.actionDevice(index_);
  724. } else {
  725. that.setData({
  726. actionIndex: null,
  727. deviceListIndex: null,
  728. });
  729. };
  730. } else {
  731. deviceList.map((v, index) => {
  732. if (v.name === name) {
  733. that.setData({
  734. deviceListIndex: index,
  735. });
  736. }
  737. });
  738. }
  739. }
  740. }
  741. });
  742. },
  743. // {"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",
  744. /// deviceList
  745. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  746. /// clientType
  747. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  748. ///新添加蓝牙设备
  749. addConnectBlueDevice(newDevice) {
  750. var that = this;
  751. // 同一个设备
  752. var deviceList = that.data.deviceList;
  753. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  754. if (tempList && tempList.length > 0) {
  755. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  756. };
  757. console.log("添加蓝牙设备:", newDevice);
  758. deviceList.unshift({
  759. /// 蓝牙ble连接
  760. connectType: 1,
  761. deviceId: newDevice.deviceId,
  762. name: newDevice.name,
  763. state: "online",
  764. ProdModel: newDevice.clientType,
  765. devName: newDevice.name,
  766. mac: newDevice.mac,
  767. });
  768. ///在线排序前面,wifi设备排序前面
  769. that.updateDeviceList(deviceList, false);
  770. var indexPage = that.data.indexPage;
  771. if (indexPage != 0) {
  772. that.setData({
  773. indexPage: 0,
  774. deviceListIndex: 0,
  775. });
  776. } else {
  777. that.setData({
  778. deviceListIndex: 0,
  779. });
  780. }
  781. },
  782. ///新添加wifi设备
  783. addConnectWifiDevice(deviceList) {
  784. var that = this;
  785. ///在线排序前面,wifi设备排序前面
  786. that.updateDeviceList(deviceList, false);
  787. var indexPage = that.data.indexPage;
  788. if (indexPage != 0) {
  789. that.setData({
  790. indexPage: 0,
  791. deviceListIndex: 0,
  792. });
  793. } else {
  794. that.setData({
  795. deviceListIndex: 0,
  796. });
  797. }
  798. },
  799. ///更新列表排序
  800. updateDeviceList(deviceList, isInit) {
  801. if (deviceList.length == 0) {
  802. return;
  803. }
  804. var that = this;
  805. var finalList = [];
  806. finalList.push(deviceList[0]);
  807. console.log("online", deviceList[0].deviceId, deviceList[0].state);
  808. var onLineList = [];
  809. var onNoLineList = [];
  810. ///区分在线和离线
  811. for (var i = 0; i < deviceList.length; i++) {
  812. if (isInit) {
  813. deviceList[i].state = "offline";
  814. } else {
  815. if (i > 0) {
  816. if (deviceList[i].state == "online") {
  817. onLineList.push(deviceList[i])
  818. } else {
  819. onNoLineList.push(deviceList[i])
  820. }
  821. }
  822. }
  823. }
  824. // 区分在线wifi和蓝牙 wifi在前 离线在后
  825. var onLineWifiList = [];
  826. var onLineNoWifiList = [];
  827. onLineList.forEach(element => {
  828. if (element.connectType == 3) {
  829. onLineWifiList.push(element)
  830. } else {
  831. onLineNoWifiList.push(element)
  832. }
  833. });
  834. finalList = finalList.concat(onLineWifiList);
  835. finalList = finalList.concat(onLineNoWifiList);
  836. ///区分离线wifi和蓝牙 wifi在前 离线在后
  837. var onNoLineWifiList = [];
  838. var onNoLineNoWifiList = [];
  839. onNoLineList.forEach(element => {
  840. if (element.connectType == 3) {
  841. onNoLineWifiList.push(element)
  842. } else {
  843. onNoLineNoWifiList.push(element)
  844. }
  845. });
  846. finalList = finalList.concat(onNoLineWifiList);
  847. finalList = finalList.concat(onNoLineNoWifiList);
  848. // 更新缓存
  849. wx.setStorageSync("deviceList", JSON.stringify(finalList));
  850. that.setData({
  851. deviceList: finalList
  852. });
  853. getApp().globalData.deviceList = finalList;
  854. },
  855. ///******************************* 可折叠 ********************************************///
  856. onUserInfoLoad() {
  857. var that = this;
  858. var userInfo = wx.getStorageSync("userInfo") || "";
  859. if (!strings.isEmpty(userInfo)) {
  860. var phone = userInfo.phone;
  861. if (!phone) {
  862. return;
  863. };
  864. var nickName = userInfo.nickname || "";
  865. var greeting = that.getGreetBuNickName(nickName);
  866. // 获取缓存的频道数据
  867. var channelData = wx.getStorageSync("channelData") || "";
  868. if (!strings.isEmpty(channelData)) {
  869. that.setData({
  870. channelData: channelData,
  871. greeting: greeting,
  872. nickName: nickName,
  873. userPic: userInfo.headUrl || "",
  874. isLogin: true,
  875. })
  876. } else {
  877. that.setData({
  878. greeting: greeting,
  879. nickName: nickName,
  880. userPic: userInfo.headUrl || "",
  881. isLogin: true,
  882. });
  883. }
  884. } else {
  885. var greeting = that.getGreetBuNickName("");
  886. that.setData({
  887. greeting: greeting,
  888. })
  889. }
  890. },
  891. getGreetBuNickName(nickName) {
  892. var greeting = timeUtil.getGreet();
  893. if (nickName != "" && nickName != "未登录") {
  894. greeting = greeting + ',' + nickName;;
  895. }
  896. return greeting;
  897. },
  898. onBannerLoad() {
  899. var that = this;
  900. var bannerList = wx.getStorageSync("homeBanner") || [];
  901. if (that.data.bannerList.length == 0) {
  902. that.setData({
  903. bannerList: bannerList
  904. });
  905. }
  906. },
  907. onBannerShow() {
  908. var that = this;
  909. getBanner({}).then((res) => {
  910. that.setData({
  911. bannerList: res
  912. });
  913. wx.setStorageSync("homeBanner", res);
  914. })
  915. },
  916. ///销毁蓝牙
  917. onUnload() {
  918. BtHelper.getInstance().disconnect();
  919. }
  920. })
  921. // 去掉此功能,先留着吧
  922. // const other= {
  923. // "url": "",
  924. // "media_data": "",
  925. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  926. // "timestamp": `${Math.round(new Date() / 1000)}`,
  927. // "channel_id": `${res[1].channelNum}`,
  928. // "order": "",
  929. // "resource_from": "",
  930. // "songAlbumID":"",
  931. // "version":3,
  932. // "is_debug": app.globalData.is_debug
  933. // };
  934. // app.PubMsg({
  935. // type: "play",
  936. // DstDeviceName: that.getThisDeviceID(),
  937. // other
  938. // });