index.js 34 KB

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