264 lines
9.3 KiB
HTML
264 lines
9.3 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>登录 - 南日AI</title>
|
||
<style>
|
||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||
body {
|
||
font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", sans-serif;
|
||
background: #d8e4ec;
|
||
min-height: 100vh;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 40px;
|
||
}
|
||
.header {
|
||
position: absolute;
|
||
top: 0;
|
||
left: 0;
|
||
right: 0;
|
||
height: 56px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
padding: 0 24px;
|
||
background: rgba(255,255,255,0.5);
|
||
}
|
||
.header-title { font-size: 18px; font-weight: 600; color: #333; }
|
||
.login-box {
|
||
background: #fff;
|
||
border: 1px solid #b8d4e3;
|
||
border-radius: 12px;
|
||
box-shadow: 0 4px 12px rgba(0,0,0,0.06);
|
||
padding: 40px;
|
||
width: 100%;
|
||
max-width: 380px;
|
||
}
|
||
.login-title {
|
||
font-size: 24px;
|
||
font-weight: 600;
|
||
color: #333;
|
||
text-align: center;
|
||
margin-bottom: 30px;
|
||
}
|
||
.form-group {
|
||
margin-bottom: 20px;
|
||
}
|
||
.form-group label {
|
||
display: block;
|
||
font-size: 14px;
|
||
color: #555;
|
||
margin-bottom: 8px;
|
||
}
|
||
.form-group input {
|
||
width: 100%;
|
||
padding: 12px 14px;
|
||
font-size: 14px;
|
||
border: 1px solid #b8d4e3;
|
||
border-radius: 8px;
|
||
outline: none;
|
||
transition: border-color 0.2s;
|
||
background: #fff;
|
||
}
|
||
.form-group input:focus {
|
||
border-color: #3498db;
|
||
}
|
||
.error-msg {
|
||
color: #e74c3c;
|
||
font-size: 13px;
|
||
margin-bottom: 12px;
|
||
text-align: center;
|
||
}
|
||
.btn-login {
|
||
width: 100%;
|
||
padding: 14px;
|
||
font-size: 16px;
|
||
font-weight: 500;
|
||
color: #fff;
|
||
background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
|
||
border: none;
|
||
border-radius: 8px;
|
||
cursor: pointer;
|
||
transition: all 0.2s;
|
||
}
|
||
.btn-login:hover {
|
||
opacity: 0.9;
|
||
}
|
||
.btn-login:disabled {
|
||
opacity: 0.6;
|
||
cursor: not-allowed;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header class="header">
|
||
<span class="header-title">南日AI</span>
|
||
</header>
|
||
|
||
<div class="login-box">
|
||
<h1 class="login-title">登录</h1>
|
||
{% if error %}
|
||
<p class="error-msg">{{ error }}</p>
|
||
{% endif %}
|
||
<form id="loginForm" method="POST" action="/login">
|
||
<div class="form-group">
|
||
<label>用户名</label>
|
||
<input type="text" name="username" placeholder="请输入用户名" required autofocus>
|
||
</div>
|
||
<div class="form-group">
|
||
<label>密码</label>
|
||
<input type="password" name="password" placeholder="请输入密码" required>
|
||
</div>
|
||
<button type="submit" class="btn-login" id="btnLogin">登录</button>
|
||
</form>
|
||
</div>
|
||
<script>
|
||
var JAVA_API_BASE = "{{ java_api_base or '' }}";
|
||
var AUTH_TOKEN_KEY = 'aiimage_auth_token';
|
||
var DEVICE_ID_KEY = 'aiimage_device_id';
|
||
|
||
function clearAppPermissionCaches() {
|
||
try {
|
||
var keysToRemove = [];
|
||
for (var i = 0; i < localStorage.length; i++) {
|
||
var key = localStorage.key(i);
|
||
if (key && key.indexOf('app_column_permissions:') === 0) {
|
||
keysToRemove.push(key);
|
||
}
|
||
}
|
||
keysToRemove.forEach(function(key) {
|
||
localStorage.removeItem(key);
|
||
});
|
||
} catch (e) {}
|
||
}
|
||
|
||
function waitForPywebview(timeoutMs) {
|
||
return new Promise(function(resolve) {
|
||
if (window.pywebview && window.pywebview.api && window.pywebview.api.get_device_id) {
|
||
resolve(window.pywebview.api);
|
||
return;
|
||
}
|
||
var done = false;
|
||
var finish = function() {
|
||
if (done) return;
|
||
done = true;
|
||
var api = (window.pywebview && window.pywebview.api) || null;
|
||
resolve(api);
|
||
};
|
||
window.addEventListener('pywebviewready', finish, { once: true });
|
||
setTimeout(finish, timeoutMs || 1500);
|
||
});
|
||
}
|
||
|
||
function fetchDeviceId() {
|
||
return waitForPywebview(2000).then(function(api) {
|
||
if (!api || typeof api.get_device_id !== 'function') {
|
||
return '';
|
||
}
|
||
try {
|
||
var ret = api.get_device_id();
|
||
return Promise.resolve(ret).then(function(res) {
|
||
if (res && res.success && res.device_id) {
|
||
try { localStorage.setItem(DEVICE_ID_KEY, res.device_id); } catch (e) {}
|
||
return res.device_id;
|
||
}
|
||
return '';
|
||
}).catch(function() { return ''; });
|
||
} catch (e) {
|
||
return '';
|
||
}
|
||
});
|
||
}
|
||
|
||
function buildJavaUrl(path) {
|
||
var base = (JAVA_API_BASE || '').replace(/\/+$/, '');
|
||
return base + path;
|
||
}
|
||
|
||
(function() {
|
||
var params = new URLSearchParams(window.location.search || '');
|
||
if (params.get('logout') === '1' || params.get('switch') === '1') {
|
||
try { localStorage.removeItem(AUTH_TOKEN_KEY); } catch (e) {}
|
||
}
|
||
})();
|
||
|
||
document.getElementById('loginForm').onsubmit = function(e) {
|
||
e.preventDefault();
|
||
var btn = document.getElementById('btnLogin');
|
||
btn.disabled = true;
|
||
btn.textContent = '登录中...';
|
||
var form = e.target;
|
||
var username = (form.username.value || '').trim();
|
||
var password = form.password.value || '';
|
||
|
||
function showError(msg) {
|
||
var errEl = document.querySelector('.error-msg');
|
||
if (!errEl) {
|
||
errEl = document.createElement('p');
|
||
errEl.className = 'error-msg';
|
||
form.insertBefore(errEl, form.firstChild);
|
||
}
|
||
errEl.textContent = msg || '登录失败';
|
||
btn.disabled = false;
|
||
btn.textContent = '登录';
|
||
}
|
||
|
||
fetchDeviceId().then(function(deviceId) {
|
||
if (!deviceId) {
|
||
showError('未获取到设备ID,请在桌面端打开');
|
||
return;
|
||
}
|
||
var payload = { username: username, password: password, deviceId: deviceId };
|
||
return fetch(buildJavaUrl('/login'), {
|
||
method: 'POST',
|
||
credentials: 'include',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
'X-Device-Id': deviceId,
|
||
'X-Requested-With': 'XMLHttpRequest'
|
||
},
|
||
body: JSON.stringify(payload)
|
||
})
|
||
.then(function(r) { return r.json(); })
|
||
.then(function(res) {
|
||
if (!res || !res.success) {
|
||
showError((res && res.message) || '登录失败');
|
||
return;
|
||
}
|
||
var data = res.data || {};
|
||
if (data.token) {
|
||
try { localStorage.setItem(AUTH_TOKEN_KEY, data.token); } catch (e) {}
|
||
}
|
||
// 把 Java 签发的 JWT 同步到 Python 套壳同源 cookie,供后续页面跳转携带
|
||
return fetch('/api/auth/sync', {
|
||
method: 'POST',
|
||
credentials: 'same-origin',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
'X-Requested-With': 'XMLHttpRequest'
|
||
},
|
||
body: JSON.stringify({ token: data.token })
|
||
})
|
||
.then(function(r) { return r.json().catch(function() { return {}; }); })
|
||
.then(function() {
|
||
clearAppPermissionCaches();
|
||
window.location.href = '/home';
|
||
})
|
||
.catch(function() {
|
||
clearAppPermissionCaches();
|
||
window.location.href = '/home';
|
||
});
|
||
})
|
||
.catch(function() {
|
||
showError('网络异常,请稍后重试');
|
||
});
|
||
});
|
||
};
|
||
</script>
|
||
</body>
|
||
</html>
|