page3.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. let timeLogLevel = "";
  2. function setTimeLogLevel() {
  3. const level = logLevel.value;
  4. fetch("/api/time/loglevel", {
  5. method: "POST",
  6. headers: {"Content-Type": "application/json"},
  7. body: JSON.stringify({level})
  8. })
  9. .then(async r => {
  10. if (!r.ok) throw new Error(await r.text());
  11. timeLogLevel = level;
  12. })
  13. .catch(e => {
  14. logLevel.value = timeLogLevel;
  15. });
  16. }
  17. function getTimeLogLevel() {
  18. fetch("/api/time/loglevel")
  19. .then(async r => {
  20. if (!r.ok) throw new Error(await r.text());
  21. return r.json();
  22. })
  23. .then(d => {
  24. timeLogLevel = d.log_level;
  25. logLevel.value = d.log_level;
  26. })
  27. .catch(e => {
  28. timeLogLevel = "";
  29. logLevel.value = "";
  30. });
  31. }
  32. function downloadTimeLogFile(event) {
  33. const btn = event.currentTarget;
  34. btn.disabled = true;
  35. fetch("/api/time/logfile")
  36. .then(async r => {
  37. if (!r.ok) {
  38. throw new Error("HTTP " + r.status);
  39. }
  40. const disposition = r.headers.get("Content-Disposition");
  41. let filename = "time.log.tar.gz";
  42. if (disposition) {
  43. const match = disposition.match(/filename="?([^";]+)"?/);
  44. if (match) {
  45. filename = match[1];
  46. }
  47. }
  48. return {
  49. blob: await r.blob(),
  50. filename
  51. };
  52. })
  53. .then(({ blob, filename }) => {
  54. const url = URL.createObjectURL(blob);
  55. const a = document.createElement("a");
  56. a.href = url;
  57. a.download = filename;
  58. document.body.appendChild(a);
  59. a.click();
  60. a.remove();
  61. setTimeout(() => {
  62. URL.revokeObjectURL(url);
  63. }, 1000);
  64. alert("下载成功");
  65. })
  66. .catch(e => {
  67. console.error("download failed:", e);
  68. alert("下载失败: " + e.message);
  69. })
  70. .finally(() => {
  71. btn.disabled = false;
  72. btn.textContent = "下载";
  73. });
  74. }
  75. function restartTimeService(event) {
  76. if (!confirm("确定重启时间服务吗?")) {
  77. return;
  78. }
  79. const btn = event.currentTarget;
  80. btn.disabled = true;
  81. fetch("/api/time/restart", {
  82. method: "POST"
  83. })
  84. .then(async r => {
  85. if (!r.ok) {
  86. throw new Error(await r.text());
  87. }
  88. alert("重启成功");
  89. })
  90. .catch(e => {
  91. console.error("restart failed:", e);
  92. alert("重启失败: " + e.message);
  93. })
  94. .finally(() => {
  95. btn.disabled = false;
  96. });
  97. }
  98. let timeStatusTimer = null;
  99. function getTimeStatus() {
  100. fetch("/api/time/status")
  101. .then(async r => {
  102. if (!r.ok) {
  103. throw new Error("HTTP " + r.status);
  104. }
  105. return r.json();
  106. })
  107. .then(data => {
  108. ["var1","var2","var3","var4","var5","var6"]
  109. .forEach(id => setText(id, data[id], "page3"));
  110. })
  111. .catch(e => {
  112. console.error("getTimeStatus() failed:", e);
  113. ["var1","var2","var3","var4","var5","var6"]
  114. .forEach(id => setText(id, null, "page3"));
  115. });
  116. getTimeLogLevel();
  117. }
  118. function startTimeStatusRefresh() {
  119. if (timeStatusTimer) {
  120. return;
  121. }
  122. getTimeStatus();
  123. timeStatusTimer = setInterval(() => {
  124. getTimeStatus();
  125. }, 1000);
  126. }
  127. function stopTimeStatusRefresh() {
  128. if (timeStatusTimer) {
  129. clearInterval(timeStatusTimer);
  130. timeStatusTimer = null;
  131. }
  132. }
  133. function initPage3() {
  134. startTimeStatusRefresh()
  135. connectLog("yfkj-timesyncd.service", "text1");
  136. }
  137. function exitPage3() {
  138. stopTimeStatusRefresh()
  139. disconnectLog();
  140. }