index.js 31 KB

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