176 lines
5.9 KiB
Vue
176 lines
5.9 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref } from "vue";
|
|
import { NAlert, NButton, NIcon, NInput, NTooltip } from "naive-ui";
|
|
import { ChevronForward, MoonOutline, SunnyOutline } from "@vicons/ionicons5";
|
|
import { isDark, toggleTheme } from "../../shared/theme";
|
|
|
|
interface JigsawPuzzle {
|
|
big_img: string;
|
|
small_img: string;
|
|
y: number;
|
|
width: number;
|
|
height: number;
|
|
piece_width: number;
|
|
piece_height: number;
|
|
}
|
|
|
|
const emit = defineEmits<{ success: [employeeId: string] }>();
|
|
const employeeId = ref("");
|
|
const password = ref("");
|
|
const submitting = ref(false);
|
|
const errorText = ref("");
|
|
const attemptId = ref("");
|
|
const jigsaw = ref<JigsawPuzzle | null>(null);
|
|
const offset = ref(0);
|
|
const dragging = ref(false);
|
|
let startX = 0;
|
|
let startOffset = 0;
|
|
|
|
const maxOffset = computed(() => {
|
|
const puzzle = jigsaw.value;
|
|
if (!puzzle) return 0;
|
|
return Math.max(puzzle.width - puzzle.piece_width, 0);
|
|
});
|
|
|
|
function applyResult(payload: { employee_id?: string; attempt_id?: string; jigsaw?: JigsawPuzzle }) {
|
|
if (payload.jigsaw && payload.attempt_id) {
|
|
attemptId.value = payload.attempt_id;
|
|
jigsaw.value = payload.jigsaw;
|
|
offset.value = 0;
|
|
return;
|
|
}
|
|
password.value = "";
|
|
attemptId.value = "";
|
|
jigsaw.value = null;
|
|
emit("success", String(payload.employee_id || employeeId.value.trim()));
|
|
}
|
|
|
|
async function submit() {
|
|
errorText.value = "";
|
|
submitting.value = true;
|
|
try {
|
|
const response = await fetch("/api/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ employee_id: employeeId.value.trim(), password: password.value }),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
errorText.value = typeof payload.detail === "string" ? payload.detail : "登录失败";
|
|
return;
|
|
}
|
|
applyResult(payload);
|
|
} finally {
|
|
submitting.value = false;
|
|
}
|
|
}
|
|
|
|
async function sendOffset() {
|
|
if (!attemptId.value || submitting.value) return;
|
|
submitting.value = true;
|
|
errorText.value = "";
|
|
try {
|
|
const response = await fetch("/api/login/jigsaw", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ attempt_id: attemptId.value, offset: offset.value }),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
jigsaw.value = null;
|
|
attemptId.value = "";
|
|
errorText.value = typeof payload.detail === "string" ? payload.detail : "登录失败";
|
|
return;
|
|
}
|
|
applyResult(payload);
|
|
} finally {
|
|
submitting.value = false;
|
|
}
|
|
}
|
|
|
|
function onPointerDown(event: PointerEvent) {
|
|
if (!jigsaw.value || submitting.value) return;
|
|
dragging.value = true;
|
|
startX = event.clientX;
|
|
startOffset = offset.value;
|
|
(event.currentTarget as HTMLElement).setPointerCapture(event.pointerId);
|
|
}
|
|
|
|
function onPointerMove(event: PointerEvent) {
|
|
if (!dragging.value) return;
|
|
const next = startOffset + (event.clientX - startX);
|
|
offset.value = Math.min(Math.max(Math.round(next), 0), maxOffset.value);
|
|
}
|
|
|
|
function onPointerUp() {
|
|
if (!dragging.value) return;
|
|
dragging.value = false;
|
|
if (offset.value > 0) void sendOffset();
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="gate">
|
|
<n-tooltip>
|
|
<template #trigger>
|
|
<n-button class="theme-switch" quaternary circle :aria-label="isDark ? '切换到日间' : '切换到夜间'" @click="toggleTheme">
|
|
<template #icon><n-icon><MoonOutline v-if="!isDark" /><SunnyOutline v-else /></n-icon></template>
|
|
</n-button>
|
|
</template>
|
|
{{ isDark ? "切换到日间" : "切换到夜间" }}
|
|
</n-tooltip>
|
|
<section class="gate-card" :class="{ 'with-jigsaw': jigsaw }">
|
|
<div class="brand">
|
|
<div class="brand-mark" aria-hidden="true">E</div>
|
|
<div>
|
|
<h1>EunivTools</h1>
|
|
</div>
|
|
</div>
|
|
<n-alert v-if="errorText" type="error" style="margin-bottom: 12px">{{ errorText }}</n-alert>
|
|
<form @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" />
|
|
</div>
|
|
<div class="form-row">
|
|
<label for="password">密码</label>
|
|
<n-input id="password" v-model:value="password" type="password" show-password-on="click" placeholder="密码" autocomplete="current-password" :disabled="!!jigsaw || submitting" />
|
|
</div>
|
|
<div v-if="jigsaw" class="jigsaw-block">
|
|
<p>拖动滑块完成拼图</p>
|
|
<div class="jigsaw" :style="{ width: jigsaw.width + 'px', height: jigsaw.height + 'px' }">
|
|
<img class="board" :src="jigsaw.big_img" alt="" :width="jigsaw.width" :height="jigsaw.height" draggable="false" />
|
|
<img
|
|
class="piece"
|
|
:src="jigsaw.small_img"
|
|
alt=""
|
|
:width="jigsaw.piece_width"
|
|
:height="jigsaw.piece_height"
|
|
:style="{ top: jigsaw.y + 'px', left: offset + 'px' }"
|
|
draggable="false"
|
|
/>
|
|
</div>
|
|
<div class="jigsaw-track" :style="{ width: jigsaw.width + 'px' }">
|
|
<button
|
|
type="button"
|
|
class="jigsaw-handle"
|
|
:style="{ left: offset + 'px' }"
|
|
aria-label="拼图滑块"
|
|
:disabled="submitting"
|
|
@pointerdown="onPointerDown"
|
|
@pointermove="onPointerMove"
|
|
@pointerup="onPointerUp"
|
|
@pointercancel="onPointerUp"
|
|
>
|
|
<n-icon><ChevronForward /></n-icon>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div v-else class="gate-actions">
|
|
<n-button type="primary" attr-type="submit" :loading="submitting" :disabled="!employeeId.trim() || !password">登录</n-button>
|
|
</div>
|
|
</form>
|
|
</section>
|
|
</div>
|
|
</template>
|