基于uniapp实现登录记住密码功能

基于uniapp实现登录记住密码功能

<template>
	<view class="normal-login-container">
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<!-- //记住密码 -->
			<view style="margin: 5px auto;">
				<u-checkbox-group><u-checkbox :checked="rememberPsw" v-model="rememberPsw" shape="circle" 					@change="saveps()" label="记住账号密码"></u-checkbox></u-checkbox-group>
			</view>
            <!-- //记住密码 -->
			<view class="action-btn"><button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button></view>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			rememberPsw: false,
			loginForm: {
				username: '',
				password: '',
				code: '',
				uuid: ''
			}
		};
	},
	created() {
		//页面加载完成,获取本地存储的账号及密码
		const Phone = uni.getStorageSync('Phone'); 
		const userPwd = uni.getStorageSync('userPwd');
		const rememberPsw1 = uni.getStorageSync('rememberPsw');
        //若三个都有就进行赋值,没有就初始化
		if (Phone && userPwd && rememberPsw1) {
			this.loginForm.username = Phone;
			this.loginForm.password = userPwd;
			this.rememberPsw = rememberPsw1;
		} else {
			this.loginForm.username = '';
			this.loginForm.password = '';
			this.rememberPsw = false;
		}
	},
	methods: {
        //根据选项按键来决定是否在本地存储账号密码
		saveps() {
			this.rememberPsw = !this.rememberPsw;
			this.setPsw();
		},
		//记住密码操作
		setPsw() {
			if (this.rememberPsw) {
				uni.setStorageSync('Phone', this.loginForm.username);
				uni.setStorageSync('userPwd', this.loginForm.password);
				uni.setStorageSync('rememberPsw', this.rememberPsw);
			} else {
				uni.removeStorageSync('Phone');
				uni.removeStorageSync('userPwd');
				uni.removeStorageSync('rememberPsw');
				this.loginForm.Phone = '';
				this.loginForm.userPwd = '';
			}
		},
	}
};
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值