-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.html
More file actions
236 lines (212 loc) · 9.75 KB
/
Copy pathadmin.html
File metadata and controls
236 lines (212 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenCrawl - 管理后台</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
:root {
--bg: #0d1117; --surface: #161b22; --surface2: #1c2333;
--border: #30363d; --text: #e6edf3; --text2: #8b949e;
--green: #3fb950; --red: #f85149; --blue: #58a6ff; --yellow: #d29922;
}
body { background: var(--bg); color: var(--text); font-family: system-ui, sans-serif; font-size: 14px; }
.header {
display: flex; align-items: center; justify-content: space-between;
padding: 16px 24px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.header h1 { font-size: 18px; font-weight: 600; }
.header h1 span { color: var(--blue); }
.header .tag { color: var(--yellow); font-size: 12px; background: rgba(210,153,34,0.15); padding: 3px 10px; border-radius: 10px; }
.container { max-width: 960px; margin: 0 auto; padding: 20px 24px; }
/* 登录 */
.login-box {
max-width: 400px; margin: 80px auto; background: var(--surface);
border: 1px solid var(--border); border-radius: 8px; padding: 32px;
}
.login-box h2 { margin-bottom: 16px; font-size: 16px; }
.input { width: 100%; padding: 10px 12px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 14px; margin-bottom: 12px; }
.input:focus { outline: none; border-color: var(--blue); }
.btn { padding: 10px 20px; border-radius: 6px; border: none; font-size: 14px; font-weight: 600; cursor: pointer; }
.btn-primary { background: var(--blue); color: #fff; width: 100%; }
.btn-primary:hover { opacity: 0.9; }
.btn-green { background: var(--green); color: #fff; }
.btn-green:hover { opacity: 0.9; }
.btn-sm { padding: 5px 12px; font-size: 12px; border-radius: 4px; }
.error-msg { color: var(--red); font-size: 13px; margin-top: 8px; display: none; }
/* 统计 */
.stats { display: grid; grid-template-columns: repeat(4,1fr); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
.stat .label { color: var(--text2); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
.stat .val { font-size: 24px; font-weight: 700; margin-top: 4px; }
/* 操作区 */
.actions {
display: flex; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; align-items: flex-end;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 11px; color: var(--text2); text-transform: uppercase; }
.field input { padding: 8px 10px; border-radius: 4px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 13px; width: 180px; }
.field input:focus { outline: none; border-color: var(--blue); }
/* 表格 */
.section { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin-bottom: 16px; }
.section-header { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 13px; font-weight: 600; color: var(--text2); text-transform: uppercase; letter-spacing: .5px; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; padding: 10px 16px; font-size: 11px; color: var(--text2); border-bottom: 1px solid var(--border); text-transform: uppercase; }
td { padding: 10px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
tr:last-child td { border-bottom: none; }
tr:hover { background: var(--surface2); }
.mono { font-family: monospace; font-size: 12px; color: var(--text2); }
.credits { font-weight: 700; }
.credits.pos { color: var(--green); }
.credits.low { color: var(--red); }
.toast { position: fixed; top: 20px; right: 20px; background: var(--green); color: #fff; padding: 10px 20px; border-radius: 6px; font-size: 13px; display: none; z-index: 99; }
</style>
</head>
<body>
<div class="header">
<h1><span>Open</span>Crawl</h1>
<span class="tag">ADMIN</span>
</div>
<!-- 登录 -->
<div id="loginView">
<div class="login-box">
<h2>管理员登录</h2>
<input id="adminKey" class="input" type="password" placeholder="Admin Key">
<button class="btn btn-primary" onclick="login()">登录</button>
<div id="loginError" class="error-msg"></div>
</div>
</div>
<!-- 管理面板 -->
<div id="adminView" style="display:none">
<div class="container">
<div class="stats">
<div class="stat"><div class="label">总用户</div><div class="val" id="sUsers">0</div></div>
<div class="stat"><div class="label">总积分</div><div class="val" id="sCredits" style="color:var(--blue)">0</div></div>
<div class="stat"><div class="label">总消费</div><div class="val" id="sUsed" style="color:var(--yellow)">0</div></div>
<div class="stat"><div class="label">总赚取</div><div class="val" id="sEarned" style="color:var(--green)">0</div></div>
</div>
<div class="actions">
<div class="field">
<label>用户名</label>
<input id="newName" placeholder="名称">
</div>
<div class="field">
<label>初始积分</label>
<input id="newCredits" type="number" value="100">
</div>
<button class="btn btn-green" onclick="createUser()">创建用户</button>
<div style="border-left:1px solid var(--border);height:36px;margin:0 8px"></div>
<div class="field">
<label>API Key</label>
<input id="rechargeKey" placeholder="ak_xxx">
</div>
<div class="field">
<label>充值积分</label>
<input id="rechargeAmount" type="number" value="100">
</div>
<button class="btn btn-green" onclick="recharge()">充值</button>
</div>
<div class="section">
<div class="section-header">用户列表</div>
<div id="userTable"></div>
</div>
</div>
</div>
<div id="toast" class="toast"></div>
<script>
let adminKey = '';
async function login() {
adminKey = document.getElementById('adminKey').value.trim();
try {
const res = await api('GET', '/api/status');
document.getElementById('loginView').style.display = 'none';
document.getElementById('adminView').style.display = 'block';
loadUsers();
} catch(e) {
document.getElementById('loginError').style.display = 'block';
document.getElementById('loginError').textContent = '登录失败';
}
}
async function loadUsers() {
try {
const res = await api('GET', '/api/admin/users');
renderUsers(res.users);
renderStats(res.stats);
} catch(e) {
// fallback
}
}
function renderStats(s) {
if (!s) return;
document.getElementById('sUsers').textContent = s.totalUsers || 0;
document.getElementById('sCredits').textContent = s.totalCredits || 0;
document.getElementById('sUsed').textContent = s.totalUsed || 0;
document.getElementById('sEarned').textContent = s.totalEarned || 0;
}
function renderUsers(users) {
if (!users || users.length === 0) {
document.getElementById('userTable').innerHTML = '<div style="padding:40px;text-align:center;color:var(--text2)">暂无用户</div>';
return;
}
let h = '<table><tr><th>名称</th><th>API Key</th><th>积分</th><th>已用</th><th>赚取</th><th>创建日期</th><th>操作</th></tr>';
users.forEach(u => {
const cc = u.credits > 10 ? 'pos' : 'low';
h += `<tr>
<td>${esc(u.name)}</td>
<td class="mono">${esc(u.apiKey)}</td>
<td class="credits ${cc}">${u.credits}</td>
<td>${u.totalUsed || 0}</td>
<td>${u.totalEarned || 0}</td>
<td class="mono">${u.created || '-'}</td>
<td><button class="btn btn-green btn-sm" onclick="quickRecharge('${esc(u.apiKey)}')">+100</button></td>
</tr>`;
});
document.getElementById('userTable').innerHTML = h + '</table>';
}
async function createUser() {
const name = document.getElementById('newName').value.trim() || '未命名';
const credits = parseInt(document.getElementById('newCredits').value) || 100;
try {
const res = await api('POST', '/api/admin/create-key', { name, credits });
showToast(`已创建: ${res.apiKey}`);
document.getElementById('newName').value = '';
loadUsers();
} catch(e) { showToast('创建失败: ' + e.message); }
}
async function recharge() {
const apiKey = document.getElementById('rechargeKey').value.trim();
const credits = parseInt(document.getElementById('rechargeAmount').value) || 100;
if (!apiKey) { showToast('请输入 API Key'); return; }
try {
await api('POST', '/api/admin/recharge', { apiKey, credits });
showToast(`已充值 ${credits} 积分`);
loadUsers();
} catch(e) { showToast('充值失败: ' + e.message); }
}
async function quickRecharge(apiKey) {
try {
await api('POST', '/api/admin/recharge', { apiKey, credits: 100 });
showToast('已充值 100 积分');
loadUsers();
} catch(e) { showToast('失败: ' + e.message); }
}
async function api(method, path, body) {
const opts = { method, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + adminKey } };
if (body) opts.body = JSON.stringify(body);
const res = await fetch(path, opts);
const data = await res.json();
if (!res.ok) throw new Error(data.detail || data.error || 'Error');
return data;
}
function showToast(msg) {
const t = document.getElementById('toast');
t.textContent = msg; t.style.display = 'block';
setTimeout(() => t.style.display = 'none', 3000);
}
function esc(s) { return (s||'').replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"'); }
// Enter 键登录
document.getElementById('adminKey').addEventListener('keydown', e => { if(e.key==='Enter') login(); });
</script>
</body>
</html>