247 lines
8.3 KiB
Vue
247 lines
8.3 KiB
Vue
<script setup lang="ts">
|
||
import { onMounted, onUnmounted, ref } from "vue";
|
||
import { NAlert, NButton, NIcon, NInput, NModal, NSelect, NSpin, NTooltip } from "naive-ui";
|
||
import { HelpCircleOutline, MoonOutline, SunnyOutline } from "@vicons/ionicons5";
|
||
import { ApiError } from "../../shared/http";
|
||
import { loadCaptcha, loadSaved, login, refreshCaptcha, resumeSaved, 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 saved = ref<UserInfo[]>([]);
|
||
const savedId = ref<string | null>(null);
|
||
const resuming = ref(false);
|
||
const countdown = ref(0);
|
||
let timer = 0;
|
||
|
||
onMounted(() => {
|
||
void connect();
|
||
void refreshSaved();
|
||
});
|
||
|
||
async function refreshSaved() {
|
||
try {
|
||
const result = await loadSaved();
|
||
saved.value = result.accounts;
|
||
if (!result.accounts.some((item) => item.loginId === savedId.value)) {
|
||
savedId.value = result.accounts[0]?.loginId ?? null;
|
||
}
|
||
} catch {
|
||
saved.value = [];
|
||
savedId.value = null;
|
||
}
|
||
}
|
||
|
||
async function connect() {
|
||
offline.value = "";
|
||
ready.value = false;
|
||
for (let attempt = 0; attempt < 40; 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(keepError = false) {
|
||
if (!keepError) 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();
|
||
}
|
||
}
|
||
|
||
function stopCountdown() {
|
||
window.clearInterval(timer);
|
||
countdown.value = 0;
|
||
}
|
||
|
||
function startCountdown() {
|
||
stopCountdown();
|
||
countdown.value = 60;
|
||
timer = window.setInterval(() => {
|
||
countdown.value -= 1;
|
||
if (countdown.value <= 0) stopCountdown();
|
||
}, 1000);
|
||
}
|
||
|
||
function savedLabel(item: UserInfo) {
|
||
const name = item.userName || item.loginId;
|
||
return item.cityName ? `${name} · ${item.cityName}` : name;
|
||
}
|
||
|
||
async function useSaved() {
|
||
if (!savedId.value) return;
|
||
resuming.value = true;
|
||
errorText.value = "";
|
||
try {
|
||
const result = await resumeSaved(savedId.value);
|
||
if (result.user) emit("success", result.user);
|
||
} catch (error) {
|
||
errorText.value = error instanceof ApiError ? error.message : "登录失败";
|
||
await refreshSaved();
|
||
} finally {
|
||
resuming.value = false;
|
||
}
|
||
}
|
||
|
||
async function requestSms() {
|
||
if (!sessionId.value || countdown.value > 0 || sending.value) return;
|
||
sending.value = true;
|
||
errorText.value = "";
|
||
smsHint.value = "";
|
||
startCountdown();
|
||
try {
|
||
const result = await sendSms(sessionId.value);
|
||
smsHint.value = result.message || "短信发送已受理";
|
||
} catch (error) {
|
||
stopCountdown();
|
||
errorText.value = error instanceof ApiError ? error.message : "短信发送失败";
|
||
} finally {
|
||
sending.value = false;
|
||
}
|
||
}
|
||
|
||
async function submit(smsCodeText = "") {
|
||
if (smsRequired.value && !smsCodeText) return;
|
||
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: smsCodeText,
|
||
});
|
||
if (result.status === "sms_required") {
|
||
smsRequired.value = true;
|
||
smsCode.value = "";
|
||
smsHint.value = "";
|
||
return;
|
||
}
|
||
if (result.user) {
|
||
smsRequired.value = false;
|
||
stopCountdown();
|
||
emit("success", result.user);
|
||
}
|
||
} catch (error) {
|
||
errorText.value = error instanceof ApiError ? error.message : "登录失败";
|
||
if (!smsRequired.value) await reloadCaptcha(true);
|
||
} finally {
|
||
submitting.value = false;
|
||
}
|
||
}
|
||
|
||
onUnmounted(stopCountdown);
|
||
</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 && !smsRequired" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
|
||
<form @submit.prevent="submit()">
|
||
<div v-if="saved.length" class="form-row">
|
||
<label for="saved">已登录</label>
|
||
<div class="captcha-line">
|
||
<n-select
|
||
id="saved"
|
||
v-model:value="savedId"
|
||
:options="saved.map((item) => ({ label: savedLabel(item), value: item.loginId }))"
|
||
/>
|
||
<n-button type="primary" secondary :loading="resuming" :disabled="!savedId" @click="useSaved">进入</n-button>
|
||
</div>
|
||
</div>
|
||
<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 class="gate-actions">
|
||
<n-button type="primary" attr-type="submit" :loading="submitting && !smsRequired" :disabled="!ready">登录</n-button>
|
||
</div>
|
||
</form>
|
||
</n-spin>
|
||
</section>
|
||
<n-modal v-model:show="smsRequired" :mask-closable="false" :closable="false" preset="card" title="短信认证" style="width: 420px">
|
||
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
|
||
<n-input v-model:value="smsCode" placeholder="输入短信验证码" @keyup.enter="submit(smsCode.trim())" />
|
||
<p v-if="smsHint" class="file-name" style="margin: 8px 0 0">{{ smsHint }}</p>
|
||
<div class="sms-actions">
|
||
<n-button :disabled="sending || countdown > 0" @click="requestSms">
|
||
{{ countdown > 0 ? `重新发送(${countdown})` : "发送验证码" }}
|
||
</n-button>
|
||
<n-button type="primary" :loading="submitting" :disabled="!smsCode.trim()" @click="submit(smsCode.trim())">提交</n-button>
|
||
</div>
|
||
</n-modal>
|
||
</div>
|
||
</template>
|