<view class="checkbox">
<u-checkbox-group><u-checkbox shape="square" :checked="rememberPsw" v-model="rememberPsw"
@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">登录</button>
</view>
js代码
export default {
data() {
return {
rememberPsw: true,
radio1: 0,
showPassword: true,
showPasswordText: true,
loginForm: {
username: "admin",
password: "admin12345",
// username: "",
// password: "",
},
}
},
},
created() {
//页面加载完成,获取本地存储的账号及密码 account账号 userPwd密码 rememberPsw就记住密码
const account = uni.getStorageSync('account');
const userPwd = uni.getStorageSync('userPwd');
const rememberPsw1 = uni.getStorageSync('rememberPsw');
//若三个都有就进行赋值,没有就初始化
if (account && userPwd && rememberPsw1) {
this.loginForm.username = account;
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('account', this.loginForm.username);
uni.setStorageSync('userPwd', this.loginForm.password);
uni.setStorageSync('rememberPsw', this.rememberPsw);
} else {
//用户没有勾选“记住密码”
uni.removeStorageSync('account');
uni.removeStorageSync('userPwd');
uni.removeStorageSync('rememberPsw');
this.loginForm.account = '';
this.loginForm.userPwd = '';
}
},
showPassWord() {
this.showPassword = !this.showPassword;
this.showPasswordText = !this.showPasswordText;
},
}
记住密码
不记住密码
完整代码:
<template>
<view class="container">
<view class="normal-login-container">
<view class="logo-content">
<view class="title">
<text >
您好,
</text><br>
<text>欢迎</text>
</view>
</view>
<view class="login-form-content">
<view class="input-item">
<view class="input-itemOne">
<view class="iconfont icon-user icon"></view>
<view class="text">账号</view>
</view>
<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" />
</view>
<view class="input-item">
<view class="input-itemOne">
<view class="iconfont icon-password icon"></view>
<view class="text">密码</view>
</view>
<view class="uni_password">
<input v-model="loginForm.password" :type="showPasswordText ? 'password' : 'text'"
placeholder="请输入密码" style="font-size: 14px;border: none;outline: none;">
</input>
<view class="">
<img style="width: 18px;height: 11px;" src="../static/eye-xianshi.png" alt=""
v-show="!showPasswordText" @click="showPasswordText = true">
<img style="width: 18px;height: 11px;" src="../static/eye-yincang.png" alt=""
v-show="showPasswordText" @click="showPasswordText = false">
</view>
</view>
</view>
</view>
<!-- //记住密码 -->
<view class="checkbox">
<u-checkbox-group><u-checkbox shape="square" :checked="rememberPsw" v-model="rememberPsw"
@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">登录</button>
</view>
</view>
</view>
</template>
<script>
// import {
// getCodeImg
// } from '@/api/login'
export default {
data() {
return {
rememberPsw: true,
radio1: 0,
showPassword: true,
showPasswordText: true,
codeUrl: "",
captchaEnabled: false,
// 用户注册开关
register: false,
globalConfig: getApp().globalData.config,
loginForm: {
username: "admin",
password: "admin123",
// username: "",
// password: "",
code: "",
uuid: '',
},
}
},
created() {
// this.getCode()
//页面加载完成,获取本地存储的账号及密码
const account = uni.getStorageSync('account');
const userPwd = uni.getStorageSync('userPwd');
const rememberPsw1 = uni.getStorageSync('rememberPsw');
//若三个都有就进行赋值,没有就初始化
if (account && userPwd && rememberPsw1) {
this.loginForm.username = account;
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('account', this.loginForm.username);
uni.setStorageSync('userPwd', this.loginForm.password);
uni.setStorageSync('rememberPsw', this.rememberPsw);
} else {
//用户没有勾选“记住密码”
uni.removeStorageSync('account');
uni.removeStorageSync('userPwd');
uni.removeStorageSync('rememberPsw');
this.loginForm.account = '';
this.loginForm.userPwd = '';
}
},
showPassWord() {
this.showPassword = !this.showPassword;
this.showPasswordText = !this.showPasswordText;
},
// 登录方法
async handleLogin() {
if (this.loginForm.username === "") {
this.$modal.msgError("请输入您的账号")
} else if (this.loginForm.password === "") {
this.$modal.msgError("请输入您的密码")
} else {
this.$modal.loading("登录中,请耐心等待...")
this.pwdLogin()
}
},
// 密码登录
async pwdLogin() {
this.$store.dispatch('Login', this.loginForm).then(() => {
this.$modal.closeLoading()
this.loginSuccess()
}).catch()
},
// 登录成功后,处理函数
loginSuccess(result) {
// 设置用户信息
this.$store.dispatch('GetInfo').then(res => {
this.$tab.reLaunch('/pages/index')
})
}
}
}
</script>
<style lang="scss">
.container {
background-image: url('../static/bgc.png');
/* 背景图片的路径 */
background-size: cover;
/* 背景图片的尺寸适应容器 */
background-repeat: no-repeat;
/* 不重复平铺背景图片 */
height: 100vh;
/* 设置容器的高度,以便填满整个屏幕 */
}
.action-btn {
width: 305px;
height: 45px;
margin-left: 35px;
margin-right: 35px;
}
// page {
// background-color: #ffffff;
// }
.normal-login-container {
width: 100%;
.logo-content {
margin-left: 35px;
width: 100%;
font-size: 21px;
// padding-top: 10%;
.title {
padding-top: 57px;
color: #FFF;
font-family: PingFang SC;
line-height: 44px;
}
}
.login-form-content {
margin-top: 107px;
.input-item {
margin-right: 35px;
.input-itemOne {
display: flex;
.icon {
margin-right: 7px;
margin-left: 35px;
font-family: PingFang SC;
font-size: 18px;
font-style: normal;
font-weight: bold;
border: 0px solid #333333;
}
.text {
font-size: 15px;
font-family: PingFang SC-Bold, PingFang SC;
font-weight: bold;
padding-bottom: 11px;
}
}
.input {
color: #000;
font-family: PingFang SC;
margin-left: 35px;
font-size: 14px;
margin-bottom: 20px;
padding-bottom: 4px;
border-bottom: 1px solid #E6E6E6;
}
// 请输入密码
.uni_password {
margin-left: 35px;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid #E6E6E6;
margin-bottom: 21px;
padding-bottom: 4px;
}
}
}
// 按钮
.checkbox {
width: 15px;
height: 15px;
border-radius: 0px 0px 0px 0px;
opacity: 1;
margin-left: 37px;
margin-bottom: 10px;
}
}
</style>