feat: 对接 UAC 登录并支持 iCenter 扫码

This commit is contained in:
2026-11-15 05:48:04 +08:00
parent 6340309e66
commit 158578d17c
7 changed files with 651 additions and 88 deletions
+115 -3
View File
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { computed, onMounted, onUnmounted, ref } from "vue";
import { NAlert, NButton, NCheckbox, NIcon, NInput, NTooltip } from "naive-ui";
import { ChevronForward, MoonOutline, SunnyOutline } from "@vicons/ionicons5";
import { isDark, toggleTheme } from "../../shared/theme";
@@ -21,10 +21,13 @@ interface LoginPayload {
employee_id?: string;
attempt_id?: string;
jigsaw?: JigsawPuzzle;
qr_image?: string;
status?: string;
detail?: unknown;
}
const emit = defineEmits<{ success: [session: AppSession] }>();
const mode = ref<"password" | "qr">("password");
const employeeId = ref("");
const password = ref("");
const submitting = ref(false);
@@ -34,10 +37,14 @@ const manualJigsaw = ref(false);
const errorText = ref("");
const attemptId = ref("");
const jigsaw = ref<JigsawPuzzle | null>(null);
const qrImage = ref("");
const qrExpired = ref(false);
const offset = ref(0);
const dragging = ref(false);
let startX = 0;
let startOffset = 0;
let pollTimer = 0;
let polling = false;
const maxOffset = computed(() => {
const puzzle = jigsaw.value;
@@ -63,6 +70,15 @@ onMounted(async () => {
}
});
onUnmounted(() => stopPoll());
function stopPoll() {
if (pollTimer) {
window.clearInterval(pollTimer);
pollTimer = 0;
}
}
function applyResult(payload: LoginPayload) {
if (payload.jigsaw && payload.attempt_id) {
attemptId.value = payload.attempt_id;
@@ -78,9 +94,93 @@ function applyResult(payload: LoginPayload) {
password.value = "";
attemptId.value = "";
jigsaw.value = null;
qrImage.value = "";
stopPoll();
emit("success", next);
}
async function setMode(next: "password" | "qr") {
if (mode.value === next || submitting.value) return;
mode.value = next;
errorText.value = "";
jigsaw.value = null;
attemptId.value = "";
if (next === "qr") {
await startQr();
return;
}
stopPoll();
qrImage.value = "";
qrExpired.value = false;
}
async function startQr() {
stopPoll();
errorText.value = "";
qrExpired.value = false;
submitting.value = true;
try {
const response = await fetch("/api/login/qr", { method: "POST" });
const payload = await response.json().catch(() => ({})) as LoginPayload;
if (!response.ok) {
errorText.value = typeof payload.detail === "string" ? payload.detail : "二维码没有获取到";
qrImage.value = "";
qrExpired.value = true;
return;
}
attemptId.value = payload.attempt_id || "";
qrImage.value = payload.qr_image || "";
if (attemptId.value) {
pollTimer = window.setInterval(() => {
void pollQr();
}, 1000);
}
} catch {
errorText.value = "二维码没有获取到";
qrImage.value = "";
qrExpired.value = true;
} finally {
submitting.value = false;
}
}
async function pollQr() {
if (!attemptId.value || submitting.value || polling) return;
polling = true;
try {
const response = await fetch("/api/login/qr/poll", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ attempt_id: attemptId.value }),
});
const payload = await response.json().catch(() => ({})) as LoginPayload;
if (!response.ok) {
stopPoll();
qrExpired.value = true;
errorText.value = typeof payload.detail === "string" ? payload.detail : "请重新扫码";
return;
}
if (payload.status === "waiting") return;
if (payload.status === "expired") {
stopPoll();
qrExpired.value = true;
errorText.value = "";
return;
}
applyResult(payload);
} catch {
stopPoll();
qrExpired.value = true;
} finally {
polling = false;
}
}
async function refreshQr() {
if (submitting.value) return;
await startQr();
}
async function submit() {
errorText.value = "";
submitting.value = true;
@@ -159,15 +259,27 @@ function onPointerUp() {
</template>
{{ isDark ? "切换到日间" : "切换到夜间" }}
</n-tooltip>
<section class="gate-card" :class="{ 'with-jigsaw': jigsaw }">
<section class="gate-card" :class="{ 'with-jigsaw': jigsaw, 'with-qr': mode === 'qr' }">
<div class="brand">
<div class="brand-mark" aria-hidden="true">E</div>
<div>
<h1>EunivTools</h1>
</div>
</div>
<div class="gate-tabs" role="tablist">
<button type="button" role="tab" :class="{ active: mode === 'password' }" :disabled="!!jigsaw || submitting" @click="setMode('password')">密码登录</button>
<button type="button" role="tab" :class="{ active: mode === 'qr' }" :disabled="!!jigsaw || submitting" @click="setMode('qr')">扫码登录</button>
</div>
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
<form @submit.prevent="submit">
<div v-if="mode === 'qr'" class="qr-block">
<p>请使用 iCenter 扫码登录</p>
<button class="qr-frame" type="button" :disabled="submitting" aria-label="刷新二维码" @click="refreshQr">
<img v-if="qrImage" :src="qrImage" alt="" width="210" height="210" />
<span v-if="qrExpired" class="qr-mask">点击刷新</span>
<span v-else-if="!qrImage" class="qr-mask">正在获取</span>
</button>
</div>
<form v-else @submit.prevent="submit">
<div class="form-row">
<label for="employee-id">工号</label>
<n-input id="employee-id" v-model:value="employeeId" placeholder="工号" autocomplete="username" :disabled="!!jigsaw || submitting" />
+68
View File
@@ -134,6 +134,74 @@ select {
color: var(--ink);
}
.gate-tabs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 16px;
}
.gate-tabs button {
height: 34px;
border: 1px solid var(--line);
background: var(--elevated);
color: var(--muted);
border-radius: 4px;
cursor: pointer;
}
.gate-tabs button.active {
border-color: var(--accent);
color: var(--accent);
background: var(--accent-soft);
}
.gate-tabs button:disabled {
cursor: wait;
opacity: 0.7;
}
.gate-card.with-qr {
width: min(440px, 100%);
}
.qr-block {
text-align: center;
}
.qr-block p {
margin: 0 0 12px;
color: var(--muted);
}
.qr-frame {
position: relative;
display: inline-grid;
place-items: center;
width: 210px;
height: 210px;
padding: 0;
border: 1px solid var(--line);
background: #fff;
cursor: pointer;
}
.qr-frame img {
display: block;
width: 210px;
height: 210px;
}
.qr-mask {
position: absolute;
inset: 0;
display: grid;
place-items: center;
background: rgba(13, 17, 23, 0.72);
color: #fff;
font-size: 14px;
}
.gate-card.with-jigsaw {
width: max-content;
max-width: calc(100% - 32px);