feat: 自动拼图失败三次后改为手动拖动
This commit is contained in:
@@ -1,14 +1,49 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { computed, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NInput, NTooltip } from "naive-ui";
|
||||
import { MoonOutline, SunnyOutline } from "@vicons/ionicons5";
|
||||
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 = "";
|
||||
@@ -24,12 +59,54 @@ async function submit() {
|
||||
errorText.value = typeof payload.detail === "string" ? payload.detail : "登录失败";
|
||||
return;
|
||||
}
|
||||
password.value = "";
|
||||
emit("success", String(payload.employee_id || employeeId.value.trim()));
|
||||
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>
|
||||
@@ -42,7 +119,7 @@ async function submit() {
|
||||
</template>
|
||||
{{ isDark ? "切换到日间" : "切换到夜间" }}
|
||||
</n-tooltip>
|
||||
<section class="gate-card">
|
||||
<section class="gate-card" :class="{ 'with-jigsaw': jigsaw }">
|
||||
<div class="brand">
|
||||
<div class="brand-mark" aria-hidden="true">E</div>
|
||||
<div>
|
||||
@@ -53,13 +130,43 @@ async function submit() {
|
||||
<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" />
|
||||
<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" />
|
||||
<n-input id="password" v-model:value="password" type="password" show-password-on="click" placeholder="密码" autocomplete="current-password" :disabled="!!jigsaw || submitting" />
|
||||
</div>
|
||||
<div class="gate-actions">
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user