index.js 28 KB

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