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

这篇具有很好参考价值的文章主要介绍了前端Vue自定义发送短信验证码弹框popup 实现剩余秒数计数 重发短信验证码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

阅读全文下载完整代码请关注微信公众号: 前端组件开发

效果图如下:

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

实现代码如下:文章来源地址https://www.toymoban.com/news/detail-501880.html

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>



到了这里,关于前端Vue自定义发送短信验证码弹框popup 实现剩余秒数计数 重发短信验证码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot——短信发送、手机验证码登录

    目录 一、短信发送 1.1 阿里云短信服务 1.1.1 设置短信签名 1.1.2 模板管理 1.1.3 设置AccessKey 1.2 短信发送——代码开发 1.2.1 导入maven坐标 1.2.2 调用API 1.2  手机验证码登录 1.2.1 用户数据库表 1.2.2  修改过滤器 1.2.3   随机生成验证码的工具类 1.2.4 手机验证码登录-- 发送验证码

    2023年04月22日
    浏览(32)
  • (短信服务)java SpringBoot 阿里云短信功能实现发送手机验证码

    阿里云官网: https://www.aliyun.com/ 点击官网首页注册按钮。 注册成功后,点击登录按钮进行登录。登录后进入短信服务管理页面,选择国内消息菜单: 短信签名是短信发送者的署名,表示发送方的身份。 切换到【模板管理】标签页: 短信模板包含短信发送内容、场景、变量信息

    2024年02月02日
    浏览(41)
  • 【Springboot】| 阿里云发送短信验证码,你会了吗?

    专栏 名字 🔥Elasticsearch专栏 es 🔥spring专栏 spring开发 redis专栏 redis学习笔记 🔥项目专栏 项目集锦 修bug专栏 bug修理厂 狮子之前发了一篇《邮箱发送验证码,你会了吗?》,很快上了热度榜单,但是那篇文章只是简单介绍了如何接收验证码的流程以及安利了一个接收验证码的

    2024年02月08日
    浏览(31)
  • 引入短信服务发送手机验证码进行安全校验

    其他方案=引入QQ邮箱发送验证码进行安全校验 相对短信验证码,操作更简单而且免费 最近想给自己的项目在注册时加点安全校验,准备使用免费的邮箱验证来着,在上一篇引入QQ邮箱进行安全校验时,看有朋友说阿里云会送一些短信服务免费额度,于是去官网一看,果然送了

    2024年02月04日
    浏览(45)
  • 手机短信验证码登录功能的开发实录(机器识别码、短信限流、错误提示、发送验证码倒计时60秒)

    短信验证码是通过发送验证码到手机的一种有效的验证码系统,作为比较准确和安全地保证购物的安全性,验证用户的正确性的一种手段,几乎网站登录都会使用该方式。 其特点是依据某些验证码接入商提供手机短信验证码服务,各网站通过接口发送请求到接入商的服务器,

    2024年02月02日
    浏览(34)
  • 如何通过腾讯云短信实现发送验证码并校验验证码以实现登录功能

    验证码相关的10种技术 图像处理技术:生成、识别、验证验证码的图像。 机器学习技术:让计算机自动学习并识别验证码。 文字识别技术:将图像中的文字转换成计算机可读的文本。 模式识别技术:识别验证码中的模式及规律。 图像噪声处理技术:去除图像中的噪声干扰。

    2024年02月10日
    浏览(37)
  • 腾讯云短信服务实现 Java 发送手机验证码(SpringBoot+Redis 实现)

    前置:需要腾讯云的账号,后期授权需要,不需要买云服务器,有需要的可以购买短信套餐(几块钱) 搜索框输入短信,可以买一个短信套餐包,便宜不贵,进入短信服务的控制台 发送短信有频率限制,企业用户可以修改设置 之后我们需要对短信内容进行设置      类型有网站

    2024年02月09日
    浏览(37)
  • 【账号系统之(手机验证码登录)】使用阿里云短信服务,实现服务商给手机发送验证码功能。

    目录 一、前言 二、前期准备 三、购买短信服务 四、申请签名和模板 (1) 进入阿里云短信服务控制台 (2) 添加签名 (3) 添加模板 五、RAM申请及权限配置 (1) 进入RAM访问控制界面 (2) 创建用户 (3) 分配权限 (4) 创建角色 (5) 记录段的值 六、阿里云.NET SDK身份验证接入 (1) 了解

    2024年02月04日
    浏览(42)
  • 项目7:(aliyun)实现短信的发送和验证微服务和上传文件删除文件微服务

    ①gulimall-common和service-base放什么? gulimall-common写全局用的工具包 全局异常处理 全局返回值 工具包(生成随机数,校验手机号) service-base写服务的配置 redis配置类序列化的方式 swagger文档生成分组 ②生成四位或六位随机数 ③校验手机号码正确 ④补充错误代码-501阿里云响应失

    2023年04月19日
    浏览(32)
  • JavaWeb_瑞吉外卖_业务开发Day5-套餐管理, 短信发送, 手机验证码登录

    总结 接收List数据时, 需要加上 @RequestParam 注解 操作2个及2个以上表, 加上 @Transactional 事务注解, 保持数据的一致性. 发送短信 生成验证码 黑马程序员. 瑞吉外卖项目

    2024年02月12日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包