index.js 28 KB

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