vue3+elementplus前端生成图片验证码

这篇具有很好参考价值的文章主要介绍了vue3+elementplus前端生成图片验证码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、安装

使用npm i identify --save 或者 yarn add identify --save

2、新建vue组件components/identify/identify.vue

<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: 28
    },
    fontSizeMax: {
      type: Number,
      default: 40
    },
    backgroundColorMin: {
      type: Number,
      default: 180
    },
    backgroundColorMax: {
      type: Number,
      default: 240
    },
    colorMin: {
      type: Number,
      default: 50
    },
    colorMax: {
      type: Number,
      default: 160
    },
    lineColorMin: {
      type: Number,
      default: 40
    },
    lineColorMax: {
      type: Number,
      default: 180
    },
    dotColorMin: {
      type: Number,
      default: 0
    },
    dotColorMax: {
      type: Number,
      default: 255
    },
    contentWidth: {
      type: Number,
      default: 112
    },
    contentHeight: {
      type: Number,
      default: 40
    }
  },
  methods: {
    // 生成一个随机数
    randomNum (min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    // 生成一个随机的颜色
    randomColor (min, max) {
      var r = this.randomNum(min, max)
      var g = this.randomNum(min, max)
      var b = this.randomNum(min, max)
      return 'rgb(' + r + ',' + g + ',' + b + ')'
    },
    drawPic () {
      var canvas = document.getElementById('s-canvas')
      var 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'
      var x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
      var y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
      var deg = this.randomNum(-30, 30)
      // 修改坐标原点和旋转角度
      ctx.translate(x, y)
      ctx.rotate(deg * Math.PI / 270)
      ctx.fillText(txt, 0, 0)
      // 恢复坐标原点和旋转角度
      ctx.rotate(-deg * Math.PI / 270)
      ctx.translate(-x, -y)
    },
    drawLine (ctx) {
      // 绘制干扰线
      for (let i = 0; i < 2; 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 < 20; 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 lang='less' scoped>
.s-canvas {
    height: 38px;
    cursor: pointer;
}
.s-canvas canvas{
    margin-top: 1px;
    margin-left: 8px;
}
</style>

3、一般是登录页面用到这个,在你的登录页面的from表单的相应位置加上填写验证码的html

 <el-form-item prop="verifycode">
                <el-input v-model="user.verifycode" placeholder="请输入验证码" class="identifyinput">
                </el-input>
                </el-form-item>
               <el-form-item>
              <div class="identifybox">
               <div @click="refreshCode">
              <s-identify :identifyCode="identifyCode"></s-identify>
               </div>
               <el-button @click="refreshCode" type='text' class="textbtn">看不清,换一张</el-button>
              </div>
</el-form-item>

4、在script下引入组件,并编写方法

<script>
// 引入图片验证码组件
import SIdentify from '@/components/identify'
export default {
 components: { SIdentify },
 data() {
    /* 自定义验证码规则 */
    const validateVerifycode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value !== this.identifyCode) {
        callback(new Error('验证码不正确!'))
      } else {
        callback()
      }
    }
    
    return {
      identifyCodes: '1234567890abcdefghijklmnopqrstuvwxyz',
      identifyCode: '',
       rules: { 
       	verifycode: [{
          required: true,
          trigger: 'blur',
          validator: validateVerifycode,}]
      }
    }
 },
  mounted(){
    this.identifyCode='';
    this.makeCode(this.identifyCodes,4);
    history.pushState(null, null, document.URL);
    if (window.history && window.history.pushState) {
      $(window).on('popstate', function (){
        window.history.pushState('forward', null, '');
        window.history.forward(1);
      });
      window.history.pushState('forward', null, ''); //在IE中必须得有这两行
      window.history.forward(1);
    }
 },
 methods: {
  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[
        Math.floor(Math.random() * (this.identifyCodes.length - 0) + 0)
      ]
    }
  },
 }
}
</script>

5、效果

elenment验证码图标,前端,状态模式,vue.js

 文章来源地址https://www.toymoban.com/news/detail-807083.html

到了这里,关于vue3+elementplus前端生成图片验证码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 后端“fastapi”+前端“vue3+ts+ElementPlus”上传文件到uploads目录

    确保已安装好python3和fastapi mail.py 运行fastapi服务器 使用浏览器访问 http://127.0.0.1:8000/http://127.0.0.1:8000/docs 确保已安装node.js和yarn 使用vite初始化前端目录  安装element-plus main.ts中导入element-plus  修改vite.config.ts配置“CORS 跨域” 修改App.vue 运行 使用浏览器访问 http://127.0.0.1:70

    2024年02月22日
    浏览(36)
  • 前端生成图片验证码怎么做?

    ##题记:我们实现一个功能首先想一下我们需要做哪些工作,比如我们需要生成一个随机的图片验证码,我们需要一个就是点击事件获取验证码,通过接口我们去获取图片路径进行渲染就行,这里边还要牵扯一件事情就是获取一个随机数字,通过随机数字我们传给后端索取一

    2024年02月14日
    浏览(26)
  • vue 学习笔记 【ElementPlus】el-menu 折叠后图标不见了

    项目当前版本 运行过程中,菜单折叠后,图标不见了 图标不见了 解决办法是 把el-icon 从#title 中提取出来 如 运行结果 el-icon 还是要放于#title里,否则显示不正常

    2024年02月15日
    浏览(58)
  • Vue3 前端生成随机id( 生成 UUID )

    重新创建一个文件**/utils/someTools.js**,并在里面写入如下代码。 随机打开一个你想要生成id的文件,先 引入 文件,然后调用**guid()**方法。

    2024年04月15日
    浏览(37)
  • 【vue3】前端上传图片的格式大小限制和压缩

    目录 前言 对上传图片进行格式大小限制 压缩上传图片 上篇文章中研究了如何使用双token机制,在此篇中就暴露了一些问题:当accesstoken过期后,直到拿到最终想要得到的数据,期间需要经历三次请求——第一次请求,拿到accesstoken过期的消息——第二次携带refreshtoken发起请求

    2024年02月06日
    浏览(50)
  • Vue3+elementPlus组件递归

    下面以实现导航菜单为例 1、父页面: 2、递归组件:

    2024年04月09日
    浏览(59)
  • VUE3结合ElementPlus的过程

    首先在前端项目的依赖列表中加入Elemen的依赖 { “name”: “vue-process”, “version”: “0.0.0”, “private”: true, “scripts”: { “dev”: “vite”, “build”: “vite build”, “preview”: “vite preview”, “lint”: “eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore” }, “dependencies”:

    2024年02月12日
    浏览(27)
  • Elenment UI表单验证时,有值但还是提示错误,表单验证失效问题

    下午在用Element UI写一个表单的时候突然发现,表单的验证规则失效了 使用Element自带的表单校验规则时,表单为空的时候下方有提示信息,表单不为空的时候依然有提示信息   查看资料找到几种方式都不管用 一种是检查 el-form-item 中的 prop值 和 el-input 的v-model的值是否一致,

    2024年02月11日
    浏览(34)
  • Vue3+elementplus动态表格table实现

    描述 :使用el-table的时候,根据需求,能够实现由字段个数动态增加表格列,表格行数固定为3行。 实现效果 : 实现代码 : 总结 :如果需要控制行数,并且动态加载列数,故需要两个变量进行设置,这里采用的就是这个思想,能够实现需求效果。

    2024年03月15日
    浏览(52)
  • 国内前端vue对接OpenAI/chatgpt【文本互动/生成图片】

       如图;国内通过调用openai接口进行互动,实现图文互动/文本互动  注意:请求人数较多,需要等待   1、🔔 获取ApiKey 注册 OpenAI 账号,获取你的 ApiKey,过程略。 2、💬 聊天接口 ⚠️ 不再推荐使用本接口,后面将废弃。 接口地址 ( POST请求 ) POST https://api.openai.com/pro/cha

    2023年04月20日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包