feat: 完成登录与栅格下载桌面端
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NInput, NSpin, NTooltip } from "naive-ui";
|
||||
import { HelpCircleOutline, MoonOutline, SunnyOutline } from "@vicons/ionicons5";
|
||||
import { ApiError } from "../../shared/http";
|
||||
import { loadCaptcha, login, refreshCaptcha, sendSms, type UserInfo } from "./api";
|
||||
import { isDark, toggleTheme } from "../../shared/theme";
|
||||
|
||||
const emit = defineEmits<{ success: [user: UserInfo] }>();
|
||||
|
||||
const ready = ref(false);
|
||||
const offline = ref("");
|
||||
const sessionId = ref("");
|
||||
const captchaUrl = ref("");
|
||||
const username = ref("");
|
||||
const password = ref("");
|
||||
const captcha = ref("");
|
||||
const smsCode = ref("");
|
||||
const smsRequired = ref(false);
|
||||
const sending = ref(false);
|
||||
const submitting = ref(false);
|
||||
const errorText = ref("");
|
||||
const smsHint = ref("");
|
||||
const countdown = ref(0);
|
||||
let timer = 0;
|
||||
|
||||
onMounted(() => {
|
||||
void connect();
|
||||
});
|
||||
|
||||
async function connect() {
|
||||
offline.value = "";
|
||||
ready.value = false;
|
||||
for (let attempt = 0; attempt < 20; attempt += 1) {
|
||||
try {
|
||||
const result = await loadCaptcha();
|
||||
sessionId.value = result.sessionId;
|
||||
captchaUrl.value = result.captcha;
|
||||
ready.value = true;
|
||||
return;
|
||||
} catch (error) {
|
||||
const failedLocally = !(error instanceof ApiError) || error.status === 0;
|
||||
offline.value = error instanceof ApiError ? error.message : "连接失败";
|
||||
if (!failedLocally) {
|
||||
ready.value = true;
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => window.setTimeout(resolve, 500));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function reloadCaptcha() {
|
||||
errorText.value = "";
|
||||
try {
|
||||
if (!sessionId.value) {
|
||||
await connect();
|
||||
return;
|
||||
}
|
||||
const result = await refreshCaptcha(sessionId.value);
|
||||
captchaUrl.value = result.captcha;
|
||||
captcha.value = "";
|
||||
} catch (error) {
|
||||
errorText.value = error instanceof ApiError ? error.message : "验证码刷新失败";
|
||||
await connect();
|
||||
}
|
||||
}
|
||||
|
||||
async function requestSms() {
|
||||
if (!sessionId.value || countdown.value > 0) return;
|
||||
sending.value = true;
|
||||
smsHint.value = "";
|
||||
try {
|
||||
const result = await sendSms(sessionId.value);
|
||||
smsHint.value = result.message || "短信已发送";
|
||||
countdown.value = 60;
|
||||
timer = window.setInterval(() => {
|
||||
countdown.value -= 1;
|
||||
if (countdown.value <= 0) window.clearInterval(timer);
|
||||
}, 1000);
|
||||
} catch (error) {
|
||||
errorText.value = error instanceof ApiError ? error.message : "短信发送失败";
|
||||
} finally {
|
||||
sending.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
errorText.value = "";
|
||||
submitting.value = true;
|
||||
try {
|
||||
const result = await login({
|
||||
sessionId: sessionId.value,
|
||||
username: username.value.trim(),
|
||||
password: password.value,
|
||||
captcha: captcha.value.trim(),
|
||||
smsCode: smsCode.value.trim(),
|
||||
});
|
||||
if (result.status === "sms_required") {
|
||||
smsRequired.value = true;
|
||||
smsHint.value = result.message || "请获取并填写短信验证码";
|
||||
return;
|
||||
}
|
||||
if (result.user) emit("success", result.user);
|
||||
} catch (error) {
|
||||
errorText.value = error instanceof ApiError ? error.message : "登录失败";
|
||||
smsRequired.value = false;
|
||||
await reloadCaptcha();
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="gate">
|
||||
<n-button class="theme-switch" quaternary circle :aria-label="isDark ? '切换到日间' : '切换到夜间'" @click="toggleTheme">
|
||||
<template #icon><n-icon><MoonOutline v-if="!isDark" /><SunnyOutline v-else /></n-icon></template>
|
||||
</n-button>
|
||||
<section class="gate-card">
|
||||
<div class="brand">
|
||||
<div class="brand-mark" aria-hidden="true">
|
||||
<svg width="24" height="24" viewBox="0 0 24 24" fill="none">
|
||||
<path d="M4 16.5 12 4l8 12.5" stroke="currentColor" stroke-width="1.8" />
|
||||
<path d="M7.2 16.5h9.6M9.2 13.2h5.6" stroke="currentColor" stroke-width="1.8" />
|
||||
</svg>
|
||||
</div>
|
||||
<div>
|
||||
<h1>NqiTools</h1>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<n-spin :show="!ready && !offline">
|
||||
<n-alert v-if="!ready" type="warning" style="margin-bottom: 12px">连接失败,正在重试。</n-alert>
|
||||
<n-alert v-else-if="offline" type="warning" style="margin-bottom: 12px">{{ offline }}</n-alert>
|
||||
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
|
||||
<form @submit.prevent="submit">
|
||||
<div class="form-row">
|
||||
<label for="username">账号</label>
|
||||
<n-input id="username" v-model:value="username" placeholder="账号" autocomplete="username" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="password">密码</label>
|
||||
<n-input id="password" v-model:value="password" type="password" show-password-on="click" placeholder="密码" autocomplete="current-password" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="captcha">
|
||||
验证码
|
||||
<n-tooltip>
|
||||
<template #trigger>
|
||||
<n-icon class="hint" :size="15"><HelpCircleOutline /></n-icon>
|
||||
</template>
|
||||
点击图片刷新。
|
||||
</n-tooltip>
|
||||
</label>
|
||||
<div class="captcha-line">
|
||||
<n-input id="captcha" v-model:value="captcha" placeholder="验证码" />
|
||||
<img v-if="captchaUrl" :src="captchaUrl" alt="验证码" title="点击刷新" @click="reloadCaptcha" />
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="smsRequired" class="form-row">
|
||||
<label for="sms">短信</label>
|
||||
<div class="captcha-line">
|
||||
<n-input id="sms" v-model:value="smsCode" placeholder="短信验证码" />
|
||||
<n-button :disabled="sending || countdown > 0" @click="requestSms">
|
||||
{{ countdown > 0 ? `${countdown}s` : "获取短信" }}
|
||||
</n-button>
|
||||
</div>
|
||||
</div>
|
||||
<p v-if="smsHint" class="file-name" style="margin: 0 0 8px 96px">{{ smsHint }}</p>
|
||||
<div class="gate-actions">
|
||||
<n-button type="primary" attr-type="submit" :loading="submitting" :disabled="!ready">登录</n-button>
|
||||
</div>
|
||||
</form>
|
||||
</n-spin>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,43 @@
|
||||
import { api } from "../../shared/http";
|
||||
|
||||
export interface UserInfo {
|
||||
loginId: string;
|
||||
userName: string;
|
||||
cityName: string;
|
||||
orgName: string;
|
||||
}
|
||||
|
||||
export function loadCaptcha() {
|
||||
return api<{ sessionId: string; captcha: string }>("/api/auth/captcha", { method: "POST" });
|
||||
}
|
||||
|
||||
export function refreshCaptcha(sessionId: string) {
|
||||
return api<{ sessionId: string; captcha: string }>("/api/auth/captcha/refresh", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ sessionId }),
|
||||
});
|
||||
}
|
||||
|
||||
export function sendSms(sessionId: string) {
|
||||
return api<{ message: string }>("/api/auth/sms", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ sessionId }),
|
||||
});
|
||||
}
|
||||
|
||||
export function login(body: {
|
||||
sessionId: string;
|
||||
username: string;
|
||||
password: string;
|
||||
captcha: string;
|
||||
smsCode?: string;
|
||||
}) {
|
||||
return api<{ status: string; message?: string; user?: UserInfo }>("/api/auth/login", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
return api<{ ok: boolean }>("/api/auth/logout", { method: "POST" });
|
||||
}
|
||||
Reference in New Issue
Block a user