index.js 33 KB

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