index.js 33 KB

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