324 lines
8.4 KiB
Vue
324 lines
8.4 KiB
Vue
<script setup lang="ts">
|
||
import { NIcon } from 'naive-ui';
|
||
import { User as UserIcon, CloudUpload, Logout } from '@vicons/tabler';
|
||
import { useUserAuth } from '@/composable/user-auth';
|
||
import { useCloudSync } from '@/composable/cloud-sync';
|
||
|
||
const { isLoggedIn, username, isAdmin, login, register, logout } = useUserAuth();
|
||
const { syncNow } = useCloudSync();
|
||
|
||
// ---- 本地图片验证码(服务端出图,不依赖外网) ----
|
||
const captchaId = ref('');
|
||
const captchaImg = ref('');
|
||
const captchaCode = ref('');
|
||
|
||
async function loadCaptcha() {
|
||
captchaCode.value = '';
|
||
try {
|
||
const res = await fetch('/relay/user/captcha');
|
||
const body = await res.json().catch(() => ({} as any));
|
||
if (body?.code === 200 && body.data?.id) {
|
||
captchaId.value = body.data.id;
|
||
captchaImg.value = `data:image/png;base64,${body.data.img}`;
|
||
}
|
||
}
|
||
catch { /* 静默:提交时服务端会再校验 */ }
|
||
}
|
||
|
||
const showLogin = ref(false);
|
||
|
||
watch(showLogin, (v) => {
|
||
if (v) {
|
||
loadCaptcha().catch(() => {});
|
||
}
|
||
else {
|
||
captchaId.value = '';
|
||
captchaImg.value = '';
|
||
captchaCode.value = '';
|
||
}
|
||
});
|
||
const mode = ref<'login' | 'register'>('login');
|
||
const formUser = ref('');
|
||
const formPass = ref('');
|
||
const formPass2 = ref('');
|
||
const error = ref('');
|
||
const busy = ref(false);
|
||
const notice = ref('');
|
||
let noticeTimer: ReturnType<typeof setTimeout> | undefined;
|
||
|
||
function setNotice(msg: string) {
|
||
notice.value = msg;
|
||
if (noticeTimer) {
|
||
clearTimeout(noticeTimer);
|
||
}
|
||
noticeTimer = setTimeout(() => (notice.value = ''), 4000);
|
||
}
|
||
|
||
async function submit() {
|
||
error.value = '';
|
||
if (mode.value === 'register' && formPass.value !== formPass2.value) {
|
||
error.value = '两次输入的密码不一致';
|
||
return;
|
||
}
|
||
busy.value = true;
|
||
try {
|
||
if (mode.value === 'login') {
|
||
await login(formUser.value.trim(), formPass.value, captchaId.value, captchaCode.value.trim());
|
||
}
|
||
else {
|
||
await register(formUser.value.trim(), formPass.value, captchaId.value, captchaCode.value.trim());
|
||
}
|
||
showLogin.value = false;
|
||
formPass.value = '';
|
||
formPass2.value = '';
|
||
setNotice('登录成功,正在同步数据…');
|
||
const r = await syncNow();
|
||
setNotice(r.ok ? '✓ 数据已合并同步到云端' : r.message);
|
||
}
|
||
catch (e: any) {
|
||
error.value = String(e?.message || e);
|
||
// 验证码单次有效,失败后必须换一张
|
||
loadCaptcha().catch(() => {});
|
||
}
|
||
finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
async function doSync() {
|
||
setNotice('同步中…');
|
||
const r = await syncNow();
|
||
setNotice(r.ok ? '✓ 同步完成,云端与本机数据已合并' : r.message);
|
||
}
|
||
|
||
function doLogout() {
|
||
logout();
|
||
setNotice('已退出登录(云端数据保留)');
|
||
}
|
||
|
||
function switchMode() {
|
||
mode.value = mode.value === 'login' ? 'register' : 'login';
|
||
error.value = '';
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="user-root">
|
||
<div v-if="notice" class="user-notice">
|
||
{{ notice }}
|
||
</div>
|
||
|
||
<!-- not logged in -->
|
||
<c-button v-if="!isLoggedIn" size="small" round :bordered="false" @click="showLogin = true">
|
||
<NIcon :component="UserIcon" mr-4px />
|
||
登录
|
||
</c-button>
|
||
|
||
<!-- logged in -->
|
||
<div v-else class="user-chip" :title="`当前用户:${username}`">
|
||
<NIcon :component="UserIcon" />
|
||
<RouterLink to="/user-center" class="user-name user-center-link" title="用户中心">{{ username }}</RouterLink>
|
||
<RouterLink v-if="isAdmin" to="/admin" class="user-action" title="管理后台">🛠</RouterLink>
|
||
<a href="javascript:;" class="user-action" title="同步数据到云端(收藏/最近使用/生成记录/网站收藏/上传文件)" @click="doSync">
|
||
<NIcon :component="CloudUpload" />
|
||
</a>
|
||
<a href="javascript:;" class="user-action" title="退出登录" @click="doLogout">
|
||
<NIcon :component="Logout" />
|
||
</a>
|
||
</div>
|
||
|
||
<!-- login modal -->
|
||
<div v-if="showLogin" class="user-mask" @click.self="showLogin = false">
|
||
<div class="user-modal">
|
||
<div class="user-modal-title">
|
||
{{ mode === 'login' ? '登录' : '注册' }}
|
||
</div>
|
||
<div class="user-modal-sub">
|
||
登录后收藏、最近使用、生成记录、网站收藏等数据自动同步云端,换电脑登录即恢复;工具上传的文件也会保存到云端。
|
||
</div>
|
||
|
||
<input v-model="formUser" class="user-input" placeholder="用户名(3-32位字母/数字/下划线/中文)" autocomplete="username" @keyup.enter="submit">
|
||
<input v-model="formPass" class="user-input" type="password" placeholder="密码(至少 6 位)" :autocomplete="mode === 'login' ? 'current-password' : 'new-password'" @keyup.enter="submit">
|
||
<input v-if="mode === 'register'" v-model="formPass2" class="user-input" type="password" placeholder="确认密码" autocomplete="new-password" @keyup.enter="submit">
|
||
|
||
<div class="captcha-row">
|
||
<input v-model="captchaCode" class="user-input captcha-input" placeholder="验证码(4位)" autocomplete="off" @keyup.enter="submit">
|
||
<img v-if="captchaImg" class="captcha-img" :src="captchaImg" alt="验证码" title="点击换一张" @click="loadCaptcha">
|
||
<a v-else href="javascript:;" class="captcha-reload" @click="loadCaptcha">获取验证码</a>
|
||
</div>
|
||
|
||
<div v-if="error" class="user-error">
|
||
{{ error }}
|
||
</div>
|
||
|
||
<div class="user-modal-actions">
|
||
<a href="javascript:;" class="user-switch" @click="switchMode">
|
||
{{ mode === 'login' ? '没有账号?注册' : '已有账号?登录' }}
|
||
</a>
|
||
<div>
|
||
<c-button size="small" @click="showLogin = false">
|
||
取消
|
||
</c-button>
|
||
<c-button size="small" type="primary" ml-8px :disabled="busy || !formUser || !formPass || !captchaCode.trim() || !captchaId" @click="submit">
|
||
{{ busy ? '请稍候…' : (mode === 'login' ? '登录' : '注册并登录') }}
|
||
</c-button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style lang="less" scoped>
|
||
.user-root {
|
||
position: relative;
|
||
}
|
||
|
||
.user-notice {
|
||
position: fixed;
|
||
top: 60px;
|
||
right: 16px;
|
||
z-index: 9999;
|
||
background: #18a058;
|
||
color: #fff;
|
||
font-size: 13px;
|
||
padding: 8px 16px;
|
||
border-radius: 8px;
|
||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
|
||
}
|
||
|
||
.user-chip {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
border: 1px solid rgba(128, 128, 128, 0.35);
|
||
border-radius: 999px;
|
||
padding: 3px 12px;
|
||
font-size: 13px;
|
||
|
||
.user-name {
|
||
max-width: 110px;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.user-center-link {
|
||
color: inherit;
|
||
text-decoration: none;
|
||
|
||
&:hover {
|
||
color: #18a058;
|
||
}
|
||
}
|
||
|
||
.user-action {
|
||
display: inline-flex;
|
||
opacity: 0.6;
|
||
transition: opacity 0.15s;
|
||
|
||
&:hover {
|
||
opacity: 1;
|
||
color: #18a058;
|
||
}
|
||
}
|
||
}
|
||
|
||
.captcha-row {
|
||
display: flex;
|
||
gap: 8px;
|
||
align-items: center;
|
||
margin: 2px 0 8px;
|
||
|
||
.captcha-input {
|
||
flex: 1;
|
||
margin: 0;
|
||
}
|
||
|
||
.captcha-img {
|
||
height: 40px;
|
||
border-radius: 4px;
|
||
cursor: pointer;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.captcha-reload {
|
||
flex-shrink: 0;
|
||
font-size: 13px;
|
||
color: #18a058;
|
||
white-space: nowrap;
|
||
}
|
||
}
|
||
|
||
.user-mask {
|
||
position: fixed;
|
||
inset: 0;
|
||
background: rgba(0, 0, 0, 0.35);
|
||
z-index: 9998;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
|
||
.user-modal {
|
||
width: 360px;
|
||
max-width: 92vw;
|
||
background: #fff;
|
||
border-radius: 14px;
|
||
padding: 22px 24px;
|
||
box-shadow: 0 12px 48px rgba(0, 0, 0, 0.2);
|
||
|
||
.user-modal-title {
|
||
font-size: 18px;
|
||
font-weight: 800;
|
||
margin-bottom: 6px;
|
||
}
|
||
|
||
.user-modal-sub {
|
||
font-size: 12px;
|
||
opacity: 0.6;
|
||
line-height: 1.7;
|
||
margin-bottom: 14px;
|
||
}
|
||
|
||
.user-input {
|
||
width: 100%;
|
||
box-sizing: border-box;
|
||
border: 1px solid rgba(128, 128, 128, 0.35);
|
||
border-radius: 8px;
|
||
padding: 9px 12px;
|
||
font-size: 14px;
|
||
margin-bottom: 10px;
|
||
outline: none;
|
||
transition: border-color 0.15s;
|
||
|
||
&:focus {
|
||
border-color: #18a058;
|
||
}
|
||
}
|
||
|
||
.user-error {
|
||
color: #d03050;
|
||
font-size: 12.5px;
|
||
margin-bottom: 8px;
|
||
}
|
||
|
||
.user-modal-actions {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
margin-top: 4px;
|
||
|
||
.user-switch {
|
||
font-size: 12.5px;
|
||
color: #18a058;
|
||
text-decoration: none;
|
||
|
||
&:hover {
|
||
text-decoration: underline;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</style>
|