-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
505 lines (479 loc) · 20.7 KB
/
Copy pathindex.php
File metadata and controls
505 lines (479 loc) · 20.7 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据库管理 - PHP + MySQL</title>
<style>
* { box-sizing: border-box; }
body { font-family: -apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
margin: 0; color: #222; }
a { color: #2d7ff9; }
button { padding: 6px 12px; border: none; border-radius: 6px; background: #2d7ff9;
color: #fff; font-size: 13px; cursor: pointer; }
button:hover { background: #1c6fe0; }
button.ghost { background: #eee; color: #333; }
button.danger { background: #e23; }
input, select { padding: 6px 8px; border: 1px solid #ccc; border-radius: 6px; font-size: 13px; }
/* 登录层 */
#login { position: fixed; inset: 0; background: #f2f4f8; display: flex;
align-items: center; justify-content: center; z-index: 50; }
#login .card { width: 320px; }
.crow { display: flex; gap: 6px; margin-bottom: 6px; align-items: center; }
.crow input { flex: 1; }
.crow select { flex: 1; }
/* 顶栏 */
#top { display: flex; gap: 8px; align-items: center; padding: 8px 12px;
background: #2d7ff9; color: #fff; }
#top .spacer { flex: 1; }
#top .who { font-size: 14px; }
#top button { background: rgba(255,255,255,.2); }
#top button:hover { background: rgba(255,255,255,.35); }
/* 主体 */
#app { display: none; flex-direction: column; height: 100vh; }
#wrap { display: flex; flex: 1; overflow: hidden; }
#side { width: 220px; background: #f7f8fa; border-right: 1px solid #eee;
padding: 12px; overflow-y: auto; }
#side h3 { font-size: 13px; color: #888; margin: 0 0 8px; }
.titem { padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 14px; }
.titem:hover { background: #e9eef7; }
.titem.active { background: #2d7ff9; color: #fff; }
#main { flex: 1; padding: 16px; overflow: auto; }
#main h2 { margin: 0 0 12px; font-size: 18px; }
.bar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { border: 1px solid #eee; padding: 6px 8px; text-align: left; }
th { background: #f7f8fa; position: sticky; top: 0; }
td input { width: 100%; box-sizing: border-box; }
.msg { padding: 8px 10px; border-radius: 6px; margin-bottom: 10px; display: none; }
.msg.err { background: #fdecea; color: #c0392b; display: block; }
.msg.ok { background: #eafaf1; color: #1e8449; display: block; }
/* 弹窗 */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: none;
align-items: center; justify-content: center; z-index: 40; }
.modal.show { display: flex; }
.card { background: #fff; border-radius: 10px; padding: 18px; width: 560px; max-width: 92vw;
max-height: 88vh; overflow: auto; }
.card h3 { margin: 0 0 12px; }
.muted { color: #999; font-size: 12px; }
.pager { margin-top: 10px; display: flex; gap: 8px; align-items: center; }
.userRow { display: flex; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid #f0f0f0; }
.userRow .un { flex: 1; font-size: 14px; }
</style>
</head>
<body>
<!-- 登录层 -->
<div id="login">
<div class="card">
<h3>登录数据库管理</h3>
<div class="crow"><input id="lu" placeholder="用户名" autofocus></div>
<div class="crow"><input id="lp" type="password" placeholder="密码"></div>
<div id="lmsg" class="msg err" style="display:none;"></div>
<button id="loginBtn" style="width:100%;">登录</button>
<div class="muted" style="margin-top:8px;">默认管理员 admin / admin123,首次登录后请修改密码。</div>
</div>
</div>
<!-- 主应用 -->
<div id="app">
<div id="top">
<span class="who" id="who"></span>
<span class="spacer"></span>
<button class="ghost" id="adminBtn" style="display:none;">用户管理</button>
<button id="logoutBtn">退出登录</button>
</div>
<div id="wrap">
<div id="side">
<h3>数据表</h3>
<div id="tableList"></div>
<div class="bar" style="margin-top:12px;">
<button class="ghost" id="newTableBtn">+ 新建表</button>
</div>
</div>
<div id="main">
<div id="msg" class="msg"></div>
<h2 id="title">请选择左侧的表</h2>
<div class="bar" id="toolbar" style="display:none;">
<button class="ghost" id="addRowBtn">+ 新增一行</button>
<button class="ghost" id="delTableBtn" style="margin-left:auto;">删除此表</button>
</div>
<div id="grid"></div>
<div class="pager" id="pager" style="display:none;">
<button class="ghost" id="prevPage">上一页</button>
<span id="pageInfo" class="muted"></span>
<button class="ghost" id="nextPage">下一页</button>
<span class="muted">排序:</span>
<select id="orderBy"></select>
<select id="orderDir"><option value="ASC">升序</option><option value="DESC">降序</option></select>
</div>
</div>
</div>
</div>
<!-- 新建表弹窗 -->
<div class="modal" id="tableModal">
<div class="card">
<h3>新建数据表</h3>
<div class="crow">
<input id="newTableName" placeholder="表名(英文/数字/下划线)">
</div>
<div id="colDefs"></div>
<button class="ghost" id="addColBtn" style="margin:6px 0 12px;">+ 加一列</button>
<div>
<button id="createTableOk">创建</button>
<button class="ghost" id="tableCancel">取消</button>
</div>
<div class="muted" style="margin-top:8px;">提示:至少一列设为「主键」;自增主键通常用 INT + 主键 + 自增。</div>
</div>
</div>
<!-- 用户管理弹窗 -->
<div class="modal" id="userModal">
<div class="card">
<h3>用户管理(管理员)</h3>
<div class="crow">
<input id="nu" class="cn" placeholder="新用户名">
<input id="np" type="password" placeholder="密码(至少6位)">
<label class="muted"><input type="checkbox" id="na">管理员</label>
<button id="createUserBtn">创建</button>
</div>
<div id="userList" style="margin-top:10px;"></div>
<div style="margin-top:12px;"><button class="ghost" id="userCancel">关闭</button></div>
</div>
</div>
<!-- 修改密码弹窗 -->
<div class="modal" id="chgModal">
<div class="card">
<h3>修改密码</h3>
<div class="crow"><input id="co" type="password" placeholder="原密码(管理员重置他人时无需填)"></div>
<div class="crow"><input id="cn" type="password" placeholder="新密码(至少6位)"></div>
<div class="crow" id="chgTargetRow" style="display:none;">
<input id="chgTarget" placeholder="要重置密码的用户名">
</div>
<div>
<button id="chgSave">保存</button>
<button class="ghost" id="chgCancel">取消</button>
</div>
</div>
</div>
<script>
const $ = (s) => document.querySelector(s);
const api = (action, opts = {}) => {
const url = 'api.php?action=' + action + (opts.q || '');
return fetch(url, {
method: opts.method || 'GET',
headers: opts.body ? {'Content-Type': 'application/json'} : {},
body: opts.body ? JSON.stringify(opts.body) : undefined
}).then(async r => {
const txt = await r.text();
try { return JSON.parse(txt); }
catch (e) { return { error: '服务器返回异常(HTTP ' + r.status + '):' + txt.replace(/\s+/g, ' ').slice(0, 600) }; }
});
};
let me = null;
let state = { table: null, columns: [], rows: [], page: 1, size: 50, orderBy: '', dir: 'ASC' };
function msg(text, ok) {
const m = $('#msg');
m.textContent = text;
m.className = 'msg ' + (ok ? 'ok' : 'err');
if (ok) setTimeout(() => { m.style.display = 'none'; }, 2500);
}
function showApp() { $('#login').style.display = 'none'; $('#app').style.display = 'flex'; }
function showLogin() {
$('#app').style.display = 'none'; $('#login').style.display = 'flex';
$('#lmsg').style.display = 'none'; $('#lp').value = '';
}
// 启动:探测登录态
async function boot() {
const d = await api('me');
if (d.error) { showLogin(); return; }
me = d.user;
$('#who').textContent = me.username + (me.is_admin ? '(管理员)' : '');
$('#adminBtn').style.display = me.is_admin ? 'inline-block' : 'none';
showApp();
loadTables();
if (me.must_change) openChange();
}
/* ---------- 登录 / 登出 ---------- */
$('#loginBtn').onclick = async () => {
const d = await api('login', { method: 'POST', body: { username: $('#lu').value, password: $('#lp').value } });
if (d.error) { const m = $('#lmsg'); m.textContent = d.error; m.style.display = 'block'; return; }
$('#lu').value = ''; $('#lp').value = '';
boot();
};
$('#lp').addEventListener('keydown', e => { if (e.key === 'Enter') $('#loginBtn').click(); });
$('#logoutBtn').onclick = async () => { await api('logout'); me = null; showLogin(); };
/* ---------- 用户管理(管理员) ---------- */
$('#adminBtn').onclick = async () => {
const d = await api('listUsers');
if (d.error) return msg(d.error);
const box = $('#userList'); box.innerHTML = '';
d.users.forEach(u => {
const row = document.createElement('div'); row.className = 'userRow';
const name = document.createElement('span'); name.className = 'un';
name.textContent = u.username + (u.is_admin ? '(管理员)' : '');
row.appendChild(name);
if (!u.is_admin || u.username !== me.username) {
const reset = document.createElement('button'); reset.className = 'ghost'; reset.textContent = '重置密码';
reset.onclick = async () => {
const pw = prompt('为 ' + u.username + ' 设置新密码(至少6位)');
if (!pw) return;
const r = await api('changePassword', { method: 'POST', body: { username: u.username, new: pw } });
if (r.error) return msg(r.error); msg('已重置 ' + u.username + ' 的密码', true);
};
row.appendChild(reset);
const del = document.createElement('button'); del.className = 'danger'; del.textContent = '删除';
del.onclick = async () => {
if (!confirm('确定删除用户 ' + u.username + '?')) return;
const r = await api('deleteUser', { method: 'POST', body: { username: u.username } });
if (r.error) return msg(r.error); msg('已删除 ' + u.username, true); $('#adminBtn').click();
};
row.appendChild(del);
}
box.appendChild(row);
});
$('#userModal').classList.add('show');
};
$('#createUserBtn').onclick = async () => {
const username = $('#nu').value.trim();
const password = $('#np').value;
const is_admin = $('#na').checked;
const d = await api('createUser', { method: 'POST', body: { username, password, is_admin } });
if (d.error) return msg(d.error);
msg('已创建用户 ' + username, true);
$('#nu').value = ''; $('#np').value = ''; $('#na').checked = false;
$('#adminBtn').click();
};
$('#userCancel').onclick = () => $('#userModal').classList.remove('show');
/* ---------- 修改密码 ---------- */
function openChange() {
$('#chgTargetRow').style.display = me.is_admin ? 'flex' : 'none';
$('#chgModal').classList.add('show');
}
$('#chgSave').onclick = async () => {
const body = { old: $('#co').value, new: $('#cn').value };
if (me.is_admin && $('#chgTarget').value.trim()) body.username = $('#chgTarget').value.trim();
const d = await api('changePassword', { method: 'POST', body });
if (d.error) return msg(d.error);
msg('密码已更新', true);
$('#chgModal').classList.remove('show');
$('#co').value = ''; $('#cn').value = ''; $('#chgTarget').value = '';
if (me.must_change) { me.must_change = false; }
};
$('#chgCancel').onclick = () => $('#chgModal').classList.remove('show');
/* ---------- 表操作(与原逻辑一致) ---------- */
function loadTables() {
api('tables').then(d => {
if (d.error) return msg(d.error);
const box = $('#tableList'); box.innerHTML = '';
d.tables.forEach(t => {
const div = document.createElement('div');
div.className = 'titem' + (t === state.table ? ' active' : '');
div.textContent = t;
div.onclick = () => selectTable(t);
box.appendChild(div);
});
});
}
function selectTable(name) {
state.table = name; state.page = 1; state.orderBy = ''; state.dir = 'ASC';
loadTables();
api('structure', { q: '&table=' + encodeURIComponent(name) }).then(d => {
if (d.error) return msg(d.error);
state.columns = d.columns;
$('#title').textContent = '表:' + name;
$('#toolbar').style.display = 'flex';
$('#pager').style.display = 'flex';
const ob = $('#orderBy'); ob.innerHTML = '<option value="">(默认)</option>';
d.columns.forEach(c => {
const o = document.createElement('option'); o.value = c.name; o.textContent = c.name; ob.appendChild(o);
});
loadRows();
});
}
function loadRows() {
const q = '&table=' + encodeURIComponent(state.table) +
'&page=' + state.page + '&size=' + state.size +
'&orderBy=' + encodeURIComponent(state.orderBy) +
'&dir=' + state.dir;
api('rows', { q }).then(d => {
if (d.error) return msg(d.error);
state.rows = d.rows; state.columns = d.columns;
renderGrid();
$('#pageInfo').textContent = '第 ' + d.page + ' 页 / 共 ' + Math.ceil(d.total / d.size) + ' 页(' + d.total + ' 条)';
});
}
function renderGrid() {
const cols = state.columns;
const grid = $('#grid'); grid.innerHTML = '';
const table = document.createElement('table');
const thead = document.createElement('thead');
const htr = document.createElement('tr');
cols.forEach(c => {
const th = document.createElement('th');
th.textContent = c.name + (c.primary ? ' *' : '');
htr.appendChild(th);
});
const thOp = document.createElement('th'); thOp.textContent = '操作'; thOp.style.width = '120px';
htr.appendChild(thOp);
thead.appendChild(htr); table.appendChild(thead);
const tbody = document.createElement('tbody');
state.rows.forEach((row) => {
const tr = document.createElement('tr');
cols.forEach(c => {
const td = document.createElement('td');
const inp = document.createElement('input');
inp.value = row[c.name] === null ? '' : row[c.name];
inp.disabled = true;
if (c.autoIncrement) inp.title = '自增,无需填写';
td.appendChild(inp);
tr.appendChild(td);
});
const tdOp = document.createElement('td');
const edit = document.createElement('button'); edit.className = 'ghost'; edit.textContent = '编辑';
edit.onclick = () => startEdit(tr, row);
const del = document.createElement('button'); del.className = 'danger'; del.textContent = '删除';
del.onclick = () => deleteRow(row);
tdOp.appendChild(edit); tdOp.appendChild(del);
tr.appendChild(tdOp);
tbody.appendChild(tr);
});
table.appendChild(tbody);
grid.appendChild(table);
}
function pkOf(row) {
const pk = {};
state.columns.filter(c => c.primary).forEach(c => { pk[c.name] = row[c.name]; });
return pk;
}
function startEdit(tr, row) {
const cols = state.columns;
tr.querySelectorAll('input').forEach((inp, i) => {
const c = cols[i];
inp.disabled = c.autoIncrement;
});
const tdOp = tr.lastChild;
tdOp.innerHTML = '';
const save = document.createElement('button'); save.textContent = '保存';
const cancel = document.createElement('button'); cancel.className = 'ghost'; cancel.textContent = '取消';
save.onclick = () => {
const values = {};
tr.querySelectorAll('input').forEach((inp, i) => {
const c = cols[i];
if (!c.autoIncrement) values[c.name] = inp.value;
});
api('update', { method: 'POST', q: '&table=' + encodeURIComponent(state.table),
body: { pk: pkOf(row), values } })
.then(d => {
if (d.error) return msg(d.error);
msg('已保存', true); loadRows();
});
};
cancel.onclick = () => loadRows();
tdOp.appendChild(save); tdOp.appendChild(cancel);
}
function deleteRow(row) {
if (!confirm('确定删除这一行?')) return;
api('delete', { method: 'POST', q: '&table=' + encodeURIComponent(state.table),
body: { pk: pkOf(row) } })
.then(d => {
if (d.error) return msg(d.error);
msg('已删除', true); loadRows();
});
}
let adding = false;
$('#addRowBtn').onclick = () => {
if (adding) return; adding = true;
const cols = state.columns;
const tr = document.createElement('tr');
tr.style.background = '#fffbe6';
cols.forEach(c => {
const td = document.createElement('td');
const inp = document.createElement('input');
inp.placeholder = c.autoIncrement ? '(自增)' : '';
inp.disabled = c.autoIncrement;
td.appendChild(inp); tr.appendChild(td);
});
const tdOp = document.createElement('td');
const save = document.createElement('button'); save.textContent = '保存';
const cancel = document.createElement('button'); cancel.className = 'ghost'; cancel.textContent = '取消';
save.onclick = () => {
const values = {};
tr.querySelectorAll('input').forEach((inp, i) => {
const c = cols[i];
if (!c.autoIncrement) values[c.name] = inp.value;
});
api('insert', { method: 'POST', q: '&table=' + encodeURIComponent(state.table),
body: { values } })
.then(d => {
if (d.error) return msg(d.error);
msg('已新增', true); adding = false; loadRows();
});
};
cancel.onclick = () => { adding = false; loadRows(); };
tdOp.appendChild(save); tdOp.appendChild(cancel); tr.appendChild(tdOp);
$('#grid table tbody').prepend(tr);
};
$('#delTableBtn').onclick = () => {
if (!confirm('确定删除整张表「' + state.table + '」?此操作不可恢复!')) return;
api('dropTable', { method: 'POST', body: { name: state.table } })
.then(d => {
if (d.error) return msg(d.error);
msg('表已删除', true);
state.table = null;
$('#title').textContent = '请选择左侧的表';
$('#toolbar').style.display = 'none';
$('#pager').style.display = 'none';
$('#grid').innerHTML = '';
loadTables();
});
};
$('#prevPage').onclick = () => { if (state.page > 1) { state.page--; loadRows(); } };
$('#nextPage').onclick = () => { state.page++; loadRows(); };
$('#orderBy').onchange = (e) => { state.orderBy = e.target.value; state.page = 1; loadRows(); };
$('#orderDir').onchange = (e) => { state.dir = e.target.value; state.page = 1; loadRows(); };
function colRow() {
const row = document.createElement('div'); row.className = 'crow';
row.innerHTML = `
<input class="cn" placeholder="列名">
<select class="ct">
<option>INT</option><option>VARCHAR(100)</option><option>TEXT</option>
<option>DECIMAL(10,2)</option><option>DATETIME</option><option>DATE</option>
<option>BOOLEAN</option><option>CHAR(1)</option><option>BIGINT</option>
</select>
<label class="muted"><input type="checkbox" class="cpri">主键</label>
<label class="muted"><input type="checkbox" class="cai">自增</label>
<label class="muted"><input type="checkbox" class="cnul" checked>可空</label>
<button class="ghost cdel">×</button>`;
row.querySelector('.cdel').onclick = () => row.remove();
return row;
}
$('#newTableBtn').onclick = () => {
$('#colDefs').innerHTML = '';
$('#colDefs').appendChild(colRow());
$('#tableModal').classList.add('show');
};
$('#addColBtn').onclick = () => $('#colDefs').appendChild(colRow());
$('#tableCancel').onclick = () => $('#tableModal').classList.remove('show');
$('#createTableOk').onclick = () => {
const name = $('#newTableName').value.trim();
const columns = [];
document.querySelectorAll('#colDefs .crow').forEach(r => {
columns.push({
name: r.querySelector('.cn').value.trim(),
type: r.querySelector('.ct').value,
primary: r.querySelector('.cpri').checked,
autoIncrement: r.querySelector('.cai').checked,
nullable: r.querySelector('.cnul').checked
});
});
api('createTable', { method: 'POST', body: { name, columns } })
.then(d => {
if (d.error) return msg(d.error);
msg('表已创建', true);
$('#tableModal').classList.remove('show');
loadTables();
});
};
// 启动
boot();
</script>
</body>
</html>