uniapp实现手机验证码登录

266 阅读1分钟

引言

在微信小程序开发过程中,通常有两种常见的登录方式。一种微信授权登录, 一种手机验证码登录。
本文记录一下手机验证码登录的页面布局。 以便后期使用。

代码

<template>
	<view class="container">
		<view class="tui-page-title">欢迎登录xx</view>
		<view class="tui-form">
			<view class="tui-view-input">
				<tui-list-cell :hover="false" :lineLeft="0" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="mobile" color="#6d7a87" :size="20"></tui-icon>
					
						<input
							:adjust-position="false"
							:value="mobile"
							placeholder="请输入手机号"
							placeholder-class="tui-phcolor"
							type="number"
							maxlength="11"
							@input="inputMobile"
						/>
						<view class="tui-icon-close" v-show="mobile" @tap="clearInput(1)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="0" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="pwd" color="#6d7a87" :size="20"></tui-icon>
						<input
							:adjust-position="false"
							:value="password"
							placeholder="请输入验证码"
							placeholder-class="tui-phcolor"
							type="text"
							maxlength="36"
							@input="inputPwd"
						/>
						
						<view class="tui-icon-close" >
							<tui-countdown-verify :successVal="successVal" @send="send" :resetVal="resetVal" ></tui-countdown-verify>
						</view>
					</view>
				</tui-list-cell>
			</view>
			<view class="tui-cell-text" style="padding-left: 40rpx;">
				我们将保护您的隐私安全
			</view>
			<view class="tui-btn-box">
				<tui-button :disabledGray="true" :disabled="disabled" :shadow="true" shape="circle" @click="handleLogin()">登录</tui-button>
			</view>
			
		</view>
	</view>
</template>

<script>
export default {
	computed: {
		disabled: function() {
			let bool = true;
			if (this.mobile && this.password) {
				bool = false;
			}
			return bool;
		}
	},
	data() {
		return {
			mobile: '',
			password: '',
			popupShow: false,
			successVal: 0,
			resetVal: 0
			
		};
	},
	onLoad(options) {
	},
	methods: {
		handleLogin(){
			var parms = {
				mobileNumber: this.mobile,
				code: this.password
			} 
			this.http.post(this.$url.login.loginWithSmsCode, {data: parms, showLoading: true}).then(res => {
				console.log(res);
				if(res.code === 200){
					//处理登录信息保存
					this.store.commit('login', res.data);
					uni.switchTab({
						url:'/pages/me/index'
					})
				}else{
					//抛出异常信息
					this.tui.toast(res.message);
				}
			}).catch(e => {
				console.log(e)
			})
		},
		send(e) {
			if(this.mobile === undefined || this.mobile === ""){
				this.tui.toast('手机号不能为空');
				this.resetVal = this.resetVal + 1;
				return;
			}
			this.sendSms();
		},
		sendSms(){
		   var parms = {
			  mobileNumber: this.mobile
		   } 	
		   this.http.post(this.$url.login.getSmsCode, {data: parms, showLoading: false}).then(res => {
				if(res.code != 200){
					this.resetVal = this.resetVal + 1;
					this.tui.toast(res.message);
				}else{
					this.tui.toast('短信发送成功');
					setTimeout(() => {this.successVal++;}, 1000);
				}
			
		   }).catch(e => {
				console.log(e)
		   })			
		},
		back() {
			uni.navigateBack();
		},
		inputMobile: function(e) {
			this.mobile = e.detail.value;
		},
		inputPwd: function(e) {
			this.password = e.detail.value;
		},
		clearInput(type) {
			if (type == 1) {
				this.mobile = '';
			} else {
				this.password = '';
			}
		},
		href(type) {
			let url = '../forgetPwd/forgetPwd';
			if (type == 2) {
				url = '../reg/reg';
			}
			this.tui.href(url);
		},
		showOtherLogin() {
			//打开后 不再关闭
			this.popupShow = true;
		},
		newWxLogin(){
		    this.login.auth();
		}
	}
};
</script>

<style lang="scss">
.container {
	// background-color: #ffffff;
	// height: 100vh;
	.tui-status-bar {
		width: 100%;
		height: var(--status-bar-height);
	}

	.tui-header {
		width: 100%;
		padding: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
	}

	.tui-page-title {
		width: 100%;
		font-size: 48rpx;
		font-weight: bold;
		color: $uni-text-color;
		line-height: 42rpx;
		padding: 40rpx;
		box-sizing: border-box;
	}

	.tui-form {
		padding-top: 50rpx;

		.tui-view-input {
			width: 100%;
			box-sizing: border-box;
			padding: 0 40rpx;

			.tui-cell-input {
				width: 100%;
				display: flex;
				align-items: center;
				padding-top: 48rpx;
				padding-bottom: $uni-spacing-col-base;

				input {
					flex: 1;
					padding-left: $uni-spacing-row-base;
				}

				.tui-icon-close {
					margin-left: auto;
				}
			}
		}

		.tui-cell-text {
			width: 100%;
			padding: $uni-spacing-col-lg $uni-spacing-row-lg;
			box-sizing: border-box;
			font-size: $uni-font-size-sm;
			color: $uni-text-color-grey;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.tui-color-primary {
				color: $uni-color-primary;
			}
		}

		.tui-btn-box {
			width: 100%;
			padding: 0 $uni-spacing-row-lg;
			box-sizing: border-box;
			margin-top: 80rpx;
		}
	}

	.tui-login-way {
		width: 100%;
		font-size: 26rpx;
		color: $uni-color-primary;
		display: flex;
		justify-content: center;
		position: fixed;
		left: 0;
		bottom: 80rpx;

		view {
			padding: 12rpx 0;
		}
	}

	.tui-auth-login {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 80rpx;
		padding-top: 20rpx;

		.tui-icon-platform {
			width: 90rpx;
			height: 90rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			margin-left: 40rpx;

			&::after {
				content: '';
				position: absolute;
				width: 200%;
				height: 200%;
				transform-origin: 0 0;
				transform: scale(0.5, 0.5) translateZ(0);
				box-sizing: border-box;
				left: 0;
				top: 0;
				border-radius: 180rpx;
				border: 1rpx solid $uni-text-color-placeholder;
			}
		}

		.tui-login-logo {
			width: 60rpx;
			height: 60rpx;
		}
	}
}
</style>