import { defineEventHandler, readBody } from 'h3' import { saveCaptcha } from '../../utils/guard' const SLIDER_SIZE = 42 const MIN_W = 240 const MAX_W = 480 /** * 生成滑块拼图验证码 challenge。 * 前端量取自身宽度后传入,后端在该宽度内随机生成缺口 X 坐标作为答案。 * 返回 token + puzzleX(缺口位置,前端用于渲染) + width + size。 */ export default defineEventHandler(async (event) => { const body = (await readBody(event).catch(() => ({}))) as Record const width = Math.min(Math.max(Number(body?.width) || 320, MIN_W), MAX_W) const token = globalThis.crypto.randomUUID() const answer = Math.floor(Math.random() * (width - SLIDER_SIZE * 2)) + SLIDER_SIZE await saveCaptcha(token, answer) return { token, puzzleX: answer, width, size: SLIDER_SIZE } })