index.js 31 KB

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