index.js 28 KB

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