69 lines
1.7 KiB
Vue
69 lines
1.7 KiB
Vue
<template>
|
|
<div class="demo">
|
|
<div class="row">
|
|
<captcha :request="requestCaptcha" @update="onUpdate" @error="onError" />
|
|
|
|
<captcha
|
|
:width="120"
|
|
:height="48"
|
|
title="点我换一张"
|
|
placeholder="加载中"
|
|
:request="requestCaptcha"
|
|
@update="onUpdate"
|
|
/>
|
|
</div>
|
|
|
|
<p class="status" v-text="status" />
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { ref } from "vue";
|
|
import { Captcha } from "timi-web";
|
|
import type { CaptchaRequest, CaptchaResult } from "timi-web";
|
|
|
|
const status = ref("等待加载验证码");
|
|
|
|
/**
|
|
* 演示用验证码:直接生成一张 SVG 图片,无需后端接口。
|
|
* 真实项目里在这里调用后端换取 captchaId 与图片。
|
|
*/
|
|
async function requestCaptcha(width: number, height: number): Promise<CaptchaResult> {
|
|
const text = String(Math.floor(1000 + Math.random() * 9000));
|
|
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">` +
|
|
`<rect width="100%" height="100%" fill="#f3f3f3"/>` +
|
|
`<text x="50%" y="50%" font-family="monospace" font-size="24" fill="#006eff" ` +
|
|
`text-anchor="middle" dominant-baseline="central">${text}</text></svg>`;
|
|
return {
|
|
id: `demo-${Date.now()}`,
|
|
data: `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`
|
|
};
|
|
}
|
|
|
|
function onUpdate(result: CaptchaResult) {
|
|
status.value = `已刷新,captchaId = ${result.id}`;
|
|
}
|
|
|
|
function onError(error: unknown) {
|
|
status.value = `获取失败:${String(error)}`;
|
|
}
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
.demo {
|
|
.row {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 1.5rem;
|
|
align-items: center;
|
|
margin-bottom: 1rem;
|
|
}
|
|
|
|
.status {
|
|
margin: 0;
|
|
font-size: .75rem;
|
|
color: var(--vp-c-text-2);
|
|
}
|
|
}
|
|
</style>
|