ERP/frontend/js/components/login.js

25 lines
1.0 KiB
JavaScript

Router.register('/login', async () => {
setTimeout(() => {
const btn = document.getElementById('login-btn');
btn?.addEventListener('click', async () => {
const u = document.getElementById('login-username').value;
const p = document.getElementById('login-password').value;
try {
await API.login(u, p);
await API.me().then(user => {
const info = document.getElementById('user-info');
if (info) info.textContent = user?.username || '未登录';
}).catch(()=>{});
location.href = '#/dashboard';
} catch(e) {}
});
}, 0);
return `<div class="grid cols-2">
<div class="card">
<div style="font-weight:600;margin-bottom:8px">管理员登录</div>
<div class="field"><label>用户名</label><input id="login-username" class="input" /></div>
<div class="field"><label>密码</label><input id="login-password" type="password" class="input" /></div>
<div class="actions"><button id="login-btn" class="btn">登录</button></div>
</div>
</div>`;
});