栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

vue(uni-app) 实现记住账号密码功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue(uni-app) 实现记住账号密码功能

1、示例:

2、代码:

只看核心代码块 其他不用管


	import {
		login
	} from "@/pages/login/api/api.js";
	export default {
		data() {
			return {
				value: "",
				rememberPsw: true,//复选框状态 默认勾选
				user: {
					userName: '',//账号
					password: ''//密码
				}
			};
		},
		mounted() {//记住密码
		//缓存的账号
		const HBusername = uni.getStorageSync('HBusername');
		//缓存的密码
		const HBpassword = uni.getStorageSync('HBpassword');
				console.log("缓存的账号:"+HBusername)
				console.log("缓存的密码:"+HBpassword)
				//有缓存就赋值给文本没有就清空
			if (HBusername && HBpassword) {
				this.user.userName = HBusername;
				this.user.password = HBpassword;
			} else {
				this.user.userName = '';
				this.user.password = '';
			}
			//自动登录
		},
		methods: {
			//复选框
			checkboxChange: function(e) {
				console.log("复选框状态:"+e.detail.value.length);
				if (e.detail.value.length == 1) {
					//获取缓存的账号 赋值
					uni.getStorageSync('HBusername',this.user.userName);
					uni.getStorageSync('HBpassword',this.user.password);
				} else {//销毁
					uni.removeStorageSync('HBusername');
					uni.removeStorageSync('HBpassword');              
				}
			},
			login() {
				var timestamp = Date.parse(new Date()) + 36000000;
				if (this.auth() == 'success') {
					login(this.user).then(res => {
						if (res.data.status == '200') {
							debugger
						   //勾选就缓存账号和密码
							if (this.rememberPsw) {
								uni.setStorageSync('HBusername', this.user.userName);
								uni.setStorageSync('HBpassword', this.user.password);
							} else {//销毁缓存
								uni.removeStorageSync('HBusername');
								uni.removeStorageSync('HBpassword');
							}
							this.$message(res.data.message, "none");
							uni.setStorageSync('userMessage', res.data.result);
							this.$jump('/pages/zpdemo/pageNew');
						}else {
							this.$message(res.data.message,"none");
						}
					}).catch(err => {
						console.log(err)
					})
				}
			},
			auth() {
				if (this.user.userName.length < 1 || this.user.userName == '' && this.user.password.length < 1 || this.user
					.password == '') {
					this.$message('请输入用户名与密码', 'none');
					this.inputColorConfigToUserName.borderColor = this.colorFocusColorError;
					this.inputColorConfigToPassWord.borderColor = this.colorFocusColorError;
					return '';
				}
				if (this.user.userName.length < 1 || this.user.userName == '') {
					this.$message('请输入用户名', 'none');
					this.inputColorConfigToUserName.borderColor = this.colorFocusColorError;
				}
				if (this.user.password.length < 1 || this.user.password == '') {
					this.$message('请输入密码', 'none');
					this.inputColorConfigToPassWord.borderColor = this.colorFocusColorError;
				}
				return 'success'
			},
		},
}
3、步骤逻辑:

第一次登录输入账号密码默认是勾选记住账号密码的,登录会将账号密码存入进缓存。(复选框做了缓存的存入与销毁,勾选状态是‘1’,将账号密码存入缓存,不勾选状态是‘0’,销毁缓存。)

账号 key:HBusername
密码 key:HBpassword

第二次及往后每次登录,都会先从缓存中获取账号密码key,将账号密码填充进文本框,没有就不填充。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号