效果图

父组件中随机生成一串字符转,数字+字母


mounted(){this.createCode()
},
methods:{createCode() {var text = "";var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";// 设置验证码长度,for( var i=0; i < 4; i++ ){text += possible.charAt(Math.floor(Math.random() * possible.length));}this.code = text},
}

导入并注册子组件

import SIdentify from 'components/common/code/SIdentify'export default {components:{SIdentify},data(){return{code:'',inputCode:'',}},}

然后在登录表单中加上

<div style="display:flex"><el-input type="text" v-model="inputCode" style="width:190px" placeholder="请输入验证码" clearable></el-input><span @click="createCode"><SIdentify :identifyCode="code"></SIdentify></span></div>

最后在登录方法中判断 inputCode 是否等于 code就可以了,加 .toLowerCace() 是为了不区分大小写

if(this.inputCode == ''){this.$message.error('请输入验证码')return
}
if(this.inputCode.toLowerCase() != this.code.toLowerCase()){this.$message.error('验证码错误')this.inputCode = ''this.createCode()return
}

子组件 SIdentify 的完整代码

<template><div class="s-canvas"><canvas id="s-canvas" :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-canvas')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)},drawLine(ctx) {// 绘制干扰线for (let i = 0; i < 5; i++) {ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)ctx.beginPath()ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))ctx.stroke()}},drawDot(ctx) {// 绘制干扰点for (let i = 0; i < 80; i++) {ctx.fillStyle = this.randomColor(0, 255)ctx.beginPath()ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)ctx.fill()}}},watch: {identifyCode() {this.drawPic()}},mounted() {this.drawPic()}}
</script>
<style scoped>.s-canvas {height: 38px;}.s-canvas canvas{margin-top: 1px;margin-left: 8px;}
</style>

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

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

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

  2. vue实现前端登录验证码

    一.验证码组件 <template><div class="s-draw"><canvas id="s-draw" :width= ...

  3. ant design Vue 纯前端实现分页

    ant design Vue 纯前端实现分页功能 (有选择的话最好让后端分页,前端分页可能会有问题,为了性能,尽量不要前端分页) 自己想的一个简单方法: 通过页码改变事件,每页数据个数改变事件,获取改 ...

  4. VUE纯前端导出excel表格功能《转载》

    插件:使用vue-json-excel插件实现Vue纯前端导出简单的Excel表格功能. 使用方法 1. 安装依赖 npm install vue-json-excel 2. 引入组件 a. 全局引入 ...

  5. Vue导出word附表部分横向显示(承接上一篇Vue纯前端导出word)

    Vue纯前端导出word直通车 1. 引入js import htmlDocx from "@/utils/html-docx"; 2. page-break-before:alw ...

  6. Vue纯前端:榜单管理系统

    文章目录: 一.主要功能: 二.实现效果: 1.主页: 2.注册: 3.登录: 4.列表界面: 5.添加应用界面: 6.修改应用界面: 7.模糊查询: 三.整体架构: 四.配置文件说明: 五.功能说明 ...

  7. vue纯前端下载表格

    一般我们做后台管理会遇到这样的需求:页面上的表格,需要纯前端技术来完成.不涉及接口对接.不涉及后端,就是你在页面上展示的表格是什么样,就要下载什么样的表格excel,方便人观察,废话不多说,上代码 如 ...

  8. 【验证码功能合集】vue简单实现验证码功能,纯前端实现验证码,拿来即用【输入,滑动,拼图】

    效果图 可以让输入的验证码和图片验证码比对. 上面的是验证码的组件,直接复制贴里面就行了,然后引入到你需要的地方 很简单的两个步骤搞定 (1)验证码组件 这里是验证码的组件,啥也不用动,直接复制创建个 ...

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

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

最新文章

  1. Myisamchk小工具使用手册
  2. oracle asm的安装和配置,ORACLE安装之ASM安装
  3. 3D场景编辑导出-LayaAir引擎Unity插件使用详解
  4. 黑马程序员—八期黑马,我放弃了
  5. 常用web服务器架构理解
  6. Layui table内写done回调事件
  7. 微信扫描普通二维码调起体验版与已发布版的小程序
  8. 程序员软技能:职场、学习、生活,代码之外生存之南
  9. 高通SDX12平台:启动流程梳理
  10. ATFX:道琼斯指数的反弹,11月能否突破35000关口?
  11. 设置Excel表打印时的表头和页码
  12. Android TabLayout基本使用及完美调整指示器位置的技巧
  13. ESXI中设置高格作为旁路由并设置双机热备(VRRP)
  14. 【CVPR智慧城市挑战赛】无监督交通异常检测,冠军团队技术分享
  15. “影响力之父”西奥迪尼:人类就像录音机,按一下就播放
  16. 爬虫练习(2)-- 使用正则匹配爬取果壳问答
  17. 没有使用IaC的DevOps系统都是耍流氓
  18. depot_tools原理和实现
  19. [转载]我的老师唐圭璋先生(王兆鹏)
  20. 智能计算机 英语作文,关于电脑的英语作文

热门文章

  1. 软件方法(下)分析和设计第8章分析 之 分析类图——知识篇Part02(202204更新)
  2. 一周热图|陈小春应采儿夫妇亮相纽约时装周;重庆杜莎夫人蜡像馆推出新展;Alan Walker开启中国巡演...
  3. 普歌-码上鸿鹄团队:配合node+MongoDB后台+vue-cli用API接口获取数据库数据
  4. 矩阵的相似与特征值和特征向量定义
  5. 20、Gateway 过滤器
  6. 是时候开发你自己的vscode扩展插件了
  7. 使用Faster R-CNN(linux-pytorch)训练自己的数据集过程记录
  8. Windows命令提示符常用操作命令
  9. HTML + CSS 实现矩形/圆形进度条效果 - SVG
  10. Android App实现第三方微信登录