index.js 28 KB

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