diff --git a/public/css/style.css b/public/css/style.css index 0624cc5..a84710d 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -12,7 +12,7 @@ body { .page { display: none; } .page.active { display: flex; flex-direction: column; } -#loginPage { +#loginPage, #registerPage { min-height: 100vh; justify-content: center; align-items: center; @@ -68,6 +68,12 @@ body { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: #fff; } +.btn-secondary { + background: transparent; + color: #667eea; + border: 1px solid #667eea; + margin-top: 12px; +} .login-error { color: #e74c3c; text-align: center; @@ -76,6 +82,17 @@ body { min-height: 20px; } +.login-register-link { + text-align: center; + margin-top: 16px; +} + +.login-register-link a { + color: #667eea; + text-decoration: none; + font-size: 14px; +} + /* 首页 */ #homePage { min-height: 100vh; diff --git a/public/index.html b/public/index.html index 3718801..167d74a 100644 --- a/public/index.html +++ b/public/index.html @@ -21,10 +21,33 @@ +
+ +
+
+

注册账号

+
+ + +
+
+ + +
+
+ + +
+ + + +
+
+
@@ -457,7 +480,7 @@
- + diff --git a/public/js/auth.js b/public/js/auth.js index 8c412f9..b6069e3 100644 --- a/public/js/auth.js +++ b/public/js/auth.js @@ -20,6 +20,40 @@ async function doLogin() { } } +async function doRegister() { + const user = document.getElementById('registerUser').value.trim(); + const pass = document.getElementById('registerPass').value.trim(); + const passConfirm = document.getElementById('registerPassConfirm').value.trim(); + if (!user || !pass) { document.getElementById('registerError').textContent = '请输入用户名和密码'; return; } + if (pass !== passConfirm) { document.getElementById('registerError').textContent = '两次密码输入不一致'; return; } + try { + const res = await fetch(API + '/api/register', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username: user, password: pass }) + }); + const data = await res.json(); + if (!res.ok) { document.getElementById('registerError').textContent = data.error; return; } + showToast('注册成功,请登录'); + showLoginPage(); + } catch (e) { + document.getElementById('registerError').textContent = '网络错误'; + } +} + +function showRegisterPage() { + document.getElementById('registerError').textContent = ''; + document.getElementById('registerUser').value = ''; + document.getElementById('registerPass').value = ''; + document.getElementById('registerPassConfirm').value = ''; + showPage('registerPage'); +} + +function showLoginPage() { + document.getElementById('loginError').textContent = ''; + showPage('loginPage'); +} + function doLogout() { token = null; username = null; diff --git a/public/js/edit-modal.js b/public/js/edit-modal.js index efce8f6..958dba5 100644 --- a/public/js/edit-modal.js +++ b/public/js/edit-modal.js @@ -135,9 +135,8 @@ async function saveEditRecord() { if (time) body.created_at = new Date(time).toISOString(); try { - const res = await fetch(API + '/api/records/' + id, { + const res = await apiFetch(API + '/api/records/' + id, { method: 'PUT', - headers: headers(), body: JSON.stringify(body) }); if (!res.ok) { showToast('保存失败'); return; } @@ -152,7 +151,7 @@ async function deleteEditRecord() { const id = document.getElementById('editId').value; if (!confirm('确定删除这条记录?')) return; try { - const res = await fetch(API + '/api/records/' + id, { method: 'DELETE', headers: headers() }); + const res = await apiFetch(API + '/api/records/' + id, { method: 'DELETE' }); if (!res.ok) { showToast('删除失败'); return; } showToast('已删除'); closeEditModal(); diff --git a/public/js/import-export.js b/public/js/import-export.js index 0f80370..0795254 100644 --- a/public/js/import-export.js +++ b/public/js/import-export.js @@ -11,7 +11,7 @@ async function doExport(format) { closeExportModal(); showToast('正在导出...'); try { - const res = await fetch(API + '/api/export?format=' + format, { headers: headers() }); + const res = await apiFetch(API + '/api/export?format=' + format); if (!res.ok) { const err = await res.json(); showToast(err.error || '导出失败'); @@ -88,9 +88,9 @@ async function confirmImport() { if (!pendingImportContent) return; showToast('正在导入...'); try { - const res = await fetch(API + '/api/import', { + const res = await apiFetch(API + '/api/import', { method: 'POST', - headers: { 'Content-Type': 'text/plain; charset=utf-8', 'Authorization': 'Bearer ' + token }, + headers: { 'Content-Type': 'text/plain; charset=utf-8' }, body: pendingImportContent }); const data = await res.json(); diff --git a/public/js/record-form.js b/public/js/record-form.js index c77f450..08dcf1c 100644 --- a/public/js/record-form.js +++ b/public/js/record-form.js @@ -159,9 +159,8 @@ async function submitRecord() { } try { - const res = await fetch(API + '/api/records', { + const res = await apiFetch(API + '/api/records', { method: 'POST', - headers: headers(), body: JSON.stringify(body) }); if (!res.ok) { showToast('保存失败'); return; }