vue前端生成验证码及操作

简介: vue前端生成验证码及操作

1、下载less 和less-loader

1. npm i less
2. 
3. npm i less-loader

2、components文件夹下封装vue组件(identify.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: 28
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: 112
57.         },
58. contentHeight: {
59. type: Number,
60. default: 40
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. var r = this.randomNum(min, max)
71. var g = this.randomNum(min, max)
72. var b = this.randomNum(min, max)
73. return 'rgb(' + r + ',' + g + ',' + b + ')'
74.         },
75.         drawPic () {
76. var canvas = document.getElementById('s-canvas')
77. var ctx = canvas.getContext('2d')
78.             ctx.textBaseline = 'bottom'
79. // 绘制背景
80.             ctx.fillStyle = this.randomColor(
81. this.backgroundColorMin,
82. this.backgroundColorMax
83.             )
84.             ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
85. // 绘制文字
86. for (let i = 0; i < this.identifyCode.length; i++) {
87. this.drawText(ctx, this.identifyCode[i], i)
88.             }
89. this.drawLine(ctx)
90. this.drawDot(ctx)
91.         },
92.         drawText (ctx, txt, i) {
93.             ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
94.             ctx.font =
95. this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
96. var x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
97. var y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
98. var deg = this.randomNum(-30, 30)
99. // 修改坐标原点和旋转角度
100.             ctx.translate(x, y)
101.             ctx.rotate(deg * Math.PI / 270)
102.             ctx.fillText(txt, 0, 0)
103. // 恢复坐标原点和旋转角度
104.             ctx.rotate(-deg * Math.PI / 270)
105.             ctx.translate(-x, -y)
106.         },
107.         drawLine (ctx) {
108. // 绘制干扰线
109. for (let i = 0; i < 2; i++) {
110.                 ctx.strokeStyle = this.randomColor(
111. this.lineColorMin,
112. this.lineColorMax
113.                 )
114.                 ctx.beginPath()
115.                 ctx.moveTo(
116. this.randomNum(0, this.contentWidth),
117. this.randomNum(0, this.contentHeight)
118.                 )
119.                 ctx.lineTo(
120. this.randomNum(0, this.contentWidth),
121. this.randomNum(0, this.contentHeight)
122.                 )
123.                 ctx.stroke()
124.             }
125.         },
126.         drawDot (ctx) {
127. // 绘制干扰点
128. for (let i = 0; i < 20; i++) {
129.                 ctx.fillStyle = this.randomColor(0, 255)
130.                 ctx.beginPath()
131.                 ctx.arc(
132. this.randomNum(0, this.contentWidth),
133. this.randomNum(0, this.contentHeight),
134. 1,
135. 0,
136. 2 * Math.PI
137.                 )
138.                 ctx.fill()
139.             }
140.         }
141.     },
142. watch: {
143.         identifyCode () {
144. this.drawPic()
145.         }
146.     },
147.     mounted () {
148. this.drawPic()
149.     }
150. }
151. </script>
152. <style lang='less' scoped>
153. .s-canvas {
154. height: 38px;
155. }
156. .s-canvas canvas{
157. margin-top: 1px;
158. margin-left: 8px;
159. }
160. </style>

3、在需要用到的地方导入该组件

例:登录

1. <template>
2.     <div class="wrapper">
3.         <el-row :gutter="20">
4.     <el-col :span="16">
5.                 <el-input v-model="ruleForm.code" placeholder="请输入验证码"></el-input>
6.             </el-col>
7.             <el-col :span="8" style="margin-top: 8px;">
8.                 <span @click="refreshCode"  style="cursor: pointer;margin-top: 10px">
9.                     <s-identify :identifyCode="identifyCode"></s-identify>
10.                 </span>
11.             </el-col>
12.         </el-row>
13.     </div>
14. </template>
15. 
16. <script>
17. import SIdentify from '@/components/identify.vue'
18. 
19. export default {
20.     name: "Login",
21. components: { SIdentify },
22.     data(){
23.         return{
24.             // 图片验证码
25. identifyCode: '',
26. // 验证码规则
27. identifyCodes: '3456789ABCDEFGHGKMNPQRSTUVWXY',
28.         }
29.     },
30.     methods: {
31.         // 切换验证码
32. refreshCode() {
33. this.identifyCode = ''
34. this.makeCode(this.identifyCodes, 4)
35.         },
36. // 生成随机验证码
37. makeCode(o, l) {
38. for (let i = 0; i < l; i++) {
39. this.identifyCode += this.identifyCodes[Math.floor(Math.random() * (this.identifyCodes.length))]
40.             }
41.         },
42.     },
43.     mounted() {
44.         //进入页面则刷新验证码图片
45. this.refreshCode();
46.     },
47. }
48. </script>


目录
打赏
0
0
0
0
1
分享
相关文章
抛弃node和vscode,如何用记事本开发出一个完整的vue前端项目
本文探讨了在不依赖Node和VSCode的情况下,仅使用记事本和浏览器开发一个完整的Vue3前端项目的方法。通过CDN引入Vue、Vue Router、Element-UI等库,直接编写HTML文件实现页面功能,展示了前端开发的本质是生成HTML。虽然日常开发离不开现代工具,但掌握这种基础方法有助于快速实现想法或应对特殊环境限制。文章还介绍了如何用Node简单部署HTML文件到服务器,提供了一种高效、轻量的开发思路。
67 10
Vue的数据驱动视图与其他前端框架的数据驱动方式有何不同?
总的来说,Vue 的数据驱动视图在诸多方面展现出独特的优势,其与其他前端框架的数据驱动方式的不同之处主要体现在绑定方式、性能表现、触发机制、组件化结合、灵活性、语法表达以及与后端数据交互等方面。这些差异使得 Vue 在前端开发领域具有独特的地位和价值。
118 58
React与Vue:前端框架的巅峰对决与选择策略
【10月更文挑战第23天】React与Vue:前端框架的巅峰对决与选择策略
React与Vue:两大前端框架的较量与选择策略
【10月更文挑战第23天】React与Vue:两大前端框架的较量与选择策略
前端优化之超大数组更新:深入分析Vue/React/Svelte的更新渲染策略
本文对比了 Vue、React 和 Svelte 在数组渲染方面的实现方式和优缺点,探讨了它们与直接操作 DOM 的差异及 Web Components 的实现方式。Vue 通过响应式系统自动管理数据变化,React 利用虚拟 DOM 和 `diffing` 算法优化更新,Svelte 通过编译时优化提升性能。文章还介绍了数组更新的优化策略,如使用 `key`、分片渲染、虚拟滚动等,帮助开发者在处理大型数组时提升性能。总结指出,选择合适的框架应根据项目复杂度和性能需求来决定。
|
4月前
|
vue实现移动端6格验证码源码
这是一个vue移动端6格验证码特效,可支持自动填充,根据项目需求,可将发送验证码功能抽离成单独的组件使用。简单好用,欢迎下载!
51 0
React与Vue:前端框架的巅峰对决与选择策略
【10月更文挑战第23天】 React与Vue:前端框架的巅峰对决与选择策略
2025年前端框架是该选vue还是react?有了大模型-例如通义灵码辅助编码,就不用纠结了!vue用的多选react,react用的多选vue
本文比较了Vue和React两大前端框架,从状态管理、数据流、依赖注入、组件管理等方面进行了详细对比。当前版本和下载量数据显示React更为流行,但Vue在国内用户量增长迅速。Vue 3通过组合式API提供了更灵活的状态管理和组件逻辑复用,适合中小型项目;React则更适合大型项目和复杂交互逻辑。文章还给出了选型建议,强调了多框架学习的重要性,认为技术问题已不再是选型的关键,熟悉各框架的最佳实践更为重要。
1387 0
|
3月前
|
vue使用iconfont图标
vue使用iconfont图标
162 1
Vue Router 核心原理
Vue Router 是 Vue.js 的官方路由管理器,用于实现单页面应用(SPA)的路由功能。其核心原理包括路由配置、监听浏览器事件和组件渲染等。通过定义路径与组件的映射关系,Vue Router 将用户访问的路径与对应的组件关联,支持哈希和历史模式监听 URL 变化,确保页面导航时正确渲染组件。

热门文章

最新文章

  • 1
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    16
  • 2
    【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    9
  • 3
    从前端视角聊聊通义灵码使用经验,如何更好地提升研发效率
    142
  • 4
    详解智能编码在前端研发的创新应用
    10
  • 5
    巧用通义灵码,提升前端研发效率
    20
  • 6
    智能编码在前端研发的创新应用
    12
  • 7
    VSCode AI提效工具,通义灵码前端开发体验
    21
  • 8
    大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
    17
  • 9
    课程预告|前端开发者如何用好通义灵码,这份实战指南请查收
    82
  • 10
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    4