index.js 33 KB

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