index.js 32 KB

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