VUE验证码demo
在这里插入代码片
<template>
<div class="demotwo">
<div class="img_bg">
<div class="sliderImg" style="width:360px;height:200px;position:relative;background:#fff;">
<img :src="'data:image/png;base64,'+save_big_url" alt="" class="big_img" style="width:260px;height:160px;position:absolute;right:0">
<div ref="right" style="position:absolute;width:40px;height:40px;top:16px;left:280px;background-color:rgba(0,0,0,0.2);"></div>
<img :src="save_small_url"
alt="" class="small_img"
ref="small_img"
@mousedown="move"
style="position:absolute;width:40px;height:40px;top:16px;left:20px;cursor:crosshair;"
>
<p :style="Verification==1?'background:#55746c;':'background:#20af5c;'" style="width:360px;height:40px;position:absolute;right:0;bottom:0;line-height:40px;color:#fff;text-align: center;">
{{Verification==1?'验证码未成功':'验证成功'}}
</p>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios'
import { baseurl2 } from '../config/urlconfig2.js';
export default{
name:'demotwo',
data() {
return {
positionX: 0,
positionY: 0,
save_big_url: '',
save_small_url: '',
Verification : 1 ,
Verificationcode:1,
tokens:'',
imgwidth:0,
imgheight:0,
imgwidthxiao:0,
imgheightxiao:0,
}
},
mounted(){
this.showImg();
},
methods:{
showImg() {
this.$axios.get(baseurl2+'/hdcaptcha/register','').then((res)=>{
this.save_big_url=res.data.data.bgImg;
this.save_small_url=res.data.data.sliceImg;
let that=this;
this.$refs.img1.onload = function(){
that.imgwidth=this.width;
that.imgheight=this.height;
}
this.$refs.img2.onload = function(){
that.imgwidthxiao=this.width;
that.imgheightxiao=this.height;
}
setTimeout(() =>{
var width1 = this.imgwidth;
var height1 = this.imgheight;
var width1s = Number(this.$refs.imgback.style.width.substring(0,this.$refs.imgback.style.width.length-2));
var heigh1s = Number(this.$refs.imgback.style.height.substring(0,this.$refs.imgback.style.height.length-2));
var width2 = this.imgwidthxiao;
var height2 = this.imgheightxiao;
this.$refs.small_img.style.width=width1s*width2/width1 +'px';
this.$refs.small_img.style.height=heigh1s*height2/height1 +'px';
var heighttop=res.data.data.y * heigh1s / height1;
this.$refs.small_img.style.top = heighttop + 'px';
},100)
this.tokens=res.data.data.token;
}).catch((res) =>{
console.log(res)
this.$message({
message: '背景图片错误',
type: 'warning',
showClose: true,
duration:0,
});
})
},
move(e) {
let odiv = e.target;
let disX = e.clientX - odiv.offsetLeft;
let disY = e.clientY - odiv.offsetTop;
document.onmousemove = (e) => {
if (e.preventDefault) e.preventDefault();
let left = e.clientX - disX;
let top = e.clientY - disY;
this.positionX = left;
this.positionY = top;
let bigImg = document.getElementsByClassName("sliderImg")[0];
const bigX = bigImg.clientWidth - odiv.offsetWidth ;
left = Math.min(Math.max(0, left), bigX);
odiv.style.left = left + 'px';
};
document.onmouseup = (e) => {
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.cancelBubble = true;
document.onmousemove = null;
document.onmouseup = null;
const xvalue = e.clientX - e.offsetX - document.getElementsByClassName("sliderImg")[0].getBoundingClientRect().x;
var xvalues=xvalue-100;
var obj={
"accountName": 123,
"password": 123,
"token": this.tokens,
"sliceX": xvalues
};
var objjson=JSON.stringify(obj);
this.$axios.post(baseurl2+'/hdcaptcha/check',objjson).then((res)=>{
if(res.code==200){
this.Verification=2;
this.Verificationcode=3;
}else{
this.Verificationcode=2;
setTimeout(() =>{
this.Verificationcode=1;
this.$refs.small_img.style.left=20 + 'px';
this.showImg();
},1500)
}
}).catch((res) =>{
this.Verificationcode=2;
setTimeout(() =>{
this.Verificationcode=1;
this.$refs.small_img.style.left=20 + 'px';
this.showImg();
},1320)
})
};
}
}
}
</script>
<style>
.demotwo {
width: 100%;
height: 100%;
position:relative;
}
.img_bg{
width:360px;
height:200px;
position: absolute;
top:0;
left:0;
right:0;
bottom:0;
margin: auto;
background:#ccc;
border:1px solid seagreen;
}
</style>