357 lines
12 KiB
Vue
357 lines
12 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
|
import { useRoute, useRouter } from "vue-router";
|
|
import { NButton, NInput, NModal, NSpin } from "naive-ui";
|
|
import { api, saveSession } from "../../shared/http";
|
|
import { useToast } from "../../shared/toast";
|
|
import ThemeButton from "../../shell/ThemeButton.vue";
|
|
import "./login-gate.css";
|
|
|
|
const props = defineProps<{ purpose?: "login" | "pool" }>();
|
|
const emit = defineEmits<{ done: [] }>();
|
|
const route = useRoute();
|
|
const router = useRouter();
|
|
const toast = useToast();
|
|
const mode = ref("local");
|
|
const username = ref("");
|
|
const password = ref("");
|
|
const captcha = ref("");
|
|
const captchaUrl = ref("");
|
|
const captchaLoading = ref(false);
|
|
const captchaBroken = ref(false);
|
|
const captchaSrc = computed(() => captchaBroken.value || !captchaUrl.value ? "/__captcha_broken__" : captchaUrl.value);
|
|
|
|
function markCaptchaBroken(event: Event) {
|
|
const img = event.target as HTMLImageElement;
|
|
if (captchaBroken.value) return;
|
|
captchaBroken.value = true;
|
|
img.src = "/__captcha_broken__";
|
|
}
|
|
const sessionId = ref("");
|
|
const smsCode = ref("");
|
|
const smsRequired = ref(false);
|
|
const smsSending = ref(false);
|
|
const countdown = ref(0);
|
|
const busy = ref(false);
|
|
const noticeVisible = ref(false);
|
|
const noticePinned = ref(false);
|
|
let countdownTimer = 0;
|
|
let noticeTimer = 0;
|
|
|
|
function clearNoticeTimer() {
|
|
window.clearTimeout(noticeTimer);
|
|
noticeTimer = 0;
|
|
}
|
|
|
|
function showNotice() {
|
|
if (mode.value !== "nqi") return;
|
|
noticePinned.value = false;
|
|
noticeVisible.value = true;
|
|
clearNoticeTimer();
|
|
}
|
|
|
|
function beginNoticeCountdown() {
|
|
if (mode.value !== "nqi" || noticePinned.value) return;
|
|
clearNoticeTimer();
|
|
noticeTimer = window.setTimeout(() => {
|
|
noticeTimer = 0;
|
|
if (!noticePinned.value) noticeVisible.value = false;
|
|
}, 5000);
|
|
}
|
|
|
|
function pinNotice() {
|
|
if (mode.value !== "nqi") return;
|
|
clearNoticeTimer();
|
|
noticePinned.value = true;
|
|
noticeVisible.value = true;
|
|
}
|
|
|
|
watch(mode, (value) => {
|
|
if (value === "nqi") showNotice();
|
|
else {
|
|
noticePinned.value = false;
|
|
noticeVisible.value = false;
|
|
clearNoticeTimer();
|
|
}
|
|
});
|
|
|
|
onMounted(async () => {
|
|
if (props.purpose !== "pool" && route.query.notice === "already") {
|
|
toast.info("这个数据库已经安装过,请使用原来的管理员账号登录。");
|
|
}
|
|
if (props.purpose === "pool") {
|
|
mode.value = "nqi";
|
|
showNotice();
|
|
await startNqi();
|
|
beginNoticeCountdown();
|
|
return;
|
|
}
|
|
const result = await api<{ mode: string }>("/api/auth/mode");
|
|
mode.value = result.mode;
|
|
if (mode.value === "nqi") {
|
|
showNotice();
|
|
await startNqi();
|
|
beginNoticeCountdown();
|
|
}
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
window.clearInterval(countdownTimer);
|
|
clearNoticeTimer();
|
|
});
|
|
|
|
function submitLogin() {
|
|
if (busy.value) return;
|
|
if (props.purpose === "pool") {
|
|
if (captchaLoading.value) return;
|
|
if (!sessionId.value) {
|
|
startNqi();
|
|
return;
|
|
}
|
|
finishNqi();
|
|
return;
|
|
}
|
|
beginLogin();
|
|
}
|
|
|
|
async function beginLogin() {
|
|
busy.value = true;
|
|
try {
|
|
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string; message?: string }>("/api/auth/login", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
username: username.value,
|
|
password: password.value,
|
|
sessionId: sessionId.value,
|
|
captcha: captcha.value,
|
|
smsCode: smsCode.value,
|
|
}),
|
|
});
|
|
if (result.status === "need_captcha") {
|
|
smsRequired.value = false;
|
|
stopCountdown();
|
|
smsCode.value = "";
|
|
captcha.value = "";
|
|
captchaUrl.value = result.captcha || "";
|
|
captchaBroken.value = !captchaUrl.value;
|
|
toast.error(result.message || "请重新输入验证码");
|
|
pinNotice();
|
|
return;
|
|
}
|
|
if (result.status === "sms_required") {
|
|
smsRequired.value = true;
|
|
smsCode.value = "";
|
|
return;
|
|
}
|
|
if (result.status === "ok" && result.token && result.user) {
|
|
saveSession(result.token, result.user);
|
|
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
|
|
}
|
|
} catch (error) {
|
|
fail(error);
|
|
pinNotice();
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
|
|
function fail(error: unknown) {
|
|
toast.error(error instanceof Error ? error.message : "登录失败");
|
|
}
|
|
|
|
async function startNqi() {
|
|
captchaLoading.value = true;
|
|
captchaBroken.value = false;
|
|
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 || "";
|
|
captchaBroken.value = !captchaUrl.value;
|
|
} catch (error) {
|
|
captchaUrl.value = "";
|
|
captchaBroken.value = true;
|
|
fail(error);
|
|
} finally {
|
|
captchaLoading.value = false;
|
|
}
|
|
}
|
|
|
|
async function refreshCaptcha() {
|
|
if (captchaLoading.value) return;
|
|
if (!sessionId.value) {
|
|
await startNqi();
|
|
return;
|
|
}
|
|
captchaLoading.value = true;
|
|
captchaBroken.value = false;
|
|
try {
|
|
const result = await api<{ captcha: string }>("/api/auth/nqi/captcha", {
|
|
method: "POST",
|
|
body: JSON.stringify({ sessionId: sessionId.value }),
|
|
});
|
|
captchaUrl.value = result.captcha;
|
|
captcha.value = "";
|
|
captchaBroken.value = !result.captcha;
|
|
} catch (error) {
|
|
captchaUrl.value = "";
|
|
captchaBroken.value = true;
|
|
fail(error);
|
|
} finally {
|
|
captchaLoading.value = false;
|
|
}
|
|
}
|
|
|
|
function stopCountdown() {
|
|
window.clearInterval(countdownTimer);
|
|
countdown.value = 0;
|
|
}
|
|
|
|
function startCountdown() {
|
|
stopCountdown();
|
|
countdown.value = 60;
|
|
countdownTimer = window.setInterval(() => {
|
|
countdown.value -= 1;
|
|
if (countdown.value <= 0) stopCountdown();
|
|
}, 1000);
|
|
}
|
|
|
|
async function backFromSms() {
|
|
smsRequired.value = false;
|
|
stopCountdown();
|
|
smsCode.value = "";
|
|
await refreshCaptcha();
|
|
}
|
|
|
|
async function sendSms() {
|
|
if (!sessionId.value || countdown.value > 0 || smsSending.value) return;
|
|
smsSending.value = true;
|
|
startCountdown();
|
|
try {
|
|
const result = await api<{ message?: string }>("/api/auth/nqi/sms", {
|
|
method: "POST",
|
|
body: JSON.stringify({ sessionId: sessionId.value, username: username.value, password: password.value, captcha: captcha.value }),
|
|
});
|
|
toast.info(result.message || "短信发送已受理");
|
|
} catch (error) {
|
|
stopCountdown();
|
|
fail(error);
|
|
pinNotice();
|
|
} finally {
|
|
smsSending.value = false;
|
|
}
|
|
}
|
|
|
|
async function finishNqi() {
|
|
busy.value = true;
|
|
try {
|
|
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; message?: string; captcha?: 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 === "need_captcha") {
|
|
smsRequired.value = false;
|
|
stopCountdown();
|
|
smsCode.value = "";
|
|
captcha.value = "";
|
|
captchaUrl.value = result.captcha || "";
|
|
captchaBroken.value = !captchaUrl.value;
|
|
toast.error(result.message || "请重新输入验证码");
|
|
pinNotice();
|
|
return;
|
|
}
|
|
if (result.status === "sms_required") {
|
|
smsRequired.value = true;
|
|
smsCode.value = "";
|
|
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);
|
|
pinNotice();
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section v-if="purpose === 'pool'" class="lv-embed">
|
|
<p v-if="noticeVisible" class="lv-notice">请使用大数据平台账号登录</p>
|
|
<form class="lv-fields" @submit.prevent="submitLogin">
|
|
<label for="pool-account">账号</label>
|
|
<n-input :input-props="{ id: 'pool-account', autocomplete: 'username' }" v-model:value="username" @keyup.enter="submitLogin" />
|
|
<label for="pool-password">密码</label>
|
|
<n-input :input-props="{ id: 'pool-password', autocomplete: 'current-password' }" v-model:value="password" type="password" show-password-on="click" @keyup.enter="submitLogin" />
|
|
<label for="pool-captcha">验证码</label>
|
|
<div class="lv-captcha">
|
|
<n-input :input-props="{ id: 'pool-captcha', autocomplete: 'off' }" v-model:value="captcha" @keyup.enter="submitLogin" />
|
|
<button type="button" class="lv-captcha-box" title="点击刷新" @click="refreshCaptcha">
|
|
<n-spin v-if="captchaLoading" size="small" />
|
|
<img v-else :src="captchaSrc" alt="验证码" @error="markCaptchaBroken" />
|
|
</button>
|
|
</div>
|
|
<n-button class="lv-go" type="primary" attr-type="submit" :loading="busy">添加</n-button>
|
|
</form>
|
|
</section>
|
|
<div v-else class="gate lv-tower">
|
|
<ThemeButton placement="left" />
|
|
<div class="lv-mast" aria-hidden="true"><span /></div>
|
|
<section class="lv-side">
|
|
<div class="lv-brand">
|
|
<h1>DecomEval</h1>
|
|
</div>
|
|
<form class="lv-fields" @submit.prevent="submitLogin">
|
|
<label for="login-account">账号</label>
|
|
<input id="login-account" v-model="username" placeholder="account" autocomplete="username" @keyup.enter="submitLogin" />
|
|
<label for="login-password">密码</label>
|
|
<input id="login-password" v-model="password" type="password" placeholder="password" autocomplete="current-password" @keyup.enter="submitLogin" />
|
|
<template v-if="mode === 'nqi'">
|
|
<label for="login-captcha">验证码</label>
|
|
<div class="lv-captcha">
|
|
<input id="login-captcha" v-model="captcha" placeholder="captcha" autocomplete="off" @keyup.enter="submitLogin" />
|
|
<button type="button" class="lv-captcha-box" title="点击刷新" @click="refreshCaptcha">
|
|
<n-spin v-if="captchaLoading" size="small" />
|
|
<img v-else :src="captchaSrc" alt="验证码" @error="markCaptchaBroken" />
|
|
</button>
|
|
</div>
|
|
</template>
|
|
<button class="lv-go" type="submit" :disabled="busy">{{ busy ? "登录中" : "登录" }}</button>
|
|
</form>
|
|
<p v-if="mode === 'nqi'" class="lv-notice" :class="{ on: noticeVisible }" :aria-hidden="!noticeVisible">请使用大数据平台账号登录</p>
|
|
</section>
|
|
<n-modal v-model:show="smsRequired" :mask-closable="false" :auto-focus="false">
|
|
<div class="lv-sms-sheet" role="dialog" aria-modal="true" aria-labelledby="sms-title">
|
|
<h2 id="sms-title">短信认证</h2>
|
|
<label for="login-sms">验证码</label>
|
|
<input id="login-sms" v-model="smsCode" placeholder="sms code" autocomplete="one-time-code" @keyup.enter="finishNqi" />
|
|
<div class="lv-sms-actions">
|
|
<button type="button" @click="backFromSms">返回</button>
|
|
<button type="button" :disabled="smsSending || countdown > 0" @click="sendSms">{{ countdown > 0 ? `重新发送(${countdown})` : "发送验证码" }}</button>
|
|
</div>
|
|
<button class="lv-sms-go" type="button" :disabled="busy || !smsCode.trim()" @click="finishNqi">{{ busy ? "提交中" : "提交" }}</button>
|
|
</div>
|
|
</n-modal>
|
|
</div>
|
|
</template>
|