feat: 完善登录保持、栅格下载和任务调度
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NInput, NSpin, NTooltip } from "naive-ui";
|
||||
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, login, refreshCaptcha, sendSms, type UserInfo } from "./api";
|
||||
import { loadCaptcha, loadSaved, login, refreshCaptcha, resumeSaved, sendSms, type UserInfo } from "./api";
|
||||
import { isDark, toggleTheme } from "../../shared/theme";
|
||||
|
||||
const emit = defineEmits<{ success: [user: UserInfo] }>();
|
||||
@@ -21,13 +21,30 @@ 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;
|
||||
@@ -50,8 +67,8 @@ async function connect() {
|
||||
}
|
||||
}
|
||||
|
||||
async function reloadCaptcha() {
|
||||
errorText.value = "";
|
||||
async function reloadCaptcha(keepError = false) {
|
||||
if (!keepError) errorText.value = "";
|
||||
try {
|
||||
if (!sessionId.value) {
|
||||
await connect();
|
||||
@@ -66,26 +83,59 @@ async function reloadCaptcha() {
|
||||
}
|
||||
}
|
||||
|
||||
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) return;
|
||||
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 || "短信已发送";
|
||||
countdown.value = 60;
|
||||
timer = window.setInterval(() => {
|
||||
countdown.value -= 1;
|
||||
if (countdown.value <= 0) window.clearInterval(timer);
|
||||
}, 1000);
|
||||
smsHint.value = result.message || "短信发送已受理";
|
||||
} catch (error) {
|
||||
stopCountdown();
|
||||
errorText.value = error instanceof ApiError ? error.message : "短信发送失败";
|
||||
} finally {
|
||||
sending.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
async function submit(smsCodeText = "") {
|
||||
if (smsRequired.value && !smsCodeText) return;
|
||||
errorText.value = "";
|
||||
submitting.value = true;
|
||||
try {
|
||||
@@ -94,22 +144,28 @@ async function submit() {
|
||||
username: username.value.trim(),
|
||||
password: password.value,
|
||||
captcha: captcha.value.trim(),
|
||||
smsCode: smsCode.value.trim(),
|
||||
smsCode: smsCodeText,
|
||||
});
|
||||
if (result.status === "sms_required") {
|
||||
smsRequired.value = true;
|
||||
smsHint.value = result.message || "请获取并填写短信验证码";
|
||||
smsCode.value = "";
|
||||
smsHint.value = "";
|
||||
return;
|
||||
}
|
||||
if (result.user) emit("success", result.user);
|
||||
if (result.user) {
|
||||
smsRequired.value = false;
|
||||
stopCountdown();
|
||||
emit("success", result.user);
|
||||
}
|
||||
} catch (error) {
|
||||
errorText.value = error instanceof ApiError ? error.message : "登录失败";
|
||||
smsRequired.value = false;
|
||||
await reloadCaptcha();
|
||||
if (!smsRequired.value) await reloadCaptcha(true);
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onUnmounted(stopCountdown);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -133,8 +189,19 @@ async function submit() {
|
||||
<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">
|
||||
<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" />
|
||||
@@ -158,21 +225,22 @@ async function submit() {
|
||||
<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>
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user