slider-verify

422 阅读1分钟
<template>
	<view class="slider-verify-box" v-if="isShow">
		<view class="verifyBox">
			<view class="slider-title">{{$i18nMsg.base.sliderVerifyMoudule.graphicVerification}}</view>
			<view class="slide-content">
				<view class="slide-tips">{{$i18nMsg.base.sliderVerifyMoudule.dragTheSlider_down}}</view>
				<view class="slider-pintu">
					<image id="pintuImg" :src="'/static/base-module/slider-verify/' + img + '.jpg'" class="pintu"></image>

					<view class="pintukuai" :style="{ top: top + 'px', left: oldx + 'px' }">
						<image :src="'/static/base-module/slider-verify/' + img + '.jpg'" :style="{ top: '-' + top + 'px', left: '-' + left + 'px'}"></image>
					</view>

					<view class="yinying" :style="{ top: top + 'px', left: left + 'px' }"></view>
				</view>

				<view class="slider-movearea" @touchend="endTouchMove">
					<movable-area :animation="true"><movable-view :x="x" direction="horizontal" @change="startMove"></movable-view></movable-area>

					<view class="huadao">{{$i18nMsg.base.sliderVerifyMoudule.dragTheSlider_left}}</view>
				</view>
			</view>
			
			<view class="slider-btn-group">
				<view class="slider-btn" @tap="closeSlider">{{$i18nMsg.base.sliderVerifyMoudule.close}}</view>
				<view class="slider-btn slide-btn-refresh" @tap="refreshVerify">{{$i18nMsg.base.sliderVerifyMoudule.refresh}}</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	name: 'slider-verify',
	props: {
		isShow: true
	},
	data() {
		return {
			x: 0, //初始距离
			oldx: 0, //移动的距离
			img: '1', //显示哪张图片
			left: 0, //随机拼图的最终X轴距离
			top: 0, //拼图的top距离
		};
	},
	watch: {
		// 每次打开重新刷新拼图
		isShow(newValue, oldValue) {
			if(newValue){
				this.refreshVerify();	//刷新
			}
		}
	},
	mounted() {
		var that = this;
		that.refreshVerify();
	},
	methods: {
		//刷新验证
		refreshVerify() {
			var gl = Math.random().toFixed(2);
			this.left = 50+Math.round(500*gl); //生成随机X轴最终距离
            this.top = 30; //生成随机Y轴初始距离
            console.log("this.left"+this.left)
             console.log("this.left"+this.top)
			
			if (gl <= 0.2) {
				this.img = 1;
			}
			if (gl > 0.2 && gl <= 0.4) {
				this.img = 2;
			}
			if (gl > 0.4 && gl <= 0.6) {
				this.img = 3;
			}
			if (gl > 0.6 && gl <= 0.8) {
				this.img = 4;
			}
			if (gl > 0.8 && gl <= 1) {
				this.img = 5;
			}
			
			this.resetMove();	//重置阴影位置
		},

		/* 滑动中 */
		startMove(e) {
			this.oldx = e.detail.x;
		},

		/* 滑动结束 */
		endTouchMove() {
			var that = this;

			if (Math.abs(that.oldx - that.left) <= 5) {
				this.$refs.uToast.show({
					title: this.$i18nMsg.base.sliderVerifyMoudule.verifiedSuccessfully,
					duration: 2500,
					icon:true,
					type:"success",
					callback() {
						that.$emit('touchSliderResult', true);
					}
				});
			} else {
				that.refreshVerify();
			}
		},
		
		/* 重置阴影位置 */
		resetMove() {
			this.x = 1;
			this.oldx = 1;
			setTimeout(() => {
				this.x = 0;
				this.oldx = 0;
			}, 300);
		},
		
		// 关闭
		closeSlider(){
			this.$emit('touchSliderResult', false);
		}	
	}
};
</script>

<style lang="scss">
.slider-verify-box {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 999;
}
.verifyBox {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	// width: 85%;
	background-color: #fff;
	border-radius: 20rpx;
	

	.slider-title {
		font-size: 20rpx;
		text-align: center;
		padding: 10rpx 0;
		color: rgba(2, 20, 33, 0.85);
		border-bottom: 1px solid rgba(2, 20, 33, 0.15);
	}

	.slide-content {
		width: 560rpx;
		padding: 0 1em;
		margin: 0 auto;
		.slide-tips {
		   font-size: 20rpx;
			color: rgba(2, 20, 33, 0.45);
			padding: 10rpx 0;
		}

		.slider-pintu {
			position: relative;
			width: 100%;
			border-radius: 10rpx;
			overflow: hidden;
			.pintu {
				width: 560rpx;
				height: rpx(800);
				display: block;
				margin: 0 auto;
			}

			.pintukuai {
				position: absolute;
				top: 0;
				left: 0;
				width: rpx(200);
				height:  rpx(200);
				z-index: 100;
				overflow: hidden;

				image {
					display: block;
					position: absolute;
					top: 0;
					left: 0;
					width: 560rpx;
					height: 315rpx;
				}
			}
		}

		.yinying {
			position: absolute;
			width: rpx(200);
			height: rpx(200);
			background-color: rgba(0, 0, 0, 0.5);
		}
	}
}

.slider-movearea {
	position: relative;
	height:  rpx(200);
	width: 100%;
	margin: 10rpx auto;

	movable-area {
		height:  rpx(200);
		width: 100%;

		movable-view {
			width: 80rpx;
			height:  rpx(240);
			border-radius: 50%;
			background-color: #007cff;
			background-image: url(/static/base-module/slider-verify/icon-button-normal.png);
			background-repeat: no-repeat;
			background-size: auto 30rpx;
			background-position: center;
			position: relative;
			z-index: 100;
		}
	}
}

.huadao {
	width: 100%;
	height:  rpx(200);
	line-height: 66rpx;
	background: #eee;
	box-shadow: inset 0 0 5rpx #ccc;
	border-radius: 40rpx;
	color: #999;
	text-align: center;
	box-sizing: border-box;
	position: absolute;
	top: 7rpx;
	left: 0;
	font-size: 20rpx;
	z-index: 99;
}

.slider-btn-group{
	width: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
	margin-top: 30rpx;
	border-top: 1px solid rgba(2, 20, 33, 0.15);
	
	.slider-btn{
		flex: 1;
		height: rpx(200);
		line-height:  rpx(200);
		text-align: center;
		font-size: 20rpx;
		color:rgba(2,20,33,0.85);
		&:active{
			opacity: 0.8;
		}
	}
	.slide-btn-refresh{
		color:rgba(14,107,176,1);
		border-left: 1px solid rgba(2, 20, 33, 0.15);
	}	
}
</style>