登录页图形验证码插件

identify是一款使用使用canvas来生成图形验证码的vue插件

npm i identify --save

可以全局引入


   
   
    
    
  1. import SIdentify from './components/page/identify'
  2. Vue. use(SIdentify)

也可以局部引入,本文采用局部引入 

 

下面是实现的代码:

子组件


   
   
    
    
  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: 18
  17. },
  18. fontSizeMax: {
  19. type: Number,
  20. default: 40
  21. },
  22. backgroundColorMin: {
  23. type: Number,
  24. default: 180
  25. },
  26. backgroundColorMax: {
  27. type: Number,
  28. default: 240
  29. },
  30. colorMin: {
  31. type: Number,
  32. default: 50
  33. },
  34. colorMax: {
  35. type: Number,
  36. default: 160
  37. },
  38. lineColorMin: {
  39. type: Number,
  40. default: 40
  41. },
  42. lineColorMax: {
  43. type: Number,
  44. default: 180
  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: 111
  57. },
  58. contentHeight: {
  59. type: Number,
  60. default: 38
  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. / / 绘制文本
  90. drawText(ctx, txt, i) {
  91. ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
  92. ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
  93. let x = (i + 1) * (this.contentWidth / (this.identifyCode. length + 1))
  94. let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
  95. var deg = this.randomNum(- 30, 30) / / 字符旋转角度(不超过 45度比较好)
  96. / / 修改坐标原点和旋转角度
  97. ctx.translate(x, y)
  98. ctx.rotate(deg * Math.PI / 180)
  99. ctx.fillText(txt, 0, 0)
  100. / / 恢复坐标原点和旋转角度
  101. ctx.rotate(-deg * Math.PI / 180)
  102. ctx.translate(-x, -y)
  103. },
  104. drawLine(ctx) {
  105. / / 绘制干扰线
  106. for (let i = 0; i < 8; i + +) {
  107. ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
  108. ctx.beginPath()
  109. ctx.moveTo(this.randomNum( 0, this.contentWidth), this.randomNum( 0, this.contentHeight))
  110. ctx.lineTo(this.randomNum( 0, this.contentWidth), this.randomNum( 0, this.contentHeight))
  111. ctx.stroke()
  112. }
  113. },
  114. drawDot(ctx) {
  115. / / 绘制干扰点
  116. for (let i = 0; i < 100; i + +) {
  117. ctx.fillStyle = this.randomColor( 0, 255)
  118. ctx.beginPath()
  119. ctx.arc(this.randomNum( 0, this.contentWidth), this.randomNum( 0, this.contentHeight), 1, 0, 2 * Math.PI)
  120. ctx.fill()
  121. }
  122. }
  123. },
  124. watch: {
  125. identifyCode() {
  126. this.drawPic()
  127. }
  128. },
  129. mounted() {
  130. this.drawPic()
  131. }
  132. }
  133. < /script >

父组件


   
   
    
    
  1. <template >
  2. <div >
  3. <div >验证码测试 < /div >
  4. <div @click = "refreshCode()" class = "code" style = "cursor:pointer;" title = "点击切换验证码" >
  5. <s-identify :identifyCode = "identifyCode" > < /s-identify >
  6. < /div >
  7. < /div >
  8. < /template >
  9. <script >
  10. import { defineComponent } from 'vue';
  11. import sIdentify from "../../src/components/Sidentify.vue";
  12. / / import axios from 'axios'
  13. export default defineComponent({
  14. name: 'list',
  15. components: { sIdentify },
  16. data() {
  17. return {
  18. identifyCode: "",
  19. identifyCodes: [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd'], / /根据实际需求加入自己想要的字符
  20. }
  21. },
  22. mounted() {
  23. th is.refreshCode()
  24. },
  25. unmounted() {
  26. },
  27. methods: {
  28. / / 生成随机数
  29. randomNum(min, max) {
  30. max = max + 1
  31. return Math.floor(Math. random() * (max - min) + min);
  32. },
  33. / / 更新验证码
  34. refreshCode() {
  35. this.identifyCode = "";
  36. this.makeCode(this.identifyCodes, 4);
  37. console.log( '当前验证码:',this.identifyCode);
  38. },
  39. / / 随机生成验证码字符串
  40. makeCode( data, len) {
  41. console.log( 'data, len:', data, len)
  42. for (let i = 0; i < len; i + +) {
  43. this.identifyCode + = this.identifyCodes[this.randomNum( 0, this.identifyCodes. length- 1)]
  44. }
  45. },
  46. },
  47. });
  48. < /script >
  49. <style lang = "less" scoped >
  50. . code{
  51. width: 500px;
  52. height: 31.25rem;
  53. border: 1px solid #ddd;
  54. }
  55. < /style >

效果图

 

identify是一款使用使用canvas来生成图形验证码的vue插件

npm i identify --save

可以全局引入


   
   
  
  
  1. import SIdentify from './components/page/identify'
  2. Vue. use(SIdentify)

也可以局部引入,本文采用局部引入 

 

下面是实现的代码:

子组件


   
   
  
  
  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: 18
  17. },
  18. fontSizeMax: {
  19. type: Number,
  20. default: 40
  21. },
  22. backgroundColorMin: {
  23. type: Number,
  24. default: 180
  25. },
  26. backgroundColorMax: {
  27. type: Number,
  28. default: 240
  29. },
  30. colorMin: {
  31. type: Number,
  32. default: 50
  33. },
  34. colorMax: {
  35. type: Number,
  36. default: 160
  37. },
  38. lineColorMin: {
  39. type: Number,
  40. default: 40
  41. },
  42. lineColorMax: {
  43. type: Number,
  44. default: 180
  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: 111
  57. },
  58. contentHeight: {
  59. type: Number,
  60. default: 38
  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. / / 绘制文本
  90. drawText(ctx, txt, i) {
  91. ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
  92. ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
  93. let x = (i + 1) * (this.contentWidth / (this.identifyCode. length + 1))
  94. let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
  95. var deg = this.randomNum(- 30, 30) / / 字符旋转角度(不超过 45度比较好)
  96. / / 修改坐标原点和旋转角度
  97. ctx.translate(x, y)
  98. ctx.rotate(deg * Math.PI / 180)
  99. ctx.fillText(txt, 0, 0)
  100. / / 恢复坐标原点和旋转角度
  101. ctx.rotate(-deg * Math.PI / 180)
  102. ctx.translate(-x, -y)
  103. },
  104. drawLine(ctx) {
  105. / / 绘制干扰线
  106. for (let i = 0; i < 8; i + +) {
  107. ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
  108. ctx.beginPath()
  109. ctx.moveTo(this.randomNum( 0, this.contentWidth), this.randomNum( 0, this.contentHeight))
  110. ctx.lineTo(this.randomNum( 0, this.contentWidth), this.randomNum( 0, this.contentHeight))
  111. ctx.stroke()
  112. }
  113. },
  114. drawDot(ctx) {
  115. / / 绘制干扰点
  116. for (let i = 0; i < 100; i + +) {
  117. ctx.fillStyle = this.randomColor( 0, 255)
  118. ctx.beginPath()
  119. ctx.arc(this.randomNum( 0, this.contentWidth), this.randomNum( 0, this.contentHeight), 1, 0, 2 * Math.PI)
  120. ctx.fill()
  121. }
  122. }
  123. },
  124. watch: {
  125. identifyCode() {
  126. this.drawPic()
  127. }
  128. },
  129. mounted() {
  130. this.drawPic()
  131. }
  132. }
  133. < /script >

父组件


   
   
  
  
  1. <template >
  2. <div >
  3. <div >验证码测试 < /div >
  4. <div @click = "refreshCode()" class = "code" style = "cursor:pointer;" title = "点击切换验证码" >
  5. <s-identify :identifyCode = "identifyCode" > < /s-identify >
  6. < /div >
  7. < /div >
  8. < /template >
  9. <script >
  10. import { defineComponent } from 'vue';
  11. import sIdentify from "../../src/components/Sidentify.vue";
  12. / / import axios from 'axios'
  13. export default defineComponent({
  14. name: 'list',
  15. components: { sIdentify },
  16. data() {
  17. return {
  18. identifyCode: "",
  19. identifyCodes: [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd'], / /根据实际需求加入自己想要的字符
  20. }
  21. },
  22. mounted() {
  23. th is.refreshCode()
  24. },
  25. unmounted() {
  26. },
  27. methods: {
  28. / / 生成随机数
  29. randomNum(min, max) {
  30. max = max + 1
  31. return Math.floor(Math. random() * (max - min) + min);
  32. },
  33. / / 更新验证码
  34. refreshCode() {
  35. this.identifyCode = "";
  36. this.makeCode(this.identifyCodes, 4);
  37. console.log( '当前验证码:',this.identifyCode);
  38. },
  39. / / 随机生成验证码字符串
  40. makeCode( data, len) {
  41. console.log( 'data, len:', data, len)
  42. for (let i = 0; i < len; i + +) {
  43. this.identifyCode + = this.identifyCodes[this.randomNum( 0, this.identifyCodes. length- 1)]
  44. }
  45. },
  46. },
  47. });
  48. < /script >
  49. <style lang = "less" scoped >
  50. . code{
  51. width: 500px;
  52. height: 31.25rem;
  53. border: 1px solid #ddd;
  54. }
  55. < /style >

效果图

 

猜你喜欢

转载自blog.csdn.net/weixin_64310738/article/details/129034206