一、验证码组件

<template><div class="s-draw"><canvas id="s-draw" :width="contentWidth" :height="contentHeight"></canvas></div>
</template>
<script>
export default {name: 'SIdentify',props: {identifyCode: {type: String,default: '1234',},fontSizeMin: {type: Number,default: 25,},fontSizeMax: {type: Number,default: 30,},backgroundColorMin: {type: Number,default: 255,},backgroundColorMax: {type: Number,default: 255,},colorMin: {type: Number,default: 0,},colorMax: {type: Number,default: 160,},lineColorMin: {type: Number,default: 100,},lineColorMax: {type: Number,default: 255,},dotColorMin: {type: Number,default: 0,},dotColorMax: {type: Number,default: 255,},contentWidth: {type: Number,default: 132,},contentHeight: {type: Number,default: 31,},},methods: {// 生成一个随机数randomNum(min, max) {return Math.floor(Math.random() * (max - min) + min)},// 生成一个随机的颜色randomColor(min, max) {let r = this.randomNum(min, max)let g = this.randomNum(min, max)let b = this.randomNum(min, max)return 'rgb(' + r + ',' + g + ',' + b + ')'},drawPic() {let canvas = document.getElementById('s-draw')let ctx = canvas.getContext('2d')ctx.textBaseline = 'bottom'// 绘制背景ctx.fillStyle = this.randomColor(this.backgroundColorMin,this.backgroundColorMax)ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)// 绘制文字for (let i = 0; i < this.identifyCode.length; i++) {this.drawText(ctx, this.identifyCode[i], i)}this.drawLine(ctx)this.drawDot(ctx)},drawText(ctx, txt, i) {ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)ctx.font =this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)var deg = this.randomNum(-45, 45)// 修改坐标原点和旋转角度ctx.translate(x, y)ctx.rotate((deg * Math.PI) / 180)ctx.fillText(txt, 0, 0)// 恢复坐标原点和旋转角度ctx.rotate((-deg * Math.PI) / 180)ctx.translate(-x, -y)},},watch: {identifyCode() {this.drawPic()},},mounted() {this.drawPic()},
}
</script>
<style scoped>
.s-draw {height: 38px;
}
.s-draw canvas {margin-top: 5px;margin-left: 8px;
}
</style>

二、父组件注册、使用

<template><Form ref="loginForm" :model="form" :rules="rules"><FormItem prop="userName"><Input v-model="form.userName" placeholder="请输入用户名"/></FormItem><FormItem prop="password"><Input type="password" v-model="form.password" placeholder="请输入密码"/></FormItem><FormItem><div style="display:flex" id="atilposition"><Inputtype="text"ref="inputCode"v-model="inputCode"style="width:175px"placeholder="请输入验证码"clearable></Input><span @click="createCode" id="spancode"><Sldentify :identifyCode="code"></Sldentify></span></div><Button :loading="form.loading" @click="handleSubmit" type="primary"  long class="bottom-size">立即登录</Button></FormItem></Form>
</template>
import Sldentify from './SIdentify'components:{Sldentify},data () {return {code:'',inputCode:'',form: {userName: '',password: '',loading: false}}},methods: {createCode() {let text = "";let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";// 设置验证码长度,for( let i=0; i < 4; i++ ){text += possible.charAt(Math.floor(Math.random() * possible.length));}this.code = text},handleSubmit () {this.form.loading = truethis.$refs.loginForm.validate((valid) => {if (valid) {if(this.inputCode == ''){this.$Message.error('请输入验证码')return}if(this.inputCode.toLowerCase() != this.code.toLowerCase()){this.$Message.error('验证码错误')this.inputCode = ''this.createCode()return}this.$emit('on-success-valid', {userName: this.form.userName,password: this.form.password})} else {this.form.loading = false}})}
},
mounted(){
this.createCode()
},

成果:

vue实现前端登录验证码相关推荐

  1. vue 纯前端登录验证码

    效果图 父组件中随机生成一串字符转,数字+字母 mounted(){this.createCode() }, methods:{createCode() {var text = "" ...

  2. 【vue】纯前端登录验证码实现记录

    [写在前面的话] 应项目安全需求,登录验证要新增验证码,下意识觉得是前端的工作,后来才发现前端验证了个寂寞,真正安全的验证码验证工作应该交给后台来做,此是后话,先记录一下纯前端实现的验证码模块. [实 ...

  3. html前端登录验证码,前端登录页面开发_js生成验证码并验证

    /p> 前端集成开发工具: margin: 0; padding: 0; } a { text-decoration: none; } .main_bar { width: 100%; heig ...

  4. vue+elementui 实现登录验证码功能

    最终实现效果(点击图片进行切换 及 验证登录) html // getCode() 点击调切换图片 // @keyup.enter.native="handleLogin" 回车登 ...

  5. vue 通过接口实现前端登录页验证码功能

    记录个人学习使用 页面样式: 接口返回数据: 此处的base64不需要转,可直接赋值使用,详情如methods内的方法显示 html部分: <el-form-item label="& ...

  6. 前端获取验证码、手机号登录、注册功能

    07.前端获取验证码.手机号登录.注册功能 Register.vue <template><div class="register"><div cla ...

  7. SpringBoot+Vue+Mybatis-plus 博客(一):完成博客后台前端登录页面、后端登录接口

    SpringBoot+Vue+Mybatis-plus 博客:个人博客介绍及效果展示 SpringBoot+Vue+Mybatis-plus 博客(一):完成博客后台前端登录页面.后端登录接口 Spr ...

  8. vue框架搭建前端登录界面

    使用vue框架搭建前端登录界面,用v-bind引入类,v-model实现数据绑定,v-on实现事件处理机制的处理. <!DOCTYPE html> <html lang=" ...

  9. 若依前后端分离版怎样去掉登录验证码

    场景 若依前后端分离版手把手教你本地搭建环境并运行项目: 若依前后端分离版手把手教你本地搭建环境并运行项目_BADAO_LIUMANG_QIZHI的博客-CSDN博客_若依前后端分离版本的配置 上面在 ...

最新文章

  1. arm linux嵌入式网络控制系统,基于ARM uCLinux的网络控制系统设计与实现
  2. Python中的str与unicode处理方法
  3. java面向对象课件_《JAVA面向对象基础》PPT课件.ppt
  4. blufr 算法的理解---理解的不对的,欢迎拍砖
  5. win8+sdk8+vs2012+freeglut+glew开发opengl
  6. Visual Studio Code 配合 Node.js 轻松实现JS断点调试
  7. GlobalMapper--去除tif影像黑边
  8. 南阳oj入门题-cigarettes
  9. 国庆黄金周即将到来!包容的三亚期待游客前来探索
  10. 文件锁(一)——文件锁的概述
  11. 深入理解 window.onload
  12. (附源码)基于java的校园二手书籍交易平台 毕业设计131558
  13. 模电学习笔记(上交郑老师)25.深度负反馈放大电路分析
  14. 尼康相机测试软件mac版,Nikon Camera Control
  15. Android图片轮播第三弹,给你想不到的惊喜
  16. C语言文件读写操作,写入数据到文件
  17. XPosed修改微信余额
  18. matlab三维显示圆锥,基于MATLAB和SOLIDWORKS的弧齿锥齿轮三维精确建模
  19. Heritrix配置
  20. linux 易语言窗口程序_易语言开发Linux程序

热门文章

  1. 【安全知识分享】重磅|一般带式输送机司机的操作规程.docx(附下载)
  2. 如何在premiere 中 裁切异形幕4K视频?
  3. Verilog学习脚印1-组合逻辑-反相器
  4. 【Git-10】Eclipse中Git分支管理
  5. 苹果 iOS/iPadOS 14.5 公测版 Beta 2 新增功能有哪些?
  6. 芒格-“永远不要有受害者心态”
  7. 这名乌克兰黑客成为FBI的最佳武器和黑客的噩梦
  8. SCAU SQL 数据库 期末复习
  9. NeRF:火爆科研圈的三维重建技术大揭秘
  10. python大学教程吕云翔课后题答案_python大学教程吕云翔.ppt