index.js 33 KB

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