index.js 33 KB

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