index.js 33 KB

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