<template>
<div class="main">
<div class="head">
<el-input v-model="ipt" placeholder="Please input" />
<canvas id="canvas" width="100" height="40" @click="SelectNumber"></canvas>
</div>
<div class="foot">
<el-button type="primary">Primary</el-button>
</div>
</div>
<div>
</div>
</template>
<script setup>
import { nextTick, ref } from 'vue';
import { ElMessage } from 'element-plus'
let ipt = ref()
let num = ref()
// 生成二维码code
const generateCode = () => {
nextTick(() => {
const canvas = document.querySelector('#canvas')
const context = canvas.getContext('2d')
context.font = 'oblique 20px Arial'
context.fillStyle = '#fff'
context.rotate(Math.PI * 1.4 / 180)
context.fillText(num.value, 10, 25)
})
}
// 获取四位随机数
const generateNum = () => {
num.value = Math.floor(Math.random() * 4000 + 1000)
}
// 清空画布
const clearCanvas = () => {
nextTick(() => {
const canvas = document.querySelector('#canvas')
canvas.width = 100
})
}
// 触发画布切换验证码
const SelectNumber = () => {
clearCanvas()
generateNum()
generateCode()
}
const submit = () => {
if (num.value !== Number(ipt.value)) ElMessage.error('验证码输入错误')
else ElMessage.success('验证码输入正确')
}
generateNum()
generateCode()
</script>