ok
Browse files- public/js/main.js +55 -4
public/js/main.js
CHANGED
@@ -18,7 +18,7 @@ async function executeCommand() {
|
|
18 |
output.textContent = '正在执行命令...';
|
19 |
|
20 |
try {
|
21 |
-
|
22 |
method: 'POST',
|
23 |
headers: {
|
24 |
'Content-Type': 'application/json',
|
@@ -26,9 +26,29 @@ async function executeCommand() {
|
|
26 |
},
|
27 |
body: JSON.stringify({ command })
|
28 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
if (!response.ok) {
|
30 |
throw new Error(`HTTP error! status: ${response.status}`);
|
31 |
}
|
|
|
32 |
const data = await response.json();
|
33 |
output.textContent = data.output || data.error || '命令执行成功,但没有输出。';
|
34 |
commandInput.value = '';
|
@@ -36,9 +56,11 @@ async function executeCommand() {
|
|
36 |
} catch (error) {
|
37 |
console.error('执行命令时出错:', error);
|
38 |
output.textContent = '错误: ' + (error.message || '未知错误');
|
39 |
-
if (error.message.includes('
|
40 |
-
alert('
|
41 |
localStorage.removeItem('token');
|
|
|
|
|
42 |
checkLoginStatus();
|
43 |
}
|
44 |
} finally {
|
@@ -86,6 +108,8 @@ async function login() {
|
|
86 |
const data = await response.json();
|
87 |
if (response.ok) {
|
88 |
localStorage.setItem('token', data.token);
|
|
|
|
|
89 |
document.getElementById('loginForm').style.display = 'none';
|
90 |
document.getElementById('commandInterface').style.display = 'block';
|
91 |
loadCommandHistory();
|
@@ -97,6 +121,34 @@ async function login() {
|
|
97 |
}
|
98 |
}
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
document.getElementById('loginButton').addEventListener('click', login);
|
101 |
|
102 |
loadCommandHistory();
|
@@ -112,5 +164,4 @@ function checkLoginStatus() {
|
|
112 |
|
113 |
// 在页面加载时调用此函数
|
114 |
window.addEventListener('load', checkLoginStatus);
|
115 |
-
|
116 |
document.getElementById('executeButton').addEventListener('click', executeCommand);
|
|
|
18 |
output.textContent = '正在执行命令...';
|
19 |
|
20 |
try {
|
21 |
+
let response = await fetch('/api/execute', {
|
22 |
method: 'POST',
|
23 |
headers: {
|
24 |
'Content-Type': 'application/json',
|
|
|
26 |
},
|
27 |
body: JSON.stringify({ command })
|
28 |
});
|
29 |
+
|
30 |
+
if (response.status === 403) {
|
31 |
+
// Token 可能已过期,尝试刷新
|
32 |
+
const refreshed = await refreshToken();
|
33 |
+
if (refreshed) {
|
34 |
+
// 重试请求
|
35 |
+
response = await fetch('/api/execute', {
|
36 |
+
method: 'POST',
|
37 |
+
headers: {
|
38 |
+
'Content-Type': 'application/json',
|
39 |
+
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
40 |
+
},
|
41 |
+
body: JSON.stringify({ command })
|
42 |
+
});
|
43 |
+
} else {
|
44 |
+
throw new Error('Token 刷新失败');
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
if (!response.ok) {
|
49 |
throw new Error(`HTTP error! status: ${response.status}`);
|
50 |
}
|
51 |
+
|
52 |
const data = await response.json();
|
53 |
output.textContent = data.output || data.error || '命令执行成功,但没有输出。';
|
54 |
commandInput.value = '';
|
|
|
56 |
} catch (error) {
|
57 |
console.error('执行命令时出错:', error);
|
58 |
output.textContent = '错误: ' + (error.message || '未知错误');
|
59 |
+
if (error.message.includes('Token 刷新失败')) {
|
60 |
+
alert('访问被拒绝。请重新登录。');
|
61 |
localStorage.removeItem('token');
|
62 |
+
localStorage.removeItem('username');
|
63 |
+
localStorage.removeItem('password');
|
64 |
checkLoginStatus();
|
65 |
}
|
66 |
} finally {
|
|
|
108 |
const data = await response.json();
|
109 |
if (response.ok) {
|
110 |
localStorage.setItem('token', data.token);
|
111 |
+
localStorage.setItem('username', username);
|
112 |
+
localStorage.setItem('password', password);
|
113 |
document.getElementById('loginForm').style.display = 'none';
|
114 |
document.getElementById('commandInterface').style.display = 'block';
|
115 |
loadCommandHistory();
|
|
|
121 |
}
|
122 |
}
|
123 |
|
124 |
+
async function refreshToken() {
|
125 |
+
const username = localStorage.getItem('username');
|
126 |
+
const password = localStorage.getItem('password');
|
127 |
+
if (!username || !password) {
|
128 |
+
// 如果没有保存的凭据,重定向到登录页面
|
129 |
+
document.getElementById('loginForm').style.display = 'block';
|
130 |
+
document.getElementById('commandInterface').style.display = 'none';
|
131 |
+
return;
|
132 |
+
}
|
133 |
+
try {
|
134 |
+
const response = await fetch('/api/login', {
|
135 |
+
method: 'POST',
|
136 |
+
headers: { 'Content-Type': 'application/json' },
|
137 |
+
body: JSON.stringify({ username, password })
|
138 |
+
});
|
139 |
+
const data = await response.json();
|
140 |
+
if (response.ok) {
|
141 |
+
localStorage.setItem('token', data.token);
|
142 |
+
return true;
|
143 |
+
} else {
|
144 |
+
throw new Error(data.error);
|
145 |
+
}
|
146 |
+
} catch (error) {
|
147 |
+
console.error('刷新 token 失败:', error);
|
148 |
+
return false;
|
149 |
+
}
|
150 |
+
}
|
151 |
+
|
152 |
document.getElementById('loginButton').addEventListener('click', login);
|
153 |
|
154 |
loadCommandHistory();
|
|
|
164 |
|
165 |
// 在页面加载时调用此函数
|
166 |
window.addEventListener('load', checkLoginStatus);
|
|
|
167 |
document.getElementById('executeButton').addEventListener('click', executeCommand);
|