index.js 31 KB

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