如何在vue中使用图形验证码
首先在component问价夹下创建一个新文件夹并叫identity,并在里面编写组件
<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: 16},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: 38}},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 < 8; 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 < 50; 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>
在main.js中将它变成全局组件方便调用
import Identity from '@/components/identify/identify.vue'
Vue.component('identity',Identity)
然后在你想使用的组件页面中直接写,identifyCode代表的是你的验证码
<identity :identifyCode="identifyCode"></identity>
<script>
export default {name: 'Home',data(){return{identifyCodes: "1234567890",identifyCode: ""}},components: {},mounted(){this.refreshCode()//刷新生成随机验证码的方法},methods:{/*图形验证码 */randomNum(min,max){return Math.floor(Math.random() * (max - min) + min);},refreshCode(){this.identifyCode = "";this.makeCode( this.identifyCodes,4);console.log('当前验证吗',this.identifyCode);},makeCode(o,l){for(let i =0;i<l;i++){this.identifyCode += this.identifyCodes[this.randomNum(0,this.identifyCodes.length)]}}}
}
</script>
如何在vue中使用图形验证码相关推荐
- 在vue项目或者react项目中实现图形验证码功能
效果演示 使用说明 我对这个图形验证码做了一个组件的封装,如果你的项目是vue项目就粘贴vue章节的组件代码,是react项目就粘贴react章节的组件代码.组件的使用很简单,只需要传递一个prop, ...
- vue 递归创建菜单_如何在Vue中创建类似中等的突出显示菜单
vue 递归创建菜单 by Taha Shashtari 由Taha Shashtari 如何在Vue中创建类似中等的突出显示菜单 (How to Create a Medium-Like Highl ...
- 中boxplot函数的参数设置_如何在Python中生成图形和图表
在本章中,我们将学习如何在Python中生成图形和图表,同时将使用函数和面向对象的方法来可视化数据. Python中常用的一些可视化数据包括以下几种. Matplotlib. Seaborn. ggp ...
- 如何在 Vue 中使用 Chart.js - 手把手教你搭可视化数据图表
本文首发:<如何在 Vue 中使用 Chart.js - 手把手教你搭可视化数据图表> 使用 Chart.js 在 Vue 搭建的后台管理工具里添加炫酷的图表,是所有数据展示类后台必备的功 ...
- 如何在 Vue 中导出数据至 Excel 表格 - 卡拉云
本文首发:<如何在 Vue 中导出数据至 Excel 表格 - 卡拉云> 我们经常需要在 Vue 搭建的后台管理系统里导出数据到 Excel / CSV ,方便我们将数据共享给其他同学或在 ...
- 骨架屏技术讲解以及如何在Vue中实现骨架屏
骨架屏技术讲解以及如何在Vue中实现骨架屏 写在前面 骨架屏是什么 如何实现(原理分析) 一个生动的例子 实现方式(具体实现) 方案一.在模版中来实现骨架屏 方案二.使用一个Base64的图片来作为骨 ...
- html的表单图形验证码怎么做,django中简单图形验证码实现
要实现django图形验证码,可以使用简单的captcha 一.安装captcha 在Pycharm的terminal中,使用pip安装第三方库: 执行命令: pip install django-s ...
- 如何在Vue 中管理 Mixins(搞懂这两点就足够了)
转载地址:https://segmentfault.com/a/1190000021772893 当我们的Vue项目功能越来越多时,如果有类似的组件,可能会发现自己一次又一次地复制和粘贴相同的数据.方 ...
- vue 数组转集合_思想实验:如何在Vue中使localStorage具有响应式?
响应式是Vue的最大特色之一.如果你不知道幕后情况,它也是最神秘的地方之一.例如,为什么它不能用于对象和数组,而不能用于诸如 localStorage 之类的其他东西? 让我们回答这个问题,在解决这个 ...
最新文章
- signature验证/salt验证/token验证的作用
- Redis简介及入门
- Ext JS 4.1 RC1发布
- labelimg如何调整框的颜色_如何制作摄影集(下)
- RecyclerView.Adapter的封装(RecyclerAdapter)
- python生成验证码_Python快速生成验证码
- JSTL EL 详解
- 国内外cms网站大全
- FOC——14.15.过流保护电路与单片机外围电路
- 洛谷P3966 [TJOI2013]单词 题解
- 快手公布于香港联交所主板上市计划详情,拟融资394.77亿港元
- 什么是Excel的快速编号功能?
- 20211101bugku_re_mountain_climbing
- RK3568-B2-ANDROID11-WIFI-RTL8821CU
- 组件实战(一)——导航栏设计
- 微软黑科技:DNA存储技术催生方糖大小的数据中心
- 【招聘推荐】启元世界招聘深度强化学习算法工程师
- gis 中常用cql 记录(mysql、sql server)(主要是投影转换)
- python简单温度转换,python实现简单温度转换的方法
- python线性回归算法
热门文章
- python 虚拟环境 django.db 报错_jumpserver一体化安装
- python怎么读取word文件_python之python-docx编辑和读取word文档
- mysql的explain怎么看_mysql中explain用法详解
- c++ opencv添加logo_OpenCV-Python 图像平滑 | 十六
- java jmx 配置_Java JMX远程调优一点就通
- centos6 dns转发_CentOS6 配置DNS服务器
- c语言将一个已知头结点的单链表逆序_C语言实现单链表逆序与逆序输出实例
- HH的项链 HYSBZ - 1878 (莫队/ 树状数组)
- netty源码解解析(4.0)-3 Channel的抽象实现
- Bootloader之uBoot简介(转)