首页 > 其他分享 >前端Vue自定义发送短信验证码弹框popup 实现剩余秒数计数 重发短信验证码

前端Vue自定义发送短信验证码弹框popup 实现剩余秒数计数 重发短信验证码

时间:2023-06-27 09:37:28浏览次数:49  
标签:function code 秒数 自定义 val callResult 验证码 item type

前端Vue自定义发送短信验证码弹框popup 实现剩余秒数计数 重发短信验证码, 请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=13207

效果图如下:

实现代码如下:

cc-codeDialog

使用方法


<!-- show:是否显示弹框 phone:手机号  autoCountdown:自动时间秒数 len:短信验证码长度 @closeClick:关闭弹框 @confirmClick:确认事件 -->

<cc-codeDialog :show="show" phone="1900000000" :autoCountdown="true" :len="6" @closeClick="closeCodeDialog" @confirmClick="confirmClick"></cc-codeDialog>

HTML代码实现部分


<template>

<view class="content">

<button @click="showCodeDialog" style="margin-top: 39px;">发送短信验证码 </button>

<!-- show:是否显示弹框 phone:手机号  autoCountdown:自动时间秒数 len:短信验证码长度 @closeClick:关闭弹框 @confirmClick:确认弹框 -->

<cc-codeDialog :show="show" phone="1900000000" :autoCountdown="true" :len="6" @closeClick="closeCodeDialog"

@confirmClick="confirmClick"></cc-codeDialog>

</view>

</template>

<script>

export default {

data() {

return {

show: false

}

},

methods: {

showCodeDialog(item) {

this.show = true;

},

closeCodeDialog(item) {

this.show = false;

},

confirmClick(result) {

console.log("result = " + JSON.stringify(result));

this.show = false;

}

}

}

</script>

<style>

.content {

display: flex;

flex-direction: column;

background-color: aliceblue;

height: 100vh;

}

</style>

组件实现代码


<template>
	<view v-if="show" class="codedialog">
		<view class="mask"></view>
		<view class="dialog-view">
			<text class="dialog-close" @click="closeDialog()"></text>
			<view class="dialog-hd">
				<view class="codedialog-maintitle">
					<text>发送验证码</text>
				</view>
				<view v-if="phone!='' && phone !=null " class="codedialog-subtitle">
					<text>已发送到手机号:{{phoneStr}}</text>
				</view>
			</view>
			<view class="dialog-bd">
				<view class="code-view">
					<view v-for="(code,index) of codeAry" :key="index" class="code-item">{{code.val}}</view>
				</view>
			</view>
			<view class="dialog-ft">
				<view v-if="countdown==60" @click="resend" class="resend">重新发送</view>
				<view v-if="countdown<60" class="countdown">{{countdown}}s</view>
			</view>

			<button style="margin-top: 20px; width: 88%;background-color: royalblue;color: white;"
				@click="confirmClick">确定</button>
		</view>

		<view class="keyboard">
			<view class="keyboard-line">
				<view data-val="1" @click="bindKeyEvent" class="button-item">1</view>
				<view data-val="2" @click="bindKeyEvent" class="button-item">2</view>
				<view data-val="3" @click="bindKeyEvent" class="button-item">3</view>
			</view>
			<view class="keyboard-line">
				<view data-val="4" @click="bindKeyEvent" class="button-item">4</view>
				<view data-val="5" @click="bindKeyEvent" class="button-item">5</view>
				<view data-val="6" @click="bindKeyEvent" class="button-item">6</view>
			</view>
			<view class="keyboard-line">
				<view data-val="7" @click="bindKeyEvent" class="button-item">7</view>
				<view data-val="8" @click="bindKeyEvent" class="button-item">8</view>
				<view data-val="9" @click="bindKeyEvent" class="button-item">9</view>
			</view>
			<view class="keyboard-line">
				<view data-val="clear" @click="bindKeyEvent" class="button-item">清空</view>
				<view data-val="0" @click="bindKeyEvent" class="button-item">0</view>
				<view data-val="delete" @click="bindKeyEvent" class="button-item">x</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		props: {
			show: {
				type: Boolean,
				default: false
			},
			autoCountdown: {
				type: Boolean,
				default: true
			},
			phone: {
				type: String,
				default: ""
			},
			len: {
				type: Number,
				default: 6
			}
		},
		data() {
			return {
				codeAry: [{
						"val": ""
					}, {
						"val": ""
					}, {
						"val": ""
					}, {
						"val": ""
					},
					{
						"val": ""
					},
					{
						"val": ""
					}
				],
				currItem: 0,
				countdown: 60,
				cTimer: null,
				callResult: {
					type: 0,
					code: ''
				},
				suspend: false
			};
		},
		computed: {
			phoneStr() {
				return this.phone.substr(0, 3) + "****" + this.phone.substr(7);
			}
		},
		watch: {
			show: function() {
				console.log(this.show)
				if (this.show) {
					if (!this.suspend) {
						this.init();
					}
				} else {
					if (!this.suspend) {
						this.clearTimer();
					}
					this.clearCode();
				}
			}
		},
		methods: {
			init: function() {
				var codeAry = [];
				for (var i = 0; i < this.len; i++) {
					codeAry.push({
						val: ""
					})
				}
				this.codeAry = codeAry;
				this.currItem = 0;
				if (this.autoCountdown) {
					this.startTimer();
				}
			},
			bindKeyEvent: function(e) {
				var _this = this;
				var val = e.currentTarget.dataset.val;
				switch (val) {
					case "clear":
						_this.clearCode();
						break;
					case "delete":
						_this.deleteCode();
						break;
					default:
						_this.inputVal(val);
						break;
				}
			},
			inputVal: function(val) {
				if (this.currItem < this.len) {
					this.codeAry[this.currItem].val = val;
					this.currItem++;
				}
				if (this.currItem == this.len) {
					this.execuCall(1);
				}
			},
			clearCode: function() {

				this.init();
			},
			deleteCode: function() {
				if (this.currItem > 0) {
					this.codeAry[this.currItem - 1].val = "";
					this.currItem--;
				}
			},
			closeDialog: function() {
				this.execuCall(-1);
				this.$emit("closeClick");
			},
			startTimer: function() {
				var _this = this;
				if (_this.cTimer == null) {
					_this.cTimer = setInterval(function() {
						_this.countdown--;
						if (_this.countdown == 0) {
							_this.clearTimer();
						}
					}, 1000)
				}
			},
			clearTimer: function() {
				var _this = this;
				clearInterval(_this.cTimer);
				_this.cTimer = null;
				_this.countdown = 60;
			},
			getCodeValue: function() {
				var codeStr = "";
				this.codeAry.forEach(function(code) {
					codeStr += code.val;
				})
				return codeStr;
			},
			execuCall: function(type) {
				this.callResult.type = type;
				if (type == 1) {
					this.callResult.code = this.getCodeValue();
					this.clearTimer();

				} else {
					this.suspend = true;
					this.callResult.code = '';
				}
				this.$emit("change", this.callResult);
			},
			resend: function() {
				var _this = this;
				_this.callResult.code = '';
				_this.callResult.type = 0;
				// _this.callResult.resendCall = function() {

				// }
				_this.init();
				_this.$emit("change", _this.callResult);

			},

			confirmClick() {


				console.log("result = " + JSON.stringify(this.callResult));

				if (this.callResult.code.length < 6) {

					uni.showModal({
						title: '温馨提示',
						content: '输入短信验证码长度有误'
					})
				} else {
					this.$emit("confirmClick", this.callResult);

				}

			}


		}
	}
</script>

<style scoped>
	.button-item:active {
		background: #d4d4d4;
	}

	.button-item+.button-item {
		border-left: 0.1px solid #d4d4d4;
	}

	.button-item {
		flex: 1;
		padding: 14px 0px;
	}

	.keyboard-line+.keyboard-line {
		border-top: 0.1px solid #d4d4d4;
	}

	.keyboard-line {
		display: flex;
	}

	.keyboard {
		background: #fff;
		position: absolute;
		z-index: 999;
		width: 100%;
		left: 0;
		bottom: 0;
		font-size: 17px;
	}

	.dialog-close {
		color: #999;
		height: 28px;
		width: 28px;
		font-size: 19px;
		top: 5px;
		left: 5px;
		position: absolute;
	}


	.dialog-close:before {
		content: "\2716";
	}

	.countdown {
		color: #666;
		font-size: 16px;
	}

	.resend {
		color: #007aff;
		font-size: 16px;
	}

	.dialog-ft {
		margin-top: 10px;
	}

	.code-view {
		display: flex;
		text-align: center;
		margin: 0 auto;
		border-collapse: separate;
		border-spacing: 10px 5px;
	}

	.code-item+.code-item {
		margin-left: 5px;
	}

	.code-item {
		flex: 1;
		border-bottom: 1px solid #999;
		padding-bottom: 2px;
		height: 60upx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30upx;
	}

	.dialog-bd {
		margin-top: 5px;
	}

	.codedialog-subtitle {
		margin-top: 5px;
		padding: 5px 0px;
		font-size: 15px;
		line-height: 1.4;
		word-wrap: break-word;
		word-break: break-all;
		color: #999;
	}

	.dialog-view {
		position: fixed;
		z-index: 999;
		width: 70%;
		max-width: 300px;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -120%);
		background-color: #fff;
		text-align: center;
		border-radius: 3px;
		overflow: hidden;
		padding: 20px 10px;
	}

	.mask {
		position: fixed;
		z-index: 999;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		background: rgba(0, 0, 0, .6);
	}

	.codedialog {
		z-index: 999;
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		box-sizing: border-box;
		text-align: center;
	}
</style>



标签:function,code,秒数,自定义,val,callResult,验证码,item,type
From: https://www.cnblogs.com/ccVue/p/17507778.html

相关文章

  • 前端Vue自定义验证码密码登录切换tabs选项卡标签栏标题栏 验证码登录模版 密码登录模
    前端Vue自定义验证码密码登录切换tabs选项卡标签栏标题栏验证码登录模版密码登录模版,请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=13221效果图如下:实现代码如下:cc-selectBox使用方法<!--select-arr:选择数组nowindex:当前选择序列@change:切换选择......
  • pytest + yaml 框架 -43.支持自定义函数提取返回结果
    前言在yaml用例中提取返回结果,可以支持以下三种表达式jmespath取值语法:body.keyname.keynamejsonpath语法:$..keynamere正则语法以上三种表达式可以满足99%的测试场景需求了,但是有些特殊的需求通过表达式无法取到,为了满足另外1%的需求,可以自定义函数取值。此功......
  • SAP BTP 平台 ABAP 编程环境如何维护自定义 Unit Of Measure 数据库表 T006
    在ABAPOn-Premises系统下,我们可以轻易在SAPGUI里对UnitofMeasure数据库表即T006进行维护:[图片]ABAP数据库表T006是SAP系统中的一个重要数据表,它用于存储与度量单位相关的信息。在SAP系统中,度量单位用于表示各种物料、服务和计量单位。这些信息对于物料管理、......
  • spring boot 编译打包时将自定义引入的.jar包依赖,全部打包进去
    发现自己引入的.jar包,在打包时,.jar包并不会打进去,导致报错。打包时打入自定义.jar包方法:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</a......
  • 前端Vue自定义精美tabs,可设置下划线图标 热门标题
    前端Vue自定义精美tabs,可设置下划线图标热门标题,下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=13186效果图如下:cc-beautyTabs使用方法<!--tabchange:tab选择事件tabList:tab数据--><cc-beautyTabs@tabChange="tabChange":tabList="t......
  • 前端Vue自定义滚动卡片,可以用于商品海报生成
    前端Vue自定义滚动卡片,可以用于商品海报生成,下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=13189实现代码如下:cc-scroolCard使用方法<!--dataInfo:滚动卡片数据 swiperIndex:滚动序列@change:滚动事件--><cc-scroolCard:dataInfo="data":......
  • 07前后端项目上传gitee,后端多方式登录接口,发送短信功能,发送短信封装,短信验证码接口,短
    1前后端项目上传到gitee#公司里: -前端一个仓库---》一个团队-后端一个仓库---》一个团队-微服务:两三个人一个服务---》一个项目一个仓库-网上开源软件,前后端都在一起#在远端建立前端仓库#本地代码提交到远成仓库2后端多方式......
  • springboot中自定义JavaBean返回的json对象属性名称大写变小写问题
    目录一、继承类二、手动添加Get方法三、@JsonProperty四、spring-bootjson(jackson)属性命名策略开发过程中发现查询返回的数据出现自定义的JavaBean的属性值大小写格式出现问题,导致前端无法接受到数据,目前有四种解决方法,根据大佬的经验之谈,前两种是最简单便捷的,后两种是比较通......
  • 异步注解@Async使用自定义线程池
    1.@Async注解@Async是java中一个注解,其作用就是加上该注解的类或方法能够异步执行任务,该注解添加到方法上时,表示该方法是异步方法,添加到类上时,表示该类中的所有方法都是异步方法。该注解的代码为:可以看出其是作用在类和方法上,能够在运行时被获取到。当在使用@Async时,如果不指......
  • 滑块验证码缺口识别及轨迹生成
    importbase64importrandomimporttimefromioimportBytesIOimportcv2importrequeststry:importmatplotlib.pyplotaspltexcept:plt=NoneimportnumpyasnpfromPILimportImagefromselenium.webdriverimportActionChainsfromscipyim......