Vue 实现登录界面验证码功能


Posted in Javascript onJanuary 03, 2020

登录界面

Vue 实现登录界面验证码功能

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: 112
 },
 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>

在登录界面引入验证码组件并注册

import SIdentify from '@/components/common/SIdentify'
 components: { SIdentify },

登录的form

<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
 <el-form-item prop="username">
  <el-input v-model="ruleForm.username" placeholder="账号">
  <i slot="prepend" class="el-icon-s-custom"/>
  </el-input>
 </el-form-item>
 <el-form-item prop="password">
  <el-input type="password" placeholder="密码" v-model="ruleForm.password">
  <i slot="prepend" class="el-icon-lock"/>
  </el-input>
 </el-form-item>
 <el-form-item prop="code">
  <el-row :span="24">
  <el-col :span="12">
  <el-input v-model="ruleForm.code" auto-complete="off" placeholder="请输入验证码" size=""
   @keyup.enter.native="submitForm('ruleForm')"></el-input>
  </el-col>
  <el-col :span="12">
  <div class="login-code" @click="refreshCode">
  <!--验证码组件-->
  <s-identify :identifyCode="identifyCode"></s-identify>
  </div>
  </el-col>
  </el-row>
 </el-form-item>
 <el-form-item>
  <div class="login-btn">
  <el-button type="primary" @click="submitForm()">登录</el-button>
  </div>
 </el-form-item>
 <p style="font-size:12px;line-height:30px;color:#eaeaea;">Tips : 请输入账号密码登陆</p>
 </el-form>

点击刷新验证码

randomNum (min, max) {
 return Math.floor(Math.random() * (max - min) + min)
 },
 refreshCode () {
 this.identifyCode = ''
 this.makeCode(this.identifyCodes, 4)
 },
 makeCode (o, l) {
 for (let i = 0; i < l; i++) {
 this.identifyCode += this.identifyCodes[
  this.randomNum(0, this.identifyCodes.length)
 ]
 }
 }

总结

以上所述是小编给大家介绍的Vue 实现登录界面验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Javascript 相关文章推荐
jQuery中的bind绑定事件与文本框改变事件的临时解决方法
Aug 13 Javascript
jquery动画3.创建一个带遮罩效果的图片走廊
Aug 24 Javascript
jQuery阻止同类型事件小结
Apr 19 Javascript
使用Vue完成一个简单的todolist的方法
Dec 01 Javascript
Vue DevTools调试工具的使用
Dec 05 Javascript
jQuery实现简单的下拉菜单导航功能示例
Dec 07 jQuery
React从react-router路由上做登陆验证控制的方法
May 10 Javascript
vue实现在一个方法执行完后执行另一个方法的示例
Aug 25 Javascript
node使用Mongoose类库实现简单的增删改查
Nov 08 Javascript
JavaScript基础之静态方法和实例方法分析
Dec 26 Javascript
微信小程序MUI导航栏透明渐变功能示例(通过改变opacity实现)
Jan 24 Javascript
VUE 实现element upload上传图片到阿里云
Aug 12 Javascript
JS实现扫码枪扫描二维码功能
Jan 03 #Javascript
file-loader打包图片文件时路径错误输出为[object-module]的解决方法
Jan 03 #Javascript
uni-app如何实现增量更新功能
Jan 03 #Javascript
JS实现简单日历特效
Jan 03 #Javascript
微信小程序中的video视频实现 自定义播放按钮、封面图、视频封面上文案
Jan 02 #Javascript
微信小程序地图绘制线段并且测量(实例代码)
Jan 02 #Javascript
Element-UI+Vue模式使用总结
Jan 02 #Javascript
You might like
解析如何修改phpmyadmin中的默认登陆超时时间
2013/06/25 PHP
CodeIgniter钩子用法实例详解
2016/01/20 PHP
laravel unique验证、确认密码confirmed验证以及密码修改验证的方法
2019/10/16 PHP
滚动经典最新话题[prototype框架]下编写
2006/10/03 Javascript
jquery 选项卡效果 新手代码
2011/07/08 Javascript
JS添加删除一组文本框并对输入信息加以验证判断其正确性
2013/04/11 Javascript
jQuery动画效果-fadeIn fadeOut淡入浅出示例代码
2013/08/28 Javascript
返回页面顶部top按钮通过锚点实现(自写)
2013/08/30 Javascript
iframe的onreadystatechange事件在firefox下的使用
2014/04/16 Javascript
JavaScript匿名函数与委托使用示例
2014/07/22 Javascript
jQuery实现鼠标经过图片变亮其他变暗效果
2015/05/08 Javascript
JS中的THIS和WINDOW.EVENT.SRCELEMENT详解
2015/05/25 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
微信小程序 wxapp内容组件 icon详细介绍
2016/10/31 Javascript
基于vue2框架的机器人自动回复mini-project实例代码
2017/06/13 Javascript
基于Datatables跳转到指定页的简单实例
2017/11/09 Javascript
node简单实现一个更改头像功能的示例
2017/12/29 Javascript
通过vue-cli3构建一个SSR应用程序的方法
2018/09/13 Javascript
JS实现随机抽取三人
2019/11/06 Javascript
jQuery实现简单评论功能
2020/08/19 jQuery
vue3.0 自适应不同分辨率电脑的操作
2021/02/06 Vue.js
Python中urllib+urllib2+cookielib模块编写爬虫实战
2016/01/20 Python
python实现按行切分文本文件的方法
2016/04/18 Python
python用reduce和map把字符串转为数字的方法
2016/12/19 Python
简单了解python数组的基本操作
2019/11/26 Python
keras的siamese(孪生网络)实现案例
2020/06/12 Python
CentOS 7如何实现定时执行python脚本
2020/06/24 Python
Lacoste澳大利亚官网:服装、鞋类及配饰
2018/11/14 全球购物
捷克移动配件网上商店:ProMobily.cz
2019/03/15 全球购物
三年级音乐教学反思
2014/01/28 职场文书
英语一分钟演讲稿
2014/04/29 职场文书
五分钟演讲稿
2014/04/30 职场文书
敬老院院长事迹材料
2014/05/21 职场文书
电影复兴之路观后感
2015/06/02 职场文书
vue动态绑定style样式
2022/04/20 Vue.js
阿里云服务器(windows)手动部署FTP站点详细教程
2022/08/05 Servers