只看核心代码块 其他不用管
账号密码登录 记住账号密码 登录
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,将账号密码填充进文本框,没有就不填充。



