index.js 28 KB

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