index.js 28 KB

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