vue 验证码

旧城等待, 2023-01-12 10:20 190阅读 0赞

一、概述

效果图

931d981a4579e273748953af7e22d71f.png

二、代码实现

新建test目录,放2个vue文件。

39d1a8d6e83cbc80be8b31f064d1ceea.png

sidentify.vue组件代码:

  1. <template>
  2. <div class="s-canvas">
  3. <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'SIdentify',
  9. props: {
  10. identifyCode: {
  11. type: String,
  12. default: '1234'
  13. },
  14. fontSizeMin: {
  15. type: Number,
  16. default: 25
  17. },
  18. fontSizeMax: {
  19. type: Number,
  20. default: 30
  21. },
  22. backgroundColorMin: {
  23. type: Number,
  24. default: 255
  25. },
  26. backgroundColorMax: {
  27. type: Number,
  28. default: 255
  29. },
  30. colorMin: {
  31. type: Number,
  32. default: 0
  33. },
  34. colorMax: {
  35. type: Number,
  36. default: 160
  37. },
  38. lineColorMin: {
  39. type: Number,
  40. default: 100
  41. },
  42. lineColorMax: {
  43. type: Number,
  44. default: 255
  45. },
  46. dotColorMin: {
  47. type: Number,
  48. default: 0
  49. },
  50. dotColorMax: {
  51. type: Number,
  52. default: 255
  53. },
  54. contentWidth: {
  55. type: Number,
  56. default: 112
  57. },
  58. contentHeight: {
  59. type: Number,
  60. default: 31
  61. }
  62. },
  63. methods: {
  64. // 生成一个随机数
  65. randomNum(min, max) {
  66. return Math.floor(Math.random() * (max - min) + min)
  67. },
  68. // 生成一个随机的颜色
  69. randomColor(min, max) {
  70. let r = this.randomNum(min, max)
  71. let g = this.randomNum(min, max)
  72. let b = this.randomNum(min, max)
  73. return 'rgb(' + r + ',' + g + ',' + b + ')'
  74. },
  75. drawPic() {
  76. let canvas = document.getElementById('s-canvas')
  77. let ctx = canvas.getContext('2d')
  78. ctx.textBaseline = 'bottom'
  79. // 绘制背景
  80. ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
  81. ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
  82. // 绘制文字
  83. for (let i = 0; i < this.identifyCode.length; i++) {
  84. this.drawText(ctx, this.identifyCode[i], i)
  85. }
  86. this.drawLine(ctx)
  87. this.drawDot(ctx)
  88. },
  89. drawText(ctx, txt, i) {
  90. ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
  91. ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
  92. let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
  93. let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
  94. var deg = this.randomNum(-45, 45)
  95. // 修改坐标原点和旋转角度
  96. ctx.translate(x, y)
  97. ctx.rotate(deg * Math.PI / 180)
  98. ctx.fillText(txt, 0, 0)
  99. // 恢复坐标原点和旋转角度
  100. ctx.rotate(-deg * Math.PI / 180)
  101. ctx.translate(-x, -y)
  102. },
  103. drawLine(ctx) {
  104. // 绘制干扰线
  105. for (let i = 0; i < 5; i++) {
  106. ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
  107. ctx.beginPath()
  108. ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
  109. ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
  110. ctx.stroke()
  111. }
  112. },
  113. drawDot(ctx) {
  114. // 绘制干扰点
  115. for (let i = 0; i < 80; i++) {
  116. ctx.fillStyle = this.randomColor(0, 255)
  117. ctx.beginPath()
  118. ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
  119. ctx.fill()
  120. }
  121. }
  122. },
  123. watch: {
  124. identifyCode() {
  125. this.drawPic()
  126. }
  127. },
  128. mounted() {
  129. this.drawPic()
  130. }
  131. }
  132. </script>
  133. <style scoped>
  134. .s-canvas {
  135. height: 38px;
  136. }
  137. .s-canvas canvas{
  138. margin-top: 1px;
  139. margin-left: 8px;
  140. }
  141. </style>

说明

0df9038d18f9e6b2d352bc93d5aada40.png

test.vue

  1. <template>
  2. <div class="form-group" style="display: flex;">
  3. <div>
  4. <span>验证码:</span>
  5. <input type="text" id="code" v-model="code" class="code" placeholder="请输入验证码"/>
  6. </div>
  7. <div class="login-code" @click="refreshCode">
  8. <!--验证码组件-->
  9. <s-identify :identifyCode="identifyCode"></s-identify>
  10. </div>
  11. <div>
  12. <button @click="checkCaptcha">验证</button>
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. import SIdentify from '../login/sidentify'
  18. export default {
  19. components: { SIdentify },
  20. data() {
  21. return {
  22. identifyCodes: '1234567890',
  23. identifyCode: '',
  24. code: '',//text框输入的验证码
  25. tableData: []
  26. }
  27. },
  28. mounted: function() {
  29. this.identifyCode = ''
  30. // 初始化验证码
  31. this.makeCode(this.identifyCodes, 4)
  32. },
  33. methods: {
  34. //验证码
  35. randomNum(min, max) {
  36. return Math.floor(Math.random() * (max - min) + min)
  37. },
  38. refreshCode() {
  39. this.identifyCode = ''
  40. this.makeCode(this.identifyCodes, 4)
  41. },
  42. makeCode(o, l) {
  43. for (let i = 0; i < l; i++) {
  44. this.identifyCode += this.identifyCodes[
  45. this.randomNum(0, this.identifyCodes.length)
  46. ]
  47. }
  48. console.log("验证码",this.identifyCode)
  49. },
  50. // 检查验证码是否正确
  51. checkCaptcha(){
  52. if (this.code == ""){
  53. alert("请输入验证码")
  54. return false
  55. }
  56. if (this.identifyCode != this.code){
  57. this.code = ''
  58. this.refreshCode()
  59. alert("请输入正确的验证码")
  60. return false
  61. }
  62. console.log("验证码正确")
  63. }
  64. }
  65. }
  66. </script>
  67. <style>
  68. /*验证码样式*/
  69. .code {
  70. width: 124px;
  71. height: 31px;
  72. border: 1px solid rgba(186, 186, 186, 1);
  73. }
  74. .login-code {
  75. cursor: pointer;
  76. }
  77. </style>

访问页面,输入正确的验证码,会有提示。

e98c054783747aa54f1e9d513fa2affb.png

注意:我在console中,输出了正确的验证码,照着填写即可。

这个样式,可能不太美观。如果需要用到项目中,可以自行调整样式。

本文参考链接:

https://www.cnblogs.com/web-aqin/p/10796326.html

发表评论

表情:
评论列表 (有 0 条评论,190人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue实现验证

    在Web应用程序中,为了避免机器自动化或恶意攻击,很常见的做法是要求用户在表单提交之前输入验证码。验证码最常见的形式是图片验证码,因为图片验证码最大程度地防止了自动化机器调用A