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

先来demo效果图

canvas验证码组件(可直接复制,无需改动)

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

login页面的html部分,按需改动

引入验证码组件

methods方法

login页面完整代码

<style lang="less">
@import './login.less';
</style>
<template>
<div class="login" @keydown.enter="handlesubmit">
<div class="login-con">
<card :bordered="false" style="width: 350px;height: 380px">
<div class="form-con">
<tabs value="name1" :animated="false">
<tabpane label="登录" name="name1">
<form ref="loginform" :model="form" :rules="rules" :label-width="90" inline>
<formitem label="帐号" prop="username">
<input v-model="form.username" placeholder="请输入帐号" ref="input" clearable style="width: 200px"/>
</formitem>
<formitem label="密码" prop="password">
<input v-model="form.password" placeholder="请输入密码" clearable  style="width: 200px"/>
</formitem>
<formitem label="验证码" prop="verificationcode">
<input v-model="form.verificationcode" placeholder="请输入验证码" clearable  style="width: 200px"/>
<div @click="refreshcode" style="margin-top: 20px">
<!--验证码组件-->
<s-identify :identifycode="identifycode"></s-identify>
</div>
</formitem>
<div style="text-align: center">
<button @click="handlesubmit" type="primary" style="margin-right: 20px">登录</button>
</div>
</form>
</tabpane>
<tabpane label="注册" name="name2">
<form ref="formvalidate" :model="formvalidate" :rules="rulevalidate" :label-width="90" inline>
<formitem label="帐号" prop="username">
<input v-model="formvalidate.username" placeholder="请输入帐号" ref="input" clearable style="width: 200px"/>
</formitem>
<formitem label="密码" prop="password">
<input v-model="formvalidate.password" placeholder="请输入密码" clearable  style="width: 200px"/>
</formitem>
<formitem label="手机号" prop="mobile">
<input v-model="formvalidate.mobile" placeholder="请输入手机号" clearable style="width: 200px" />
</formitem>
<formitem label="短信验证码" prop="header">
<input v-model="formvalidate.header" placeholder="短信验证码" style="width: 200px"/>
<button type="primary" size="small" @click="getcode" :disabled="codedisabled">{{codemsg}}</button>
</formitem>
<div style="text-align: center">
<button type="primary" @click="register('formvalidate')">注册</button>
</div>
</form>
</tabpane>
</tabs>
</div>
</card>
</div>
<!--<vue-particles
color="#ff4500"
:particleopacity="0.7"
:particlesnumber="300"
shapetype="circle"
:particlesize="7"
linescolor="#00ff00"
:lineswidth="2"
:linelinked="true"
:lineopacity="0.4"
:linesdistance="150"
:movespeed="4"
:hovereffect="true"
hovermode="grab"
:clickeffect="true"
clickmode="repulse"
>
</vue-particles>-->
</div>
</template>
<script>
import cookies from 'js-cookie';
import {apirequest, login, getcode} from '@/service/service';
import sidentify from '@/components/sidentify';
export default {
components: { sidentify },
name: 'login',
data() {
return {
form: {},
formvalidate: {},
rules: {
username: [
{required: true, message: '登录用户名不能为空', trigger: 'blur'}
],
password: [
{required: true, message: '登录密码不能为空', trigger: 'blur'}
],
verificationcode: [
{required: true, message: '验证码不能为空', trigger: 'blur'}
]
},
rulevalidate: {
username: [
{required: true, message: '登录用户名不能为空', trigger: 'blur'}
],
password: [
{required: true, message: '登录密码不能为空', trigger: 'blur'}
],
mobile: [
{required: true, message: '手机号不能为空', trigger: 'blur'}
],
header: [
{required: true, message: '短信验证码不能为空', trigger: 'blur'}
]
},
img:'../../static/grey_wolf.jpg',
// 是否禁用按钮
codedisabled: false,
// 倒计时秒数
countdown: 60,
// 按钮上的文字
codemsg: '获取验证码',
// 定时器
timer: null,
identifycode: '',
identifycodes: '1234567890abcdefjhijklinopqrsduvwxyz',
};
},
methods: {
// 刷新验证码
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)]
}
},
randomnum (min, max) {
return math.floor(math.random() * (max - min) + min)
},
// 获取短信验证码
getcode() {
// 验证码60秒倒计时
if (!this.timer) {
this.getvalidstr();
this.timer = setinterval(this.getvalidstr, 1000);
}
apirequest(this, getcode(this.form.mobile), response => {
});
},
getvalidstr(){
if (this.countdown > 0 && this.countdown <= 60) {
this.countdown--;
if (this.countdown !== 0) {
this.codemsg = "重新发送(" + this.countdown + ")";
this.codedisabled = true;
} else {
clearinterval(this.timer);
this.codemsg = "获取验证码";
this.countdown = 60;
this.timer = null;
this.codedisabled = false;
}
}
},
handlesubmit() {
this.$refs.loginform.validate((valid) => {
if (valid) {
//登录密码做md5加密
let password = this.$copyto.md5(this.form.password);
//登录接口请求
apirequest(this, login(this.form.username, password), response => {
this.$store.commit('setuserinfo', response.data);
cookies.set('user', this.form.username);
cookies.set('userid', response.data.id);
localstorage.sessionid = response.sessionid
this.$store.commit('setavator', '');
if (this.form.username === 'admin') {
cookies.set('access', 0);
} else {
cookies.set('access', 1);
}
this.$router.push({name: 'home_index'});
});
}
});
},
register() {
}
},
mounted () {
// 初始化验证码
this.identifycode = ''
this.makecode(this.identifycodes, 4)
},
};
</script>
<style>
</style>

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