deviceWake.js 19 KB

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