fix: 验证码框单独显示加载和加载失败

This commit is contained in:
2026-10-30 11:40:43 +08:00
parent 06549316f9
commit d0601d06f9
2 changed files with 56 additions and 13 deletions
+40 -11
View File
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import { computed, onMounted, onUnmounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { NAlert, NButton, NInput, NModal } from "naive-ui";
import { NAlert, NButton, NInput, NModal, NSpin } from "naive-ui";
import { api, saveSession } from "../../shared/http";
import ThemeButton from "../../shell/ThemeButton.vue";
@@ -14,6 +14,16 @@ 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);
@@ -71,7 +81,8 @@ async function enterLocal() {
}
async function startNqi() {
busy.value = true;
captchaLoading.value = true;
captchaBroken.value = false;
errorText.value = "";
try {
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string }>("/api/auth/nqi/start", {
@@ -85,24 +96,39 @@ async function startNqi() {
}
sessionId.value = result.sessionId || "";
captchaUrl.value = result.captcha || "";
captchaBroken.value = !captchaUrl.value;
} catch (error) {
captchaUrl.value = "";
captchaBroken.value = true;
fail(error);
} finally {
busy.value = false;
captchaLoading.value = false;
}
}
async function refreshCaptcha() {
if (captchaLoading.value) return;
if (!sessionId.value) {
await startNqi();
return;
}
const result = await api<{ captcha: string }>("/api/auth/nqi/captcha", {
method: "POST",
body: JSON.stringify({ sessionId: sessionId.value }),
});
captchaUrl.value = result.captcha;
captcha.value = "";
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() {
@@ -209,7 +235,10 @@ async function finishNqi() {
<label>验证码</label>
<div class="lv-captcha">
<n-input v-model:value="captcha" placeholder="验证码" @keyup.enter="submitLogin" />
<img v-if="captchaUrl" :src="captchaUrl" alt="验证码" title="点击刷新" @click="refreshCaptcha" />
<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>
<n-button class="lv-go" type="primary" attr-type="submit" :loading="busy">{{ purpose === "pool" ? "添加" : "登录" }}</n-button>
+16 -2
View File
@@ -259,15 +259,29 @@ button, input, select { font: inherit; }
}
.lv-captcha { display: flex; gap: 8px; align-items: center; min-width: 0; }
.lv-captcha .n-input { flex: 1; min-width: 0; }
.lv-captcha img {
.lv-captcha-box {
width: 112px;
height: 36px;
flex: none;
display: grid;
place-items: center;
padding: 0;
border: 1px solid var(--line);
border-radius: 4px;
background: var(--bg);
cursor: pointer;
overflow: hidden;
}
.lv-captcha-box img {
width: 100%;
height: 100%;
object-fit: cover;
flex: none;
border: 0;
}
.lv-captcha-box img[src="/__captcha_broken__"] {
width: auto;
height: auto;
object-fit: none;
}
.lv-embed {
width: 100%;