index.js 34 KB

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