index.js 29 KB

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