当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Vue 实现登录界面验证码功能

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

2020年03月09日  | 移动技术网IT编程  | 我要评论

windows7是什么,芙蓉姐姐瘦身后照片,佣兵之战

登录界面

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 实现登录界面验证码功能,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网