page6.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. let appInsLogLevel = "";
  2. function setAppInstallLogLevel() {
  3. const level = logLevel.value;
  4. fetch("/api/app-install/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. appInsLogLevel = level;
  12. })
  13. .catch(e => {
  14. logLevel.value = appInsLogLevel;
  15. });
  16. }
  17. function getAppInstallLevel() {
  18. fetch("/api/app-install/loglevel")
  19. .then(async r => {
  20. if (!r.ok) throw new Error(await r.text());
  21. return r.json();
  22. })
  23. .then(d => {
  24. appInsLogLevel = d.log_level;
  25. logLevel.value = d.log_level;
  26. })
  27. .catch(e => {
  28. appInsLogLevel = "";
  29. logLevel.value = "";
  30. });
  31. }
  32. function downloadAppInstallLogFile(event) {
  33. const btn = event.currentTarget;
  34. btn.disabled = true;
  35. fetch("/api/app-install/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 = "app-install.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 restartAppInstallService(event) {
  76. if (!confirm("确定重启应该管理服务吗?")) {
  77. return;
  78. }
  79. const btn = event.currentTarget;
  80. btn.disabled = true;
  81. fetch("/api/app-install/restart", {
  82. method: "POST"
  83. })
  84. .then(async r => {
  85. if (!r.ok) {
  86. throw new Error(await r.text());
  87. }
  88. logLevel.value = "info";
  89. appInsLogLevel = logLevel.value;
  90. disconnectLog();
  91. connectLog("yfkj-app-install.service", "text1");
  92. alert("重启成功");
  93. })
  94. .catch(e => {
  95. console.error("restart failed:", e);
  96. alert("重启失败: " + e.message);
  97. })
  98. .finally(() => {
  99. btn.disabled = false;
  100. });
  101. }
  102. let appInsStatusTimer = null;
  103. function getAppInstallStatus() {
  104. fetch("/api/app-install/status")
  105. .then(async r => {
  106. if (!r.ok) {
  107. throw new Error("HTTP " + r.status);
  108. }
  109. return r.json();
  110. })
  111. .then(data => {
  112. [
  113. "avar1", "avar2", "avar3", "avar4",
  114. "bvar1", "bvar2", "bvar3", "bvar4", "bvar5",
  115. "cvar1", "cvar2", "cvar3", "cvar4", "cvar5"
  116. ].forEach(id => {
  117. setText(id, data[id], "page6");
  118. });
  119. updateAppInstallButtons(Number(data.state));
  120. })
  121. .catch(e => {
  122. console.error("getAppInstallStatus() failed:", e);
  123. [
  124. "avar1", "avar2", "avar3", "avar4",
  125. "bvar1", "bvar2", "bvar3", "bvar4", "bvar5",
  126. "cvar1", "cvar2", "cvar3", "cvar4", "cvar5"
  127. ].forEach(id => {
  128. setText(id, null, "page6");
  129. });
  130. updateAppInstallButtons(-1);
  131. });
  132. getAppInstallLevel();
  133. }
  134. function startAppInstallStatusRefresh() {
  135. if (appInsStatusTimer) {
  136. return;
  137. }
  138. getAppInstallStatus();
  139. appInsStatusTimer = setInterval(() => {
  140. getAppInstallStatus();
  141. }, 5000);
  142. }
  143. function stopAppInstallStatusRefresh() {
  144. if (appInsStatusTimer) {
  145. clearInterval(appInsStatusTimer);
  146. appInsStatusTimer = null;
  147. }
  148. }
  149. function updateAppInstallButtons(state) {
  150. switch (state) {
  151. case 0: // 无安装包, 等待上传
  152. setBtnStatus("btn1", true, "page6")
  153. setBtnStatus("btn2", false, "page6")
  154. setBtnStatus("btn3", false, "page6")
  155. setBtnStatus("btn4", false, "page6")
  156. break;
  157. case 1: // 有安装包, 未安装时
  158. setBtnStatus("btn1", false, "page6")
  159. setBtnStatus("btn2", true, "page6")
  160. setBtnStatus("btn3", true, "page6")
  161. setBtnStatus("btn4", false, "page6")
  162. break;
  163. case 2: // 有安装包, 已安装时
  164. setBtnStatus("btn1", false, "page6")
  165. setBtnStatus("btn2", false, "page6")
  166. setBtnStatus("btn3", false, "page6")
  167. setBtnStatus("btn4", true, "page6")
  168. break;
  169. default:
  170. setBtnStatus("btn1", true, "page6")
  171. setBtnStatus("btn2", true, "page6")
  172. setBtnStatus("btn3", true, "page6")
  173. setBtnStatus("btn4", true, "page6")
  174. break;
  175. }
  176. }
  177. let userAppUploading = false;
  178. function onBtn1Click() {
  179. if (userAppUploading) {
  180. return;
  181. }
  182. document.getElementById("userAppFile").click();
  183. }
  184. function uploadUserAppFile(input) {
  185. let file = input.files[0];
  186. if (!file) {
  187. return;
  188. }
  189. if (!file.name.toLowerCase().endsWith(".tar.gz")) {
  190. alert("只允许上传 tar.gz 类型的文件");
  191. input.value = "";
  192. return;
  193. }
  194. userAppUploading = true;
  195. let formData = new FormData();
  196. formData.append("file", file);
  197. let xhr = new XMLHttpRequest();
  198. xhr.open("POST", "/api/app-install/uploadUserAPP", true);
  199. xhr.timeout = 600000; // 10分钟
  200. xhr.upload.onprogress = function(e) {
  201. if (!e.lengthComputable) {
  202. return;
  203. }
  204. let percent = Math.floor(e.loaded * 100 / e.total);
  205. setText("bvar5", "正在上传 " + file.name + " (" + percent + "%)", "page6");
  206. };
  207. xhr.onload = function() {
  208. userAppUploading = false;
  209. input.value = "";
  210. if (xhr.status !== 200) {
  211. setText("bvar5", "上传失败: " + xhr.responseText, "page6");
  212. return;
  213. }
  214. setText("bvar5", "上传完成,等待下一步处理", "page6");
  215. };
  216. xhr.onerror = function() {
  217. userAppUploading = false;
  218. input.value = "";
  219. setText("bvar5", "上传失败,请检查网络连接", "page6");
  220. };
  221. xhr.ontimeout = function() {
  222. userAppUploading = false;
  223. input.value = "";
  224. setText("bvar5", "上传超时,请稍后重新尝试", "page6");
  225. };
  226. xhr.send(formData);
  227. }
  228. function initPage6() {
  229. startAppInstallStatusRefresh()
  230. connectLog("yfkj-app-install.service", "text1");
  231. }
  232. function exitPage6() {
  233. stopAppInstallStatusRefresh()
  234. disconnectLog();
  235. }