uniapp
#login.vue
onReady() {//记住密码
//缓存的账号
const QS_username = uni.getStorageSync('QS_username');
//缓存的密码
const QS_password = uni.getStorageSync('QS_password');
console.log("缓存的账号:"+QS_username)
console.log("缓存的密码:"+QS_password)
//有缓存就赋值给文本没有就清空
if (QS_username && QS_password) {
this.username = QS_username;
this.password = QS_password;
} else {
this.username = '';
this.password = '';
}
//自动登录
},
//复选框
checkboxChange: function(e) {
console.log("复选框状态:"+e.detail.value.length);
if (e.detail.value.length == 1) {
//获取缓存的账号 赋值
uni.getStorageSync('QS_username',this.username);
uni.getStorageSync('QS_password',this.password);
} else {//销毁
uni.removeStorageSync('QS_username');
uni.removeStorageSync('QS_password');
}
},
uni.request({
url: $config.urls.get_token,
method: 'POST',
data: {
username: this.username,
password: this.password
},
success: (res) => {
console.log(res);
//勾选就缓存账号和密码
if (this.rememberPsw) {
uni.setStorageSync('QS_username', this.username);
uni.setStorageSync('QS_password', this.password);
} else {//销毁缓存
uni.removeStorageSync('QS_username');
uni.removeStorageSync('QS_password');
}
if (res.statusCode==200){
this.jwt_token = res.data.access
this.uid = res.data.uid
$config.user.set_user(this.username, this.uid, res.data.access)
uni.switchTab({
url:'/pages/my/my'
});
}else{
uni.showToast({
title: res.errMsg,
icon:'none'
});
}
},
fail: (err) => {
console.log(err);
},
complete: () => {}
});
====
<!-- 记住密码功能 --> <view class="item"> <checkbox-group @change="checkboxChange"> <checkbox id="chkRem" type="checkbox" :checked='rememberPsw' @tap="rememberPsw =! rememberPsw" color="#09CC86"/> <text class="remember_pwd">记住账号密码</text> </checkbox-group> </view>
data() {
return {
username:'',
password:'',
rememberPsw: true, //复选框状态 默认勾选
}
},
但有个问题,存储的密码是明文
? 需要寻找加密方案=》
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步