uniapp中注册手机号短信验证码

这是一个使用Vue.js编写的前端页面,包含两个步骤:输入手机号并进行正则验证,然后获取及输入验证码。当手机号验证通过后,向服务器发送请求获取验证码。在验证码页面,用户输入接收到的验证码,再次进行验证。如果验证码正确,将数据提交至服务器完成注册过程。
摘要由CSDN通过智能技术生成

 一、效果图

 二、输入手机号页面

<template>
    <view>
        <view class='login-tel'>
			<view class='tel-main'>
				<view class='login-from'>
					<view class='login-user'>
						<text class='user-text'>手机号</text>
						<input type="number" focus='true' v-model="userTel" value="" placeholder="请输入11位手机号"/>
					</view>
				</view>
				<view class='tel' @tap='goNextCode'>下一步</view>
			</view>
		</view>
    </view>
</template>

<script>
	import $http from '@/common/api/request.js'
    export default {
        data() {
			return {
				userTel:'',
				//验证的规格
				rules:{
					userTel:{
						rule:/^1[3456789]\d{9}$/,
						msg:"请输入11位手机号"
					}
				}
			}
		},
        methods: {
            //判断验证是否符合要求
			validate(key){
				let bool = true;
				if(  !this.rules[key].rule.test(this[key]) ){
					uni.showToast({
						title:this.rules[key].msg,
						icon:"none"
					})
					bool=false;
					return false;
				}
				return bool;
			},
            //下一步按钮
            goNextCode() {
                 if(  !this.validate('userTel')  ) return; 
                 $http.request({
					url:"/registered",
					method:"POST",
				 	data:{
				 		phone:this.userTel
				 	}
				 }).then((res)=>{
				 	if(!res.success){
				 		uni.showToast({
				 			title:res.msg,
				 			icon:"none"
						})
						return ;
				 	}else{
				 		uni.navigateTo({
				 			url:"../login-code/login-code?phone="+this.userTel+""
						})
				 	}
					
				 }).catch(()=>{
				 	uni.showToast({
						title:'请求失败',
						icon:'none'
				 	})
			     })
            }
        }
    }
</script>
<style scoped>
.login-tel{
	width: 100vw;
	height: 100vh;
}
.tel-main{
	padding:0 20rpx;
}
.login-from{
	padding:30rpx 0;
}
.login-user{
	font-size:32rpx;
	padding:10rpx 0;
	display: flex;
	align-items: center;
	border-bottom:2rpx solid #f7f7f7;
}
.user-text{
	padding-right:10rpx;
}
.tel{
	width:100%;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	color:#FFFFFF;
	background-color: #49BDFB;
	border-radius: 40rpx;
}
</style>

三、验证码页面

<template>
	<view>
		<view class='login-tel'>
			<view class='tel-main'>
				<view class='login-from'>
					<view class='login-user'>
						<text class='user-text'>验证码</text>
						<input type="text" placeholder="请输入验证码" v-model="userCode"/>
						<button class="btn" plain='true' size='mini' :disabled="disabled" @tap='sendCode'> {{codeMsg}} </button>
					</view>
				</view>
				<view class='tel' @tap='goNextIndex'>下一步</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $http from '@/common/api/request.js'
	export default {
		data() {
			return {
				//倒计时到时间
				codeNum:60,
				//显示到文本
				codeMsg:"",
				//按钮是否禁用
				disabled:false,
				//用户输入的内容
				userCode:'',
				//手机号
				phone:'',
				//得到的验证码
				getCode:""
			}
		},
		onReady() {
			this.codeMsg = '重新发送('+this.codeNum+')';
			this.sendCode();
		},
		onLoad(e) {
			this.phone = e.phone; 
		},
		methods: {
			...mapMutations(['login']),
			//点击验证码发送
			sendCode(){
				//请求接口返回验证码
				 $http.request({
				 	url:"/code",
				 	method:"POST",
				 	data:{
				 		userName:this.phone
				 	}
				 }).then((res)=>{
				 	this.getCode = res.code;
				 }).catch(()=>{
				 	uni.showToast({
						title:'请求失败',
						icon:'none'
				 	})
				 })
				this.disabled = true;
				let timer = setInterval(()=>{
					--this.codeNum;
					this.codeMsg = '重新发送('+this.codeNum+')';
				},1000);
				setTimeout(()=>{
					clearInterval(timer);
					this.codeNum=60;
					this.disabled = false;
					this.codeMsg = '重新发送';
				},60000)
			},
			//点击下一步
			goNextIndex(){
				if(  this.getCode == this.userCode ){
					//请求接口==> 往数据库增加一条数据
					 $http.request({
					 	url:"/addUser",
					 	method:"POST",
					 	data:{
					 		userName:this.phone,
					 		code:this.userCode
					 	}
					 }).then((res)=>{
					 	//注册成功
					 	if( res.success ){
					 		uni.showToast({
								title:res.msg,
					 			icon:"none"
					 		})
							
							uni.redirectTo({
					 			url:"../index/index"
					 		})
					 	}
					 }).catch(()=>{
						uni.showToast({
							title:'请求失败',
							icon:'none'
						})
					})
				}else{
					uni.showToast({
						title:"验证码错误",
						icon:"none"
					})
				}
			}
		}
	}
</script>

<style scoped>
.login-tel{
	width: 100vw;
	height: 100vh;
}
.tel-main{
	padding:0 20rpx;
}
.login-from{
	padding:30rpx 0;
}
.login-user{
	font-size:32rpx;
	padding:10rpx 0;
	display: flex;
	align-items: center;
	border-bottom:2rpx solid #f7f7f7;
}
.user-text{
	padding-right:10rpx;
	padding-bottom: 3rpx;
}
.tel{
	width:100%;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	color:#FFFFFF;
	background-color: #49BDFB;
	border-radius: 40rpx;
}
.btn{
	font-size: 24rpx;
}
</style>

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Uniapp获取短信验证码手机号校验,你可以使用第三方插件来简化开发过程。以下是一个常用的Uniapp插件示例:uni-smscode。 uni-smscode插件提供了获取短信验证码手机号校验的功能,使用该插件可以方便地实现短信验证码的发送和验证。以下是使用uni-smscode插件的步骤: 1. 在Uniapp项目,通过npm或yarn安装uni-smscode插件: ``` npm install uni-smscode ``` 2. 在需要使用短信验证码的页面,引入uni-smscode插件: ```vue <template> <view> <!-- 手机号输入框 --> <input type="text" v-model="mobile" placeholder="请输入手机号码" /> <!-- 验证码输入框 --> <input type="text" v-model="code" placeholder="请输入验证码" /> <!-- 获取验证码按钮 --> <button @click="sendCode">获取验证码</button> <!-- 校验手机号和验证码按钮 --> <button @click="verifyCode">校验</button> </view> </template> <script> import smsCode from 'uni-smscode' export default { data() { return { mobile: '', code: '' } }, methods: { sendCode() { smsCode.send(this.mobile).then(res => { // 短信验证码发送成功处理逻辑 console.log('短信验证码发送成功') }).catch(err => { // 短信验证码发送失败处理逻辑 console.error('短信验证码发送失败', err) }) }, verifyCode() { smsCode.verify(this.mobile, this.code).then(res => { // 手机号和验证码校验成功处理逻辑 console.log('手机号和验证码校验成功') }).catch(err => { // 手机号和验证码校验失败处理逻辑 console.error('手机号和验证码校验失败', err) }) } } } </script> ``` 在上述示例,通过调用smsCode.send方法来发送短信验证码,并使用smsCode.verify方法来校验手机号和验证码。你可以根据需求自定义按钮的样式和交互逻辑。 需要注意的是,具体插件的使用方法可能会因插件版本和更新而有所变化,建议查阅相关插件的文档或示例代码来进行具体的使用和定制。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值