feat: 实现已确认的登录、采集和任务导出
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { NAlert, NButton, NInput } from "naive-ui";
|
||||
import { api, saveSession } from "../../shared/http";
|
||||
import { setThemeChoice, themeChoice, type ThemeChoice } from "../../shared/theme";
|
||||
|
||||
const props = defineProps<{ purpose?: "login" | "pool" }>();
|
||||
const emit = defineEmits<{ done: [] }>();
|
||||
const router = useRouter();
|
||||
const mode = ref("local");
|
||||
const username = ref("");
|
||||
const password = ref("");
|
||||
const captcha = ref("");
|
||||
const captchaUrl = ref("");
|
||||
const sessionId = ref("");
|
||||
const smsCode = ref("");
|
||||
const smsRequired = ref(false);
|
||||
const errorText = ref("");
|
||||
const busy = ref(false);
|
||||
const themes = [
|
||||
{ label: "跟随系统", value: "system" },
|
||||
{ label: "深色", value: "dark" },
|
||||
{ label: "浅色", value: "light" },
|
||||
];
|
||||
|
||||
onMounted(async () => {
|
||||
if (props.purpose === "pool") {
|
||||
mode.value = "nqi";
|
||||
return;
|
||||
}
|
||||
const result = await api<{ mode: string }>("/api/auth/mode");
|
||||
mode.value = result.mode;
|
||||
});
|
||||
|
||||
function fail(error: unknown) {
|
||||
errorText.value = error instanceof Error ? error.message : "登录失败";
|
||||
}
|
||||
|
||||
async function enterLocal() {
|
||||
busy.value = true;
|
||||
errorText.value = "";
|
||||
try {
|
||||
const result = await api<{ token: string; user: { role: string; name: string } }>("/api/auth/local", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ username: username.value, password: password.value }),
|
||||
});
|
||||
saveSession(result.token, result.user);
|
||||
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
|
||||
} catch (error) {
|
||||
fail(error);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function startNqi() {
|
||||
busy.value = true;
|
||||
errorText.value = "";
|
||||
try {
|
||||
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string }>("/api/auth/nqi/start", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ username: username.value, purpose: props.purpose || "login" }),
|
||||
});
|
||||
if (result.status === "ok" && result.token && result.user) {
|
||||
saveSession(result.token, result.user);
|
||||
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
|
||||
return;
|
||||
}
|
||||
sessionId.value = result.sessionId || "";
|
||||
captchaUrl.value = result.captcha || "";
|
||||
} catch (error) {
|
||||
fail(error);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshCaptcha() {
|
||||
const result = await api<{ captcha: string }>("/api/auth/nqi/captcha", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ sessionId: sessionId.value }),
|
||||
});
|
||||
captchaUrl.value = result.captcha;
|
||||
}
|
||||
|
||||
async function sendSms() {
|
||||
busy.value = true;
|
||||
errorText.value = "";
|
||||
try {
|
||||
await api("/api/auth/nqi/sms", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ sessionId: sessionId.value, username: username.value, password: password.value, captcha: captcha.value }),
|
||||
});
|
||||
smsRequired.value = true;
|
||||
} catch (error) {
|
||||
fail(error);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function finishNqi() {
|
||||
busy.value = true;
|
||||
errorText.value = "";
|
||||
try {
|
||||
const result = await api<{ status: string; token?: string; user?: { role: string; name: string } }>("/api/auth/nqi/finish", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
sessionId: sessionId.value,
|
||||
username: username.value,
|
||||
password: password.value,
|
||||
captcha: captcha.value,
|
||||
smsCode: smsCode.value,
|
||||
}),
|
||||
});
|
||||
if (result.status === "sms_required") {
|
||||
smsRequired.value = true;
|
||||
return;
|
||||
}
|
||||
if (props.purpose === "pool") {
|
||||
emit("done");
|
||||
return;
|
||||
}
|
||||
if (result.token && result.user) {
|
||||
saveSession(result.token, result.user);
|
||||
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
|
||||
}
|
||||
} catch (error) {
|
||||
fail(error);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function chooseTheme(value: ThemeChoice) {
|
||||
setThemeChoice(value);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div :class="purpose === 'pool' ? '' : 'gate'">
|
||||
<div class="gate-card">
|
||||
<h1 style="margin: 0 0 16px; font-size: 20px; display: flex; justify-content: space-between; align-items: center;">
|
||||
<span>{{ purpose === "pool" ? "添加账号" : "DecomEval" }}</span>
|
||||
<select v-if="purpose !== 'pool'" :value="themeChoice" @change="chooseTheme(($event.target as HTMLSelectElement).value as ThemeChoice)">
|
||||
<option value="system">跟随系统</option>
|
||||
<option value="dark">深色</option>
|
||||
<option value="light">浅色</option>
|
||||
</select>
|
||||
</h1>
|
||||
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
|
||||
<div class="form-grid">
|
||||
<label>账号</label>
|
||||
<n-input v-model:value="username" />
|
||||
<label>密码</label>
|
||||
<n-input v-model:value="password" type="password" />
|
||||
<template v-if="mode === 'nqi' && captchaUrl">
|
||||
<label>验证码</label>
|
||||
<div>
|
||||
<n-input v-model:value="captcha" />
|
||||
<img class="captcha" :src="captchaUrl" alt="验证码" @click="refreshCaptcha" />
|
||||
</div>
|
||||
<template v-if="smsRequired">
|
||||
<label>短信验证码</label>
|
||||
<n-input v-model:value="smsCode" />
|
||||
</template>
|
||||
</template>
|
||||
</div>
|
||||
<div style="margin-top: 16px; display: flex; justify-content: flex-end; gap: 8px;">
|
||||
<n-button v-if="mode === 'local'" type="primary" :loading="busy" @click="enterLocal">登录</n-button>
|
||||
<template v-else>
|
||||
<n-button v-if="!captchaUrl" type="primary" :loading="busy" @click="startNqi">继续</n-button>
|
||||
<n-button v-if="captchaUrl" :loading="busy" @click="sendSms">发送短信验证码</n-button>
|
||||
<n-button v-if="captchaUrl" type="primary" :loading="busy" @click="finishNqi">登录</n-button>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user