vue登录页面设置验证码input框的方法

 更新时间:2022年04月14日 08:16:11   作者:张小欢ㄦ~  
这篇文章主要为大家详细介绍了vue登录页面设置验证码input框的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue登录页面设置验证码input框的具体代码,供大家参考,具体内容如下

1、效果

2、代码

第一步:建立子组件(举例,文件名可自己取)----代码如下

子组件 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>

第二步:在父组件中导入并注册子组件

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

第三步:在登录表单上加上验证码的样式

<el-form-item prop="inputCode">
            <div style="display:flex" id="atilposition">
              <el-input
                type="text"
                class="verificationcode"
                ref="inputCode"
                v-model="loginForm.inputCode"
                style="width:175px"
                prefix-icon="el-icon-key"
                placeholder="请输入验证码"
                clearable
              ></el-input>
              <span @click="createCode" id="spancode">
                <SIdentify :identifyCode="code"></SIdentify>
              </span>
            </div>
</el-form-item>

第四步: 父组件中随机生成一串字符串-------数字+字母

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

第五步:在点击登录按钮后的方法中判断验证码部分(加 .toLowerCace() 是为了不区分大小写)

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

具体位置如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 利用Vue3实现可复制表格的方法详解

    利用Vue3实现可复制表格的方法详解

    表格是前端非常常用的一个控件,本文主要为大家介绍了Vue3如何实现一个简易的可以复制的表格,文中的示例代码讲解详细,需要的可以参考一下
    2022-12-12
  • Vue组件和Route的生命周期实例详解

    Vue组件和Route的生命周期实例详解

    这篇文章主要介绍了Vue组件和Route的生命周期的相关知识,需要的朋友可以参考下
    2018-02-02
  • Vue中使用import进行路由懒加载的原理分析

    Vue中使用import进行路由懒加载的原理分析

    这篇文章主要介绍了Vue中使用import进行路由懒加载的原理分析。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • 解决Vue中引入swiper,在数据渲染的时候,发生不滑动的问题

    解决Vue中引入swiper,在数据渲染的时候,发生不滑动的问题

    今天小编就为大家分享一篇解决Vue中引入swiper,在数据渲染的时候,发生不滑动的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • 在Vue methods中调用filters里的过滤器实例

    在Vue methods中调用filters里的过滤器实例

    今天小编就为大家分享一篇在Vue methods中调用filters里的过滤器实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Vue3设置Proxy代理解决跨域问题

    Vue3设置Proxy代理解决跨域问题

    这篇文章主要介绍了Vue3设置Proxy代理解决跨域问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Vuex中this.$store.commit()和this.$store.dispatch()区别说明

    Vuex中this.$store.commit()和this.$store.dispatch()区别说明

    这篇文章主要介绍了Vuex中this.$store.commit()和this.$store.dispatch()区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • 详解Vue This$Store总结

    详解Vue This$Store总结

    这篇文章主要介绍了详解Vue This$Store总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • vue实现价格日历效果

    vue实现价格日历效果

    这篇文章主要为大家详细介绍了vue实现价格日历效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • vue时间戳和时间的相互转换方式

    vue时间戳和时间的相互转换方式

    本文通过示例代码介绍了vue时间戳和时间的相互转换方式,通过场景分析介绍了vue3使用组合式api将时间戳格式转换成时间格式(2023年09月28日 10:00),感兴趣的朋友一起看看吧
    2023-12-12

最新评论