index.js 35 KB

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