index.js 33 KB

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