|
| 1 | +<template> |
| 2 | + <div class="VerifyCode"> |
| 3 | + <canvas |
| 4 | + id="VerifyCode-canvas" |
| 5 | + :width="props.contentWidth" |
| 6 | + :height="props.contentHeight" |
| 7 | + @click="refreshCode" |
| 8 | + ></canvas> |
| 9 | + </div> |
| 10 | +</template> |
| 11 | + |
| 12 | +<script setup lang="ts"> |
| 13 | +import { onMounted, watch, computed } from 'vue' |
| 14 | +const props = defineProps({ |
| 15 | + code: { |
| 16 | + type: String, |
| 17 | + default: '1234', |
| 18 | + }, |
| 19 | + fontSizeMin: { |
| 20 | + type: Number, |
| 21 | + default: 25, |
| 22 | + }, |
| 23 | + fontSizeMax: { |
| 24 | + type: Number, |
| 25 | + default: 35, |
| 26 | + }, |
| 27 | + backgroundColorMin: { |
| 28 | + type: Number, |
| 29 | + default: 255, |
| 30 | + }, |
| 31 | + backgroundColorMax: { |
| 32 | + type: Number, |
| 33 | + default: 255, |
| 34 | + }, |
| 35 | + colorMin: { |
| 36 | + type: Number, |
| 37 | + default: 0, |
| 38 | + }, |
| 39 | + colorMax: { |
| 40 | + type: Number, |
| 41 | + default: 160, |
| 42 | + }, |
| 43 | + lineColorMin: { |
| 44 | + type: Number, |
| 45 | + default: 40, |
| 46 | + }, |
| 47 | + lineColorMax: { |
| 48 | + type: Number, |
| 49 | + default: 180, |
| 50 | + }, |
| 51 | + dotColorMin: { |
| 52 | + type: Number, |
| 53 | + default: 0, |
| 54 | + }, |
| 55 | + dotColorMax: { |
| 56 | + type: Number, |
| 57 | + default: 255, |
| 58 | + }, |
| 59 | + contentWidth: { |
| 60 | + type: Number, |
| 61 | + default: 112, |
| 62 | + }, |
| 63 | + contentHeight: { |
| 64 | + type: Number, |
| 65 | + default: 40, |
| 66 | + }, |
| 67 | +}) |
| 68 | +
|
| 69 | +//验证码 |
| 70 | +const emit = defineEmits(['update:code']) |
| 71 | +const verifyCode = computed({ |
| 72 | + get: () => { |
| 73 | + return props.code |
| 74 | + }, |
| 75 | + set: (data) => { |
| 76 | + emit('update:code', data) |
| 77 | + }, |
| 78 | +}) |
| 79 | +
|
| 80 | +// 生成校验码 |
| 81 | +const makeCode = (len = 4) => { |
| 82 | + let code = '' |
| 83 | + const codeLength = len |
| 84 | + const identifyCodes = '123456789abcdefjhijkinpqrsduvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' |
| 85 | + for (let i = 0; i < codeLength; i++) { |
| 86 | + code += identifyCodes[randomNum(0, identifyCodes.length)] |
| 87 | + } |
| 88 | + return code |
| 89 | +} |
| 90 | +
|
| 91 | +// 生成一个随机数 |
| 92 | +const randomNum = (min = 0, max: number) => Math.floor(Math.random() * (max - min)) + min |
| 93 | +
|
| 94 | +// 生成一个随机的颜色 |
| 95 | +function randomColor(min: number, max: number) { |
| 96 | + let r = randomNum(min, max) |
| 97 | + let g = randomNum(min, max) |
| 98 | + let b = randomNum(min, max) |
| 99 | + return 'rgb(' + r + ',' + g + ',' + b + ')' |
| 100 | +} |
| 101 | +
|
| 102 | +// 绘制干扰线 |
| 103 | +const drawLine = (ctx: CanvasRenderingContext2D) => { |
| 104 | + for (let i = 0; i < 5; i++) { |
| 105 | + ctx.strokeStyle = randomColor(props.lineColorMin, props.lineColorMax) |
| 106 | + ctx.beginPath() |
| 107 | + ctx.moveTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight)) |
| 108 | + ctx.lineTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight)) |
| 109 | + ctx.stroke() |
| 110 | + } |
| 111 | +} |
| 112 | +//在画布上显示数据 |
| 113 | +const drawText = (ctx: CanvasRenderingContext2D, txt: string, i: number) => { |
| 114 | + ctx.fillStyle = randomColor(props.colorMin, props.colorMax) |
| 115 | + ctx.font = randomNum(props.fontSizeMin, props.fontSizeMax) + 'px SimHei' |
| 116 | + let x = (i + 1) * (props.contentWidth / (txt.length + 1)) |
| 117 | + let y = randomNum(props.fontSizeMax, props.contentHeight - 5) |
| 118 | + var deg = randomNum(-45, 45) |
| 119 | + // 修改坐标原点和旋转角度 |
| 120 | + ctx.translate(x, y) |
| 121 | + ctx.rotate((deg * Math.PI) / 180) |
| 122 | + ctx.fillText(txt[i], 0, 0) |
| 123 | + // 恢复坐标原点和旋转角度 |
| 124 | + ctx.rotate((-deg * Math.PI) / 180) |
| 125 | + ctx.translate(-x, -y) |
| 126 | +} |
| 127 | +// 绘制干扰点 |
| 128 | +const drawDot = (ctx: CanvasRenderingContext2D) => { |
| 129 | + for (let i = 0; i < 80; i++) { |
| 130 | + ctx.fillStyle = randomColor(0, 255) |
| 131 | + ctx.beginPath() |
| 132 | + ctx.arc(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight), 1, 0, 2 * Math.PI) |
| 133 | + ctx.fill() |
| 134 | + } |
| 135 | +} |
| 136 | +//画图 |
| 137 | +const drawPic = () => { |
| 138 | + let canvas = document.getElementById('VerifyCode-canvas') as HTMLCanvasElement |
| 139 | + if (!canvas) { |
| 140 | + return |
| 141 | + } |
| 142 | + let ctx = canvas.getContext('2d') as CanvasRenderingContext2D |
| 143 | + ctx.textBaseline = 'bottom' |
| 144 | + // 绘制背景 |
| 145 | + ctx.fillStyle = randomColor(props.backgroundColorMin, props.backgroundColorMax) |
| 146 | + ctx.fillRect(0, 0, props.contentWidth, props.contentHeight) |
| 147 | + // 绘制文字 |
| 148 | + for (let i = 0; i < verifyCode.value.length; i++) { |
| 149 | + drawText(ctx, verifyCode.value, i) |
| 150 | + } |
| 151 | + drawLine(ctx) |
| 152 | + drawDot(ctx) |
| 153 | +} |
| 154 | +
|
| 155 | +// 重置验证码 |
| 156 | +const refreshCode = () => { |
| 157 | + emit('update:code', makeCode()) |
| 158 | + drawPic() |
| 159 | +} |
| 160 | +
|
| 161 | +// defineExpose({ refreshCode }); |
| 162 | +
|
| 163 | +//组件挂载 |
| 164 | +onMounted(() => { |
| 165 | + drawPic() |
| 166 | +}) |
| 167 | +</script> |
| 168 | +<style scoped lang="scss"> |
| 169 | +.VerifyCode { |
| 170 | + cursor: pointer; |
| 171 | +} |
| 172 | +</style> |
0 commit comments