deviceWake.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. const app = getApp();
  2. const {
  3. formatNumber
  4. } = require('../../../utils/requestUtil.js');
  5. const {
  6. deviceWakedetail
  7. } = require('../../../utils/apiUtil.js');
  8. import lexin_util from '../../../utils/lexin/util.js';
  9. import routePath from '../../../utils/routePath.js';
  10. import routeUtil from '../../../utils/routeUtil.js';
  11. import { autoAuth, unbind, EnumPlatformId, getAuthStatus } from '../../../utils/auth/auth_util.js';
  12. Page({
  13. data: {
  14. nvabarData: {
  15. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  16. title: '定时设置', //导航栏 中间的标题
  17. },
  18. switch1Checked: false,
  19. alarm_id_0_time: "00:00:00",
  20. switch2Checked: false,
  21. deviceMac: null,
  22. alarm_id_0: {}, // 休眠
  23. alarm_id_1: {}, // 唤醒
  24. timeindex: 1,
  25. timeArray: [{
  26. id: 600,
  27. name: '10分钟'
  28. },
  29. {
  30. id: 1800,
  31. name: '30分钟'
  32. },
  33. {
  34. id: 3600,
  35. name: '60分钟'
  36. },
  37. {
  38. id: 5400,
  39. name: '90分钟'
  40. }
  41. ],
  42. clientType: "",
  43. time0: '00:00:00',
  44. time: '07:00:00',
  45. repeat: "",
  46. repeat0: "",
  47. week: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
  48. week_actives: [],
  49. isOne: true,
  50. wakeName: "",
  51. toastData: {
  52. titlePicUrl: "./../../../img/gt.png",
  53. titlePicUrlWidth: 140,
  54. title: ["温馨提醒"],
  55. info: ["当前设备电量低"],
  56. isShowTwoBut: true,
  57. twoButText: "好的",
  58. footTop: 10,
  59. },
  60. twoButCallback: () => { },
  61. toast_visible: false,
  62. // 后续新加需求
  63. deviceOther: null,
  64. updataDeviceInfo: false,
  65. repeatIndex: 0,
  66. deviceId: "",
  67. isQQAuth: false,
  68. isAuthXmly: false,
  69. qqAuthInfo: {},
  70. },
  71. onLoad(options) {
  72. var that = this;
  73. var deviceId = options.deviceId;
  74. var clientType = options.clientType;
  75. var deviceMac = lexin_util.getDeviceMacId(deviceId);
  76. that.data.deviceId = deviceId;
  77. that.data.deviceMac = deviceMac;
  78. that.data.clientType = clientType;
  79. deviceWakedetail({
  80. clientType: clientType,
  81. deviceMac: deviceMac,
  82. }).then((res) => {
  83. if (res && res.length > 0) {
  84. that.setData({
  85. wakeName: res[0].audioName || null
  86. });
  87. }
  88. })
  89. app.PubMsg({
  90. type: "get_dev_info",
  91. DstDeviceName: deviceMac
  92. });
  93. if (that.data.qqAuthInfo) {
  94. that.getAuthStatus();
  95. }
  96. // 赋值按钮回调
  97. that.setData({
  98. twoButCallback: () => {
  99. that.setData({
  100. toast_visible: false
  101. })
  102. }
  103. })
  104. },
  105. onShow(options) {
  106. var that = this;
  107. // var deviceMac = that.data.deviceMac;
  108. // app.PubMsg({
  109. // type: "get_dev_info",
  110. // DstDeviceName: deviceMac,
  111. // });
  112. },
  113. mqttCallback(type, option) {
  114. var that = this;
  115. let payloads = null;
  116. if (option) {
  117. payloads = JSON.parse(option.payload);
  118. };
  119. switch (type) {
  120. case "message_onoffline":
  121. if (payloads.state !== "online") {
  122. // option.payload: {"uuid":"AIrSMArT_7cdfa1fcbb24","state":"offline","userid":"1"}
  123. var deviceMacId = payloads.uuid;
  124. if (deviceMacId == that.data.deviceMac) {
  125. wx.showToast({
  126. title: '设备已断开连接',
  127. icon: 'none',
  128. duration: 2000
  129. });
  130. }
  131. }
  132. break;
  133. case "message":
  134. // 设备信息
  135. if (payloads.type === "get_dev_info") {
  136. app.PubMsg({
  137. type: "start_dm_auth",
  138. DstDeviceName: that.data.deviceMac
  139. });
  140. console.log("设备信息get_dev_info:", payloads);
  141. that.setData({
  142. deviceOther: payloads.other,
  143. updataDeviceInfo: true,
  144. });
  145. if (payloads.other.alarm) {
  146. payloads.other.alarm.map((v) => {
  147. if (v.alarm_id === "0") {
  148. v.action = "update";
  149. // 休眠
  150. that.setData({
  151. alarm_id_0: v
  152. });
  153. // 定时时间
  154. if (v.on_off_timestamp !== "") {
  155. that.setData({
  156. alarm_id_0_time: v.on_off_timestamp
  157. })
  158. }
  159. // 是否启用
  160. if (v.enable === "1") {
  161. that.setData({
  162. switch1Checked: true
  163. })
  164. } else {
  165. that.setData({
  166. switch1Checked: false
  167. })
  168. }
  169. // 重复
  170. if (v.week_actives) {
  171. that.setData({
  172. repeat0: that.repeatText(v.week_actives),
  173. })
  174. }
  175. } else if (v.alarm_id === "1") {
  176. // 唤醒
  177. that.setData({
  178. alarm_id_1: v
  179. });
  180. // 定时时间
  181. if (v.on_off_timestamp !== "") {
  182. that.setData({
  183. time: v.on_off_timestamp
  184. })
  185. }
  186. // 是否启用
  187. if (v.enable === "1") {
  188. that.setData({
  189. switch2Checked: true
  190. })
  191. } else {
  192. that.setData({
  193. switch2Checked: false
  194. })
  195. }
  196. // 重复
  197. if (v.week_actives) {
  198. that.setData({
  199. repeat: that.repeatText(v.week_actives),
  200. })
  201. }
  202. }
  203. })
  204. }
  205. } else if (payloads.type === "alert_set") {
  206. if (payloads.code === 200) {
  207. app.PubMsg({
  208. type: "get_dev_info",
  209. DstDeviceName: that.data.deviceMac
  210. });
  211. } else {
  212. wx.showModal({
  213. title: '保存失败',
  214. content: '请保持设备网络通畅并检查是否已连接电源?',
  215. success(res) {
  216. if (res.confirm) {
  217. console.log('用户点击确定')
  218. } else if (res.cancel) {
  219. console.log('用户点击取消')
  220. }
  221. }
  222. })
  223. }
  224. } else if (payloads.type === "start_dm_auth") {
  225. // {
  226. // "dsn": "431589788221452",
  227. // "authReqInfo": {
  228. // "codeChallenge": "YzJkMmU5NGU2Y2NlZjM1ODAwMDIzNjJjYmZmY2Y5OTJhY2Q0MmU1N2FiYzBkYWYyMzVmMDJlYzMxMTZiMjc1Mw==",
  229. // "sessionId": "1646821421"
  230. // }
  231. // }
  232. console.log("设备信息start_dm_auth:", payloads);
  233. var noDsn = that.data.qqAuthInfo == null;
  234. that.data.qqAuthInfo = payloads.other;
  235. if (noDsn) {
  236. that.getAuthStatus();
  237. }
  238. }
  239. default:
  240. }
  241. },
  242. getAuthStatus() {
  243. var that = this;
  244. // 这个界面只有wifi的?
  245. getAuthStatus(EnumPlatformId.QQ, that.getDeivce()).then((res) => {
  246. that.setData({ isQQAuth: res });
  247. });
  248. getAuthStatus(EnumPlatformId.XMLY, that.getDeivce()).then((res) => {
  249. that.setData({ isAuthXmly: res });
  250. });
  251. },
  252. // 休眠-选择时间
  253. switch1Change(e) {
  254. var that = this;
  255. let other = that.data.alarm_id_0;
  256. if (e.detail.value) {
  257. that.setData({
  258. switch1Checked: e.detail.value
  259. });
  260. const thisTime = Math.round(new Date() / 1000);
  261. other = that.setOther(other, 1);
  262. other.on_off_timestamp = that.data.alarm_id_0_time;
  263. // 有重复
  264. if (other.week_actives && other.week_actives.filter((v) => v === 1).length > 0) {
  265. // other.week_actives = [0,1,1,1,1,1,0],
  266. other.weekly_repeat = "1";
  267. } else {
  268. other.weekly_repeat = "0";
  269. };
  270. // 如果跨天了
  271. if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  272. const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  273. other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  274. };
  275. app.PubMsg({
  276. type: "alert_set",
  277. DstDeviceName: that.data.deviceMac,
  278. other
  279. });
  280. } else {
  281. that.setData({
  282. switch1Checked: e.detail.value
  283. });
  284. other = that.setOther(other, 0);
  285. app.PubMsg({
  286. type: "alert_set",
  287. DstDeviceName: that.data.deviceMac,
  288. other
  289. });
  290. }
  291. },
  292. switch2Change(e) {
  293. var that = this;
  294. that.data.updataDeviceInfo = false;
  295. let other = that.data.alarm_id_1;
  296. if (e.detail.value) {
  297. if (that.data.deviceOther && that.data.deviceOther.Power < 20) {
  298. that.setData({
  299. toast_visible: true,
  300. });
  301. };
  302. // app.PubMsg({
  303. // type: "get_dev_info",
  304. // DstDeviceName: that.data.deviceMac
  305. // });
  306. if (!other) {
  307. wx.showToast({
  308. title: '设备已断开连接',
  309. icon: "none"
  310. })
  311. return
  312. }
  313. other.on_off_timestamp = that.data.time;
  314. other = that.setOther(other, 1);
  315. let week_actives = other.week_actives ?? []
  316. if (week_actives.filter((v) => v === 1).length > 0) {
  317. // other.week_actives = [0,1,1,1,1,1,0],
  318. other.weekly_repeat = "1";
  319. } else {
  320. other.weekly_repeat = "0";
  321. };
  322. app.PubMsg({
  323. type: "alert_set",
  324. DstDeviceName: that.data.deviceMac,
  325. other
  326. });
  327. // 不要循环发送,也不要改alarm_id_0.enable
  328. // 循环查询
  329. // const Interval = setInterval(() => {
  330. // if (that.data.updataDeviceInfo) {
  331. // clearInterval(Interval);
  332. // other.on_off_timestamp = that.data.time;
  333. // other = that.setOther(other, 1);
  334. // if (other.week_actives.filter((v) => v === 1).length > 0) {
  335. // // other.week_actives = [0,1,1,1,1,1,0],
  336. // other.weekly_repeat = "1";
  337. // } else {
  338. // other.weekly_repeat = "0";
  339. // };
  340. // app.PubMsg({
  341. // type: "alert_set",
  342. // DstDeviceName: that.data.deviceMac,
  343. // other
  344. // });
  345. // 需打开休眠
  346. // if (that.data.alarm_id_0.enable === "0") {
  347. // const thisTime = Math.round(new Date() / 1000);
  348. // const alarm_id_0 = that.setOther(that.data.alarm_id_0, 1);
  349. // alarm_id_0.on_off_timestamp = that.data.alarm_id_0_time;
  350. // // 有重复
  351. // if (alarm_id_0.week_actives.filter((v) => v === 1).length > 0) {
  352. // alarm_id_0.weekly_repeat = "1";
  353. // } else {
  354. // alarm_id_0.weekly_repeat = "0";
  355. // };;
  356. // // 如果跨天了
  357. // if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  358. // const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  359. // alarm_id_0.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  360. // };
  361. // app.PubMsg({
  362. // type: "alert_set",
  363. // DstDeviceName: that.data.deviceMac,
  364. // other: alarm_id_0
  365. // });
  366. // }
  367. // }
  368. // }, 500);
  369. } else {
  370. other = that.setOther(other, 0);
  371. app.PubMsg({
  372. type: "alert_set",
  373. DstDeviceName: that.data.deviceMac,
  374. other
  375. });
  376. }
  377. },
  378. // bindPickerChange(e) {
  379. // var that = this;
  380. // let other = that.data.alarm_id_0;
  381. // const thisTime = Math.round(new Date() / 1000);
  382. // other = that.setOther(other, 1);
  383. // other.on_off_timestamp = that.formatTime(thisTime + that.data.timeArray[e.detail.value].id, "h:m:s");
  384. // // 如果跨天了
  385. // if(parseInt(that.formatTime(thisTime + that.data.timeArray[e.detail.value].id, "D")) > parseInt(that.formatTime(thisTime, "D"))) {
  386. // const date = new Date(that.formatTime(thisTime, "Y-M-D") + " 00:00:00");
  387. // other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  388. // };
  389. // app.PubMsg({type: "alert_set", DstDeviceName: that.data.deviceMac, other});
  390. // },
  391. bindTimeChange0: function (e) {
  392. var that = this;
  393. that.setData({
  394. alarm_id_0_time: e.detail.value + ":00"
  395. });
  396. let other = that.data.alarm_id_0;
  397. other.on_off_timestamp = e.detail.value + ":00";
  398. other = that.setOther(other, 1);
  399. // 有重复
  400. if (other.week_actives.filter((v) => v === 1).length > 0) {
  401. // other.week_actives = [0,1,1,1,1,1,0],
  402. other.weekly_repeat = "1";
  403. } else {
  404. other.weekly_repeat = "0";
  405. };
  406. // 如果跨天了
  407. const thisTime = Math.round(new Date() / 1000);
  408. if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  409. const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  410. other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  411. };
  412. app.PubMsg({
  413. type: "alert_set",
  414. DstDeviceName: that.data.deviceMac,
  415. other
  416. });
  417. },
  418. bindTimeChange: function (e) {
  419. var that = this;
  420. that.setData({
  421. time: e.detail.value + ":00"
  422. });
  423. let other = that.data.alarm_id_1;
  424. other.on_off_timestamp = e.detail.value + ":00";
  425. other = that.setOther(other, 1);
  426. app.PubMsg({
  427. type: "alert_set",
  428. DstDeviceName: that.data.deviceMac,
  429. other
  430. });
  431. },
  432. setRepeat(arr) {
  433. var that = this;
  434. let other = that.data.repeatIndex === "0" ? that.data.alarm_id_0 : that.data.alarm_id_1;
  435. other = that.setOther(other, 1);
  436. other.week_actives = arr;
  437. other.weekly_repeat = arr.filter((v) => v === 1).length > 0 ? "1" : "0";
  438. // 倒计时
  439. if (that.data.repeatIndex === "0") {
  440. // 如果跨天了
  441. const thisTime = Math.round(new Date() / 1000);
  442. if (!that.compareDate(that.data.alarm_id_0_time, that.formatTime(thisTime, "h:m:s"))) {
  443. const date = new Date((that.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
  444. other.current_timestamp = that.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
  445. };
  446. }
  447. app.PubMsg({
  448. type: "alert_set",
  449. DstDeviceName: that.data.deviceMac,
  450. other
  451. });
  452. },
  453. goWakeList() {
  454. var that = this;
  455. var deviceId = that.data.deviceId;
  456. var clientType = that.data.clientType;
  457. ///唤醒的设置特殊,智能这样用
  458. var deviceMac = lexin_util.getMacByDeviceId(deviceId);
  459. var param = '?clientType=' + clientType + "&deviceMac=" + deviceMac;
  460. routeUtil.jumpParam(routePath.wakeList, param);
  461. },
  462. gorepeat(e) {
  463. var that = this;
  464. that.data.repeatIndex = e.currentTarget.dataset.index;
  465. that.data.week_actives = e.currentTarget.dataset.index === "0" ? that.data.alarm_id_0.week_actives : that.data.alarm_id_1.week_actives;
  466. var param = '?week_actives=' + that.data.week_actives;
  467. routeUtil.jumpParam(routePath.repeat, param);
  468. },
  469. setdeviceWakedetail() {
  470. var that = this;
  471. deviceWakedetail({
  472. clientType: that.data.clientType,
  473. deviceMac: that.data.deviceMac,
  474. }).then((res) => {
  475. that.setData({
  476. wakeName: res[0].audioName
  477. })
  478. })
  479. },
  480. goQQAuth() {
  481. var that = this;
  482. if (that.data.isQQAuth) {
  483. wx.showModal({
  484. title: '解除授权',
  485. content: '确定要解除QQ音乐授权吗?',
  486. success(res) {
  487. if (res.confirm) {
  488. that.setData({
  489. isQQAuth: false
  490. });
  491. }
  492. }
  493. });
  494. } else {
  495. autoAuth(EnumPlatformId.QQ, that.getDeivce()).then((res) => {
  496. console.log("isQQAuth", res);
  497. if (res) {
  498. that.setData({
  499. isAuthXmly: true
  500. });
  501. } else if (res === false) {
  502. wx.showToast({
  503. title: '授权失败,请稍后再试',
  504. icon: 'none'
  505. });
  506. } else {
  507. // null的不处理
  508. }
  509. }).catch((error) => {
  510. console.error('Error during authorization:', error);
  511. wx.showToast({
  512. title: '授权失败,请稍后再试',
  513. icon: 'none'
  514. });
  515. });
  516. }
  517. },
  518. goXmlyAuth() {
  519. var that = this;
  520. console.log("goXmlyAuth", that.data.isAuthXmly)
  521. if (that.data.isAuthXmly) {
  522. wx.showModal({
  523. title: '解除授权',
  524. content: '确定要解除喜马拉雅授权吗?',
  525. success(res) {
  526. if (res.confirm) {
  527. unbind(that.getDeivce(), EnumPlatformId.XMLY).then((res) => {
  528. console.log("解绑喜马拉雅3:", res);
  529. that.setData({
  530. isAuthXmly: !res
  531. });
  532. });
  533. }
  534. }
  535. })
  536. } else {
  537. // routeUtil.jump(routePath.xmlyAuth);
  538. autoAuth(EnumPlatformId.XMLY, that.getDeivce()).then((res) => {
  539. console.log("isAuthXmly", res);
  540. if (res) {
  541. that.setData({
  542. isAuthXmly: true
  543. });
  544. } else if (res === false) {
  545. wx.showToast({
  546. title: '授权失败,请稍后再试',
  547. icon: 'none'
  548. });
  549. } else {
  550. // null的不处理
  551. }
  552. }).catch((error) => {
  553. console.error('Error during authorization:', error);
  554. wx.showToast({
  555. title: '授权失败,请稍后再试',
  556. icon: 'none'
  557. });
  558. });
  559. }
  560. },
  561. getDeivce() {
  562. var that = this;
  563. return { "clientType": that.data.clientType, "deviceMac": that.data.deviceMac, "deviceId": that.data.deviceId, "dsn": that.data.qqAuthInfo.dsn };
  564. },
  565. /**
  566. * 时间戳转化为年 月 日 时 分 秒
  567. * number: 传入时间戳
  568. * format:返回格式,支持自定义,但参数必须与formateArr里保持一致
  569. */
  570. formatTime(number, format) {
  571. var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  572. var returnArr = [];
  573. var date = new Date(number * 1000);
  574. returnArr.push(date.getFullYear());
  575. returnArr.push(formatNumber(date.getMonth() + 1));
  576. returnArr.push(formatNumber(date.getDate()));
  577. returnArr.push(formatNumber(date.getHours()));
  578. returnArr.push(formatNumber(date.getMinutes()));
  579. returnArr.push(formatNumber(date.getSeconds()));
  580. for (var i in returnArr) {
  581. format = format.replace(formateArr[i], returnArr[i]);
  582. }
  583. return format;
  584. },
  585. // type: 0 关闭 1 开启
  586. setOther(other, type) {
  587. var that = this;
  588. if (type === 0) {
  589. other.enable = "0";
  590. other.alarm_name = "close";
  591. other.operation = "off";
  592. } else if (type === 1) {
  593. other.enable = "1";
  594. other.alarm_name = "open";
  595. other.operation = "on";
  596. }
  597. const thisTime = Math.round(new Date() / 1000);
  598. other.current_timestamp = that.formatTime(thisTime, "Y-M-D h:m:s");
  599. other.action = "update",
  600. other.song_uri = "-1";
  601. other.version = 3;
  602. other.is_debug = app.globalData.is_debug
  603. return other
  604. },
  605. // 判断当天时间前后
  606. compareDate(t1, t2) {
  607. var date = new Date();
  608. var a = t1.split(":");
  609. var b = t2.split(":");
  610. return date.setHours(a[0], a[1]) > date.setHours(b[0], b[1]);
  611. },
  612. repeatText(arr) {
  613. var that = this;
  614. let text = "";
  615. if (arr.toString() === [0, 1, 1, 1, 1, 1, 0].toString()) {
  616. text = "工作日";
  617. } else if (arr.toString() === [1, 0, 0, 0, 0, 0, 1].toString()) {
  618. text = "周末";
  619. } else {
  620. arr.map((v, index) => {
  621. if (v === 1) {
  622. if (text === "") {
  623. text = text + that.data.week[index]
  624. } else {
  625. text = text + "、" + that.data.week[index];
  626. }
  627. }
  628. });
  629. if (text === "") {
  630. text = "不重复";
  631. };
  632. }
  633. return text.length < 20 ? text : "每日";
  634. }
  635. })