index.js 33 KB

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