index.js 34 KB

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