uniapp,uview发送验证码效果

<template>
	<view>
		<view :class="status == 1? 'before-style': 'after-style'" @tap="getVerifyCode" >{
   
   {tips}}</view>
		<u-toast ref="uToast"></u-toast>
		<u-verification-code start-text="发送验证码" change-text="Xs" :seconds="seconds" @end="end" @start="start" ref="uCode" 
		@change="codeChange"></u-verification-code>
	</view>
</template>

<script>
export default {
		data() {
			return {
				tips: '',
				seconds: 10,
				status: 1
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getVerifyCode() {
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				this.status = 1
				// this.$u.toast('倒计时结束');
			},
			start() {
				this.status = 2
				// this.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style>
	.before-style {
		background: #007AFF;
		width: 100%;
		color: #fff;
		text-align: center;
		border: none;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
		border-radius: 10rpx;
	}

	.after-style {
		background:#C0C4CC;
		width: 100%;
		color: #fff;
		text-align: center;
		border: none;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
		border-radius: 10rpx;
	}
</style>

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/114633140