index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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次 payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  243. var isUpdate = false;
  244. var deviceList = that.data.deviceList;
  245. if (!strings.isEmpty(deviceList)) {
  246. for (var i = 0; i < deviceList.length; i++) {
  247. if (payloads.SrcDeviceName) {
  248. var deviceId = deviceList[i].deviceId;
  249. var splitDeviceId = deviceId.split("BLUFI_");
  250. if (splitDeviceId.length == 2) {
  251. var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
  252. if (index !== -1 && (deviceList[i].ProdModel != payloads.other.ProdModel || deviceList[i].devName != payloads.other.devName)) {
  253. isUpdate = true;
  254. deviceList[i].ProdModel = payloads.other.ProdModel;
  255. deviceList[i].devName = payloads.other.devName;
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. ///数据有更新
  263. if (isUpdate) {
  264. that.updateDeviceList(deviceList, false);
  265. }
  266. } else if (payloads.type === "battery" && payloads.other) {
  267. that.setData({
  268. battery: that._battery(payloads.other.battery),
  269. })
  270. }
  271. break;
  272. default:
  273. }
  274. },
  275. // 格式化电量
  276. _battery(battery) {
  277. let _battery = 0;
  278. if (battery < 20) {
  279. _battery = 0
  280. } else if (20 <= battery && battery < 40) {
  281. _battery = 1
  282. } else if (40 <= battery && battery < 60) {
  283. _battery = 2
  284. } else if (60 <= battery && battery < 80) {
  285. _battery = 3
  286. } else if (80 <= battery && battery <= 100) {
  287. _battery = 4
  288. } else if (battery > 100) {
  289. _battery = 5
  290. };
  291. return _battery
  292. },
  293. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  294. ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
  295. /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
  296. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  297. onlineDevice(payloads) {
  298. // 设置在线状态
  299. var that = this;
  300. // console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
  301. ///是否更新过在线离线状态
  302. var isUpdate = false;
  303. var deviceList = that.data.deviceList;
  304. if (!strings.isEmpty(deviceList)) {
  305. for (var i = 0; i < deviceList.length; i++) {
  306. if (payloads.uuid) {
  307. var deviceId = deviceList[i].deviceId;
  308. var splitDeviceId = deviceId.split("BLUFI_");
  309. if (splitDeviceId.length == 2) {
  310. var index = payloads.uuid.indexOf(splitDeviceId[1]);
  311. if (index !== -1) {
  312. if (deviceList[i].state != payloads.state) {
  313. isUpdate = true;
  314. deviceList[i].state = payloads.state;
  315. break;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. ///数据有更新
  323. if (isUpdate) {
  324. that.updateDeviceList(deviceList, false);
  325. }
  326. ///当前没有连接设备,则去连接第一个wifi设备
  327. var deviceListIndex = that.data.deviceListIndex;
  328. if (deviceListIndex === null) {
  329. var list = that.data.deviceList;
  330. for (var i = 0; i < list.length; i++) {
  331. if (list[i].state === "online" && list[i].connectType == 3) {
  332. if (that.data.isLogin) {
  333. that.actionDevice(i);
  334. }
  335. break;
  336. }
  337. }
  338. } else {
  339. // 当前播放设备离线
  340. if (that.data.deviceList.length > deviceListIndex && that.data.deviceList[deviceListIndex].state !== "online") {
  341. that.setData({
  342. actionIndex: null,
  343. deviceListIndex: null,
  344. });
  345. };
  346. }
  347. },
  348. // 订阅设备在线状态
  349. subscribeDevicesStatus() {
  350. var that = this;
  351. var deviceList = that.data.deviceList;
  352. if (!strings.isEmpty(deviceList)) {
  353. for (var i = 0; i < deviceList.length; i++) {
  354. if (deviceList[i].connectType == 3) {
  355. let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  356. app.subscribe(topic);
  357. }
  358. }
  359. }
  360. },
  361. subscribeCurrDevice() {
  362. var that = this;
  363. if (!(app.globalData.client && app.globalData.client.connected)) {
  364. console.log("未连接MQTT服务器");
  365. const str = setInterval(() => {
  366. clearInterval(str);
  367. that.subscribeCurrDevice();
  368. }, 500);
  369. return;
  370. };
  371. var deviceList = that.data.deviceList;
  372. var deviceListIndex = that.data.deviceListIndex;
  373. if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
  374. return
  375. };
  376. var device = that.data.deviceList[deviceListIndex];
  377. var tempDeviceId = device.deviceId;
  378. ///删除当前选中这个
  379. deviceList.splice(deviceListIndex, 1);
  380. ///添加到第一个去
  381. deviceList.unshift(device);
  382. that.setData({
  383. deviceListIndex: 0,
  384. autoConnected: true,
  385. deviceList: deviceList,
  386. });
  387. /// /AIrSMArT_7cdfa1fd3af0/user/pub_response
  388. var 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. var deviceList = that.data.deviceList;
  615. var deviceListIndex = that.data.deviceListIndex;
  616. var device = deviceList[deviceListIndex];
  617. wx.navigateTo({
  618. url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
  619. });
  620. },
  621. ///点击banner事件
  622. onTapBanner() {
  623. var that = this;
  624. var item = e.currentTarget.dataset.item;
  625. },
  626. ///删除当前设备
  627. deleteDevice(e) {
  628. var that = this;
  629. var index = e.currentTarget.dataset.index;
  630. wx.showModal({
  631. title: '确定删除?',
  632. success: function (res) {
  633. if (res.confirm) {
  634. if (that.data.deviceList[index].connectType == 3) {
  635. that.cancelWifi(index, false);
  636. } else {
  637. that.cancelBlue(index, false);
  638. }
  639. }
  640. }
  641. });
  642. },
  643. ///是否是同一个蓝牙
  644. isTheSameBlue(connectDevice) {
  645. var that = this;
  646. var deviceListIndex = that.data.deviceListIndex;
  647. if (deviceListIndex == null) {
  648. return false;
  649. }
  650. var deviceList = that.data.deviceList;
  651. if (deviceList.length > deviceListIndex) {
  652. if (deviceList[deviceListIndex].connectType == 3) {
  653. return false;
  654. } else {
  655. if (deviceList[deviceListIndex].deviceId == connectDevice.deviceId) {
  656. return true;
  657. } else {
  658. return false;
  659. }
  660. }
  661. } else {
  662. return false;
  663. }
  664. },
  665. ///断开当前的
  666. async cancelCurrent() {
  667. var that = this;
  668. var deviceListIndex = that.data.deviceListIndex;
  669. if (deviceListIndex == null) {
  670. return;
  671. }
  672. var deviceList = that.data.deviceList;
  673. if (deviceList.length > deviceListIndex) {
  674. if (deviceList[deviceListIndex].connectType == 3) {
  675. await that.cancelWifi(deviceListIndex, true);
  676. } else {
  677. await that.cancelBlue(deviceListIndex, true);
  678. }
  679. }
  680. },
  681. /// 断开蓝牙连接
  682. async cancelBlue(index, onlyCancel) {
  683. var that = this;
  684. // await BtHelper.getInstance().disconnect();
  685. if (onlyCancel) {
  686. return;
  687. }
  688. const id = that.data.deviceList[index].deviceId;
  689. const deviceList = that.data.deviceList.filter((item, i) => {
  690. return id !== item.deviceId
  691. });
  692. store.setStore("deviceList", deviceList);
  693. that.setData({
  694. deviceList,
  695. });
  696. // 当前没有设备
  697. // if (deviceList.length === 0) {
  698. that.setData({
  699. deviceListIndex: null,
  700. });
  701. // }
  702. },
  703. /// 断开连接wifi
  704. async cancelWifi(index, onlyCancel) {
  705. var that = this;
  706. // 取消订阅
  707. app.unsubscribe(`/AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}/user/pub_response`);
  708. if (onlyCancel) {
  709. return;
  710. }
  711. const id = that.data.deviceList[index].deviceId;
  712. let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
  713. const deviceList = that.data.deviceList.filter((item, i) => {
  714. return id !== item.deviceId
  715. });
  716. store.setStore("deviceList", deviceList);
  717. that.setData({
  718. deviceList,
  719. thisDeviceMac: null
  720. });
  721. // 当前没有设备
  722. // if (deviceList.length === 0) {
  723. that.setData({
  724. actionIndex: null,
  725. deviceListIndex: null,
  726. });
  727. // }
  728. if (that.data.deviceListIndex === null) {
  729. return
  730. };
  731. if (index === that.data.deviceListIndex) {
  732. let index_ = null;
  733. deviceList.map((v, index) => {
  734. if (v.state === "online" && index_ === null) {
  735. index_ = index;
  736. }
  737. });
  738. if (index_ !== null) {
  739. that.actionDevice(index_);
  740. } else {
  741. that.setData({
  742. actionIndex: null,
  743. deviceListIndex: null,
  744. });
  745. };
  746. } else {
  747. deviceList.map((v, index) => {
  748. if (v.name === name) {
  749. that.setData({
  750. deviceListIndex: index,
  751. autoConnected: true,
  752. });
  753. }
  754. });
  755. }
  756. },
  757. // {"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",
  758. /// deviceList
  759. /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
  760. /// clientType
  761. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
  762. ///新添加蓝牙设备
  763. addConnectBlueDevice(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("添加蓝牙设备:", newDevice);
  772. deviceList.unshift({
  773. /// 蓝牙ble连接
  774. connectType: 1,
  775. deviceId: newDevice.deviceId,
  776. name: newDevice.name,
  777. state: "online",
  778. ProdModel: newDevice.ProdModel || newDevice.clientType,
  779. clientType: newDevice.clientType || newDevice.ProdModel,
  780. devName: newDevice.name,
  781. mac: newDevice.mac,
  782. img: newDevice.img,
  783. });
  784. ///在线排序前面,wifi设备排序前面
  785. that.updateDeviceList(deviceList, false);
  786. var indexPage = that.data.indexPage;
  787. if (indexPage != 0) {
  788. that.setData({
  789. indexPage: 0,
  790. deviceListIndex: 0,
  791. autoConnected: true,
  792. });
  793. } else {
  794. that.setData({
  795. deviceListIndex: 0,
  796. autoConnected: true,
  797. });
  798. }
  799. },
  800. ///新添加wifi设备
  801. async addConnectWifiDevice(newDevice) {
  802. var that = this;
  803. // 同一个设备
  804. var deviceList = that.data.deviceList;
  805. var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
  806. if (tempList && tempList.length > 0) {
  807. deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
  808. };
  809. console.log("添加Wifi设备:" + JSON.stringify(newDevice));
  810. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  811. deviceList.unshift({
  812. /// Wifi连接
  813. connectType: 3,
  814. deviceId: newDevice.deviceId,
  815. name: "猫王小王子OTR-X",
  816. state: "online",
  817. devName: "猫王小王子OTR-X",
  818. mac: newDevice.deviceId,
  819. image: "./../../img/min.png",
  820. });
  821. ///在线排序前面,wifi设备排序前面
  822. that.updateDeviceList(deviceList, false);
  823. var indexPage = that.data.indexPage;
  824. if (indexPage != 0) {
  825. that.setData({
  826. indexPage: 0,
  827. });
  828. }
  829. ///断开蓝牙连接
  830. // await BtHelper.getInstance().disconnect();
  831. that.actionDevice(0);
  832. },
  833. ///更新列表排序
  834. updateDeviceList(deviceList, isInit) {
  835. if (deviceList.length <= 0) {
  836. return;
  837. }
  838. var that = this;
  839. var finalList = [];
  840. if (isInit) {
  841. deviceList[0].state = "offline";
  842. }
  843. var isFirstOnline = false;
  844. if (deviceList[0].state == "online") {
  845. isFirstOnline = true;
  846. finalList.push(deviceList[0]);
  847. }
  848. ///区分在线和离线
  849. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  850. if (isInit) {
  851. deviceList[i].state = "offline";
  852. }
  853. }
  854. var onLineList = [];
  855. var onNoLineList = [];
  856. ///添加在线的
  857. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  858. var device = deviceList[i];
  859. if (device.state == "online") {
  860. onLineList.push(device)
  861. }
  862. }
  863. ///添加离线的
  864. for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
  865. var device = deviceList[i];
  866. if (device.state != "online") {
  867. onNoLineList.push(device)
  868. }
  869. }
  870. // 区分在线wifi和蓝牙 wifi在前 离线在后
  871. var onLineWifiList = [];
  872. var onLineBlueList = [];
  873. ///添加在线wifi
  874. onLineList.forEach(element => {
  875. if (element.connectType == 3) {
  876. onLineWifiList.push(element);
  877. }
  878. });
  879. ///添加在线蓝牙
  880. onLineList.forEach(element => {
  881. if (element.connectType != 3) {
  882. onLineBlueList.push(element)
  883. }
  884. });
  885. finalList = finalList.concat(onLineWifiList);
  886. finalList = finalList.concat(onLineBlueList);
  887. ///只需要蓝牙和wifi在线的
  888. let mDeviceList = []
  889. mDeviceList = mDeviceList.concat(onLineWifiList);
  890. mDeviceList = mDeviceList.concat(onLineBlueList);
  891. getApp().globalData.mDeviceList = mDeviceList;
  892. ///区分离线wifi和蓝牙 wifi在前 离线在后
  893. var onNoLineWifiList = [];
  894. var onNoLineBlueList = [];
  895. ///添加离线wifi
  896. onNoLineList.forEach(element => {
  897. if (element.connectType == 3) {
  898. onNoLineWifiList.push(element)
  899. }
  900. });
  901. ///添加离线蓝牙
  902. onNoLineList.forEach(element => {
  903. if (element.connectType != 3) {
  904. onNoLineBlueList.push(element)
  905. }
  906. });
  907. finalList = finalList.concat(onNoLineWifiList);
  908. finalList = finalList.concat(onNoLineBlueList);
  909. store.setStore("deviceList", finalList);
  910. that.setData({
  911. deviceList: finalList
  912. });
  913. },
  914. ///******************************* 可折叠 ********************************************///
  915. onUserInfoLoad() {
  916. var that = this;
  917. var userInfo = wx.getStorageSync("userInfo") || "";
  918. if (!strings.isEmpty(userInfo)) {
  919. var phone = userInfo.phone;
  920. if (!phone) {
  921. return;
  922. };
  923. var nickName = userInfo.nickname || "";
  924. var greeting = that.getGreetBuNickName(nickName);
  925. // 获取缓存的频道数据
  926. var channelData = wx.getStorageSync("channelData") || "";
  927. if (!strings.isEmpty(channelData)) {
  928. that.setData({
  929. channelData: channelData,
  930. greeting: greeting,
  931. nickName: nickName,
  932. userPic: userInfo.headUrl || "",
  933. isLogin: true,
  934. })
  935. } else {
  936. that.setData({
  937. greeting: greeting,
  938. nickName: nickName,
  939. userPic: userInfo.headUrl || "",
  940. isLogin: true,
  941. });
  942. }
  943. } else {
  944. var greeting = that.getGreetBuNickName("");
  945. that.setData({
  946. greeting: greeting,
  947. })
  948. }
  949. },
  950. getGreetBuNickName(nickName) {
  951. var greeting = timeUtil.getGreet();
  952. if (nickName != "" && nickName != "未登录") {
  953. greeting = greeting + ',' + nickName;;
  954. }
  955. return greeting;
  956. },
  957. onBannerLoad() {
  958. var that = this;
  959. // var bannerList = wx.getStorageSync("homeBanner") || [];
  960. // if (that.data.bannerList.length > 0) {
  961. // that.setData({
  962. // bannerList: bannerList
  963. // });
  964. // }
  965. },
  966. onBannerShow() {
  967. var that = this;
  968. // getBanner({}).then((res) => {
  969. // that.setData({
  970. // bannerList: res
  971. // });
  972. // wx.setStorageSync("homeBanner", res);
  973. // })
  974. },
  975. ///销毁蓝牙
  976. onUnload() {
  977. var that = this;
  978. if (!strings.isEmpty(that.data.intervalId)) {
  979. clearInterval(that.data.intervalId);
  980. that.data.intervalId = null;
  981. }
  982. BtHelper.getInstance().disconnect();
  983. if (app.globalData.client === null) {
  984. return;
  985. };
  986. app.globalData.client.end(true);
  987. app.globalData.client.end(true);
  988. app.globalData.client = null;
  989. },
  990. })
  991. // addNewDeviceId() {
  992. // // 连接mqtt
  993. // var that = this;
  994. // if (app.globalData.client === null) {
  995. // app.connect();
  996. // } else if (app.globalData.newDeviceId) {
  997. // //监听
  998. // var deviceList = that.data.deviceList;
  999. // if (deviceList.length == 0) {
  1000. // return;
  1001. // }
  1002. // ///连接新添加的设备
  1003. // var deviceListIndex = that.data.deviceListIndex;
  1004. // if (deviceListIndex != null) {
  1005. // if (deviceList.length > deviceListIndex) {
  1006. // if (deviceList[deviceListIndex].deviceId != deviceList[0].deviceId) {
  1007. // that.setData({
  1008. // actionIndex: null,
  1009. // deviceListIndex: null,
  1010. // });
  1011. // }
  1012. // } else {
  1013. // that.setData({
  1014. // actionIndex: null,
  1015. // deviceListIndex: null,
  1016. // });
  1017. // }
  1018. // }
  1019. // var topic = `/AIrSMArT_${deviceList[0].deviceId.split("BLUFI_")[1]}/status/onoffline`;
  1020. // app.subscribe(topic);
  1021. // const Timeout = setTimeout(() => {
  1022. // clearTimeout(Timeout);
  1023. // that.actionDevice(0);
  1024. // }, 500);
  1025. // }
  1026. // },
  1027. // tryConnectBle() {
  1028. // var hasBle = false;
  1029. // var hasConnectBle = false;
  1030. // var hasWifi = false;
  1031. // var bleDevice;
  1032. // let that = this
  1033. // that.data.deviceList.forEach(device => {
  1034. // if (device.connectType == 1) {
  1035. // hasBle = true
  1036. // if (device.state == "online") {
  1037. // hasConnectBle = true
  1038. // }
  1039. // bleDevice = device
  1040. // } else if (device.connectType == 3 && device.state == "online") {
  1041. // // wifi
  1042. // hasWifi = true;
  1043. // }
  1044. // });
  1045. // if (!hasWifi && !hasConnectBle && hasBle) {
  1046. // // 没有wifi 没有连接的ble 有未连接的ble
  1047. // console.log("去连接蓝牙")
  1048. // let bt_helper = BtHelper.getInstance()
  1049. // bt_helper.initBluetooth(function (adapterState, hasPermission) {
  1050. // console.log("蓝牙状态", adapterState, hasPermission)
  1051. // if (adapterState && hasPermission) {
  1052. // bt_helper.connect(bleDevice, function (data) {
  1053. // console.log("连接成功:", data)
  1054. // if (data == true) {
  1055. // // 蓝牙模式
  1056. // bleDevice.connectType = 1
  1057. // // 在线
  1058. // bleDevice.state = 'online'
  1059. // bleDevice.ProdModel = bleDevice.clientType
  1060. // that.addConnectBlueDevice(connectDevice);
  1061. // }
  1062. // })
  1063. // } else {}
  1064. // })
  1065. // }
  1066. // },
  1067. // 去掉此功能,先留着吧
  1068. // const other= {
  1069. // "url": "",
  1070. // "media_data": "",
  1071. // "user_id": `${app.globalData.userInfo.deviceUid}`,
  1072. // "timestamp": `${Math.round(new Date() / 1000)}`,
  1073. // "channel_id": `${res[1].channelNum}`,
  1074. // "order": "",
  1075. // "resource_from": "",
  1076. // "songAlbumID":"",
  1077. // "version":3,
  1078. // "is_debug": app.globalData.is_debug
  1079. // };
  1080. // app.PubMsg({
  1081. // type: "play",
  1082. // DstDeviceName: that.getThisDeviceID(),
  1083. // other
  1084. // });