index.js 34 KB

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