<template>
<el-form
class="login-box1"
ref="loginForm"
:model="loginForm"
:rules="rules"
label-width="100px"
>
<h3 class="login-title">修改密码</h3>
<el-form-item label="原密码" prop="oldpassword">
<el-input
type="password"
show-password
auto-complete="off"
placeholder="请输入原密码"
v-model="loginForm.oldpassword"
></el-input>
</el-form-item>
<el-form-item label="新密码" prop="password">
<el-input
type="password"
show-password
auto-complete="off"
placeholder="请设置新密码"
v-model="loginForm.password"
></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="password2">
<el-input
type="password"
show-password
auto-complete="off"
placeholder="请确认新密码"
v-model="loginForm.password2"
></el-input>
</el-form-item>
<el-form-item style="float:right">
<el-button type="primary" @click.native.prevent="handleLogin"
>保存</el-button
>
<el-button @click="$refs['loginForm'].resetFields()">重置</el-button>
</el-form-item>
</el-form>
</template>
<script>
import { getSecondpwd, addSecondpwd, updateSecondpwd } from "@/api/self/login";
import { praseStrEmpty } from "@/utils/ruoyi";
export default {
name: "reset",
data() {
const validateNewPassword = (rule, value, callback) => {
if (value === this.loginForm.oldpassword) {
callback(new Error("新密码不能与原密码相同!"));
} else {
callback();
}
};
const validateNewPassword2 = (rule, value, callback) => {
if (value !== this.loginForm.password) {
callback(new Error("与新密码不一致!"));
} else {
callback();
}
};
return {
loginForm: {
username: "",
empNum: "",
empName: "",
oldpassword: "",
password: "",
password2: "",
},
// 表单校验
rules: {
oldpassword: [
{ required: true, message: "请输入原密码", trigger: "blur" },
],
password: [
{ required: true, message: "请设置新密码", trigger: "blur" },
{ validator: validateNewPassword, trigger: "blur" },
],
password2: [
{ required: true, message: "请确认新密码", trigger: "blur" },
{ validator: validateNewPassword2, trigger: "blur" },
],
},
};
},
created() {
// this.getCookie()
},
methods: {
/** 提交按钮 */
handleLogin() {
this.$refs.loginForm.validate().then((valid) => {
if (valid) {
let userId = this.$store.state.user.userid;
this.loginForm.empNum = userId;
getSecondpwd(userId)
.then((res) => {
// debugger;
if (res.password == this.loginForm.oldpassword) {
if (praseStrEmpty(this.loginForm.empNum) != "")
updateSecondpwd(this.loginForm).then((response) => {
alert("修改成功");
});
} else {
alert("密码错误");
}
})
.catch(() => {});
}
});
},
},
};
</script>
<style lang="scss">
.user-account-key {
width: 500px;
margin: 100px auto;
}
.login-box1 {
border: 1px solid #dcdfe6;
width: 500px;
height: 340px;
margin: 180px auto;
padding: 35px 35px 15px 35px;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
box-shadow: 0 0 25px #909399;
}
.login-title {
text-align: center;
margin: 0 auto 40px auto;
color: #303133;
}
</style>
/**
* 通用js方法封装处理
*/
// 转换字符串,undefined,null等转化为""
export function praseStrEmpty(str) {
if (!str || str == "undefined" || str == "null") {
return "";
}
return str;
}
查询密码页面
<template>
<!-- <div v-if="reset1"><reset></reset></div> -->
<div v-if="login1">
<el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-box" @submit.native.prevent v-if="!isNew">
<h3 class="login-title">请输入查询密码</h3>
<el-form-item prop="password" >
<el-input
v-model="loginForm.password"
type="password"
auto-complete="off"
placeholder="请输入查询密码"
@keyup.enter.native="handleLogin"
>
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
@keyup.enter="loginenter"
</el-input>
</el-form-item>
<el-form-item >
<el-button type="primary" @click.native.prevent="handleLogin" style="float:right">确定</el-button>
<!-- <el-button type="text" @click="resetpassword">文字按钮</el-button> -->
</el-form-item>
</el-form>
<el-form ref="loginForm" :model="loginForm" :rules="firstRules" class="login-box" @submit.native.prevent v-else>
<h3 class="login-title">请输入查询密码</h3>
<el-form-item prop="password" >
<el-input
v-model="loginForm.password"
type="password"
auto-complete="off"
placeholder="首次登录需设置查询密码"
@keyup.enter.native="handleLogin"
>
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
@keyup.enter="loginenter"
</el-input>
</el-form-item>
<el-form-item prop="confirmPassword" >
<el-input
v-model="loginForm.confirmPassword"
type="password"
auto-complete="off"
placeholder="请再次输入查询密码"
@keyup.enter.native="handleLogin"
>
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
@keyup.enter="loginenter"
</el-input>
</el-form-item>
<el-form-item >
<el-button type="primary" @click.native.prevent="handleFisrtLogin" style="float:right">确定</el-button>
</el-form-item>
</el-form>
</div>
<div v-else>
<salary></salary>
</div>
</template>
<script>
import { listSecondpwd, getSecondpwd, delSecondpwd, addSecondpwd, updateSecondpwd, exportSecondpwd } from "@/api/self/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import salary from './index.vue'
import reset from '../reset/index.vue'
export default {
name: "Login",
components:{
salary,
reset,
},
data() {
const equalToPassword = (rule, value, callback) => {
if (this.loginForm.password !== value) {
callback(new Error("两次输入的密码不一致"));
} else {
callback();
}
};
return {
reset1:false,
login1:true,
loginForm: {
username: "",
empNum:"",
empName:"",
password: '',
confirmPassword:'',
},
// 表单验证,需要在 el-form-item 元素中增加 prop 属性
rules: {
password: [
{required: true, message: '密码不可为空', trigger: 'blur'}
],
},
firstRules: {
password: [
{required: true, message: '密码不可为空', trigger: 'blur'}
],
confirmPassword: [
{ required: true, trigger: "blur", message: "请再次输入您的密码" },
{ required: true, validator: equalToPassword, trigger: "blur" }
]
},
isNew: true,
}
},
created(){
this.getCookie()
this.handleIfFirst()
},
methods: {
loginenter() {
console.log('哎呀,登录中...');
},
resetpassword(){
this.reset1 = true
},
handleIfFirst () {
let userId = this.$store.state.user.userid;
getSecondpwd(userId).then(res => {
if(res.isNew){
this.isNew=true;
}else {
this.isNew=false
}
}).catch(() => {})
},
handleLogin () {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true;
let userId = this.$store.state.user.userid;
getSecondpwd(userId).then(res => {
if(res.isNew){
this.isNew=res.isNew;
}else {
this.isNew=false
if(res.password==this.loginForm.password){
this.login1=false
}else {
alert("密码错误")
}
}
}).catch(() => {})
}
});
},
handleFisrtLogin () {
this.loginForm.empNum=this.$store.state.user.userid
this.loginForm.empName=this.$store.state.user.name
//alert(this.loginForm.empNum)
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true;
addSecondpwd(this.loginForm).then(res => {
this.login1=false
}).catch(() => {})
}
});
},
getCookie(){
const username = Cookies.get('username')
const password = Cookies.get('password')
this.loginForm = {
username: username === undefined ? this.loginForm.username : username,
password: password === undefined ? this.loginForm.password : decrypt(password),
}
}
}
}
</script>
<style lang="scss" scoped>
.login-box {
border: 1px solid #DCDFE6;
width: 500px;
margin: 180px auto;
padding: 35px 35px 15px 35px;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
box-shadow: 0 0 25px #909399;
}
.login-title {
text-align: center;
margin: 0 auto 40px auto;
color: #303133;
}
</style>
'@/utils/jsencrypt'
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='
const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
'7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
'UP8iWi1Qw0Y='
// 加密
export function encrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对数据进行加密
}
// 解密
export function decrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPrivateKey(privateKey) // 设置私钥
return encryptor.decrypt(txt) // 对数据进行解密
}
在package.json中配置
"dependencies": {
"axios": "0.21.0",
"element-ui": "2.15.5",
"js-cookie": "2.2.1",
"jsencrypt": "3.0.0-rc.1",
},
登录页面
<template>
<div class="login">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
<h3 class="title">后台管理系统</h3>
<el-form-item prop="username">
<el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
<svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
v-model="loginForm.password"
type="password"
auto-complete="off"
placeholder="密码"
@keyup.enter.native="handleLogin"
>
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
</el-input>
</el-form-item>
<!--<el-form-item prop="code" v-if="captchaOnOff">-->
<!--<el-input-->
<!--v-model="loginForm.code"-->
<!--auto-complete="off"-->
<!--placeholder="验证码"-->
<!--style="width: 63%"-->
<!--@keyup.enter.native="handleLogin"-->
<!-->-->
<!--<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
<!--</el-input>-->
<!--<div class="login-code">-->
<!--<img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
<!--</div>-->
<!--</el-form-item>-->
<el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
<el-form-item style="width:100%;">
<el-button
:loading="loading"
size="medium"
type="primary"
style="width:100%;"
@click.native.prevent="handleLogin"
>
<span v-if="!loading">登 录</span>
<span v-else>登 录 中...</span>
</el-button>
<div style="float: right;" v-if="register">
<router-link class="link-type" :to="'/register'">立即注册</router-link>
</div>
</el-form-item>
</el-form>
<!-- 底部 -->
<div class="el-login-footer">
<span>Copyright ©2019</span>
</div>
</div>
</template>
<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
export default {
name: "Login",
data() {
return {
codeUrl: "",
cookiePassword: "",
loginForm: {
username: "",
password: "",
rememberMe: false,
code: "",
uuid: ""
},
loginRules: {
username: [
{ required: true, trigger: "blur", message: "请输入您的账号" }
],
password: [
{ required: true, trigger: "blur", message: "请输入您的密码" }
],
code: [{ required: true, trigger: "change", message: "请输入验证码" }]
},
loading: false,
// 验证码开关
captchaOnOff: false,
// 注册开关
register: false,
redirect: undefined
};
},
watch: {
$route: {
handler: function(route) {
this.redirect = route.query && route.query.redirect;
},
immediate: true
}
},
created() {
//this.getCode();
this.getCookie();
this.getSrc();
},
methods: {
// getCode() {
// getCodeImg().then(res => {
// this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
// if (this.captchaOnOff) {
// this.codeUrl = "data:image/gif;base64," + res.img;
// this.loginForm.uuid = res.uuid;
// }
// });
// },
getCookie() {
const username = Cookies.get("username");
const password = Cookies.get("password");
const rememberMe = Cookies.get('rememberMe')
this.loginForm = {
username: username === undefined ? this.loginForm.username : username,
password: password === undefined ? this.loginForm.password : decrypt(password),
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
};
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true;
if (this.loginForm.rememberMe) {
Cookies.set("username", this.loginForm.username, { expires: 30 });
Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
} else {
Cookies.remove("username");
Cookies.remove("password");
Cookies.remove('rememberMe');
}
let ifTest=process.env.VUE_APP_IF_TEST
this.$store.dispatch("Login", this.loginForm).then(() => {
this.$router.push({ path: this.redirect || "/" }).catch(()=>{
this.loading = false;
});
}).catch(() => {
this.loading = false;
if (this.captchaOnOff) {
this.getCode();
}
});
}
});
},
getSrc(){
console.log("="+this.isSrc)
this.isSrc=this.getQueryString("isSrc");
console.log(this.isSrc)
},
getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
}
};
</script>
<style rel="stylesheet/scss" lang="scss">
.login {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
background-image: url("../assets/images/login-background.jpg");
background-size: cover;
}
.title {
margin: 0px auto 30px auto;
text-align: center;
color: #707070;
}
.login-form {
border-radius: 6px;
background: #ffffff;
width: 400px;
padding: 25px 25px 5px 25px;
.el-input {
height: 38px;
input {
height: 38px;
}
}
.input-icon {
height: 39px;
width: 14px;
margin-left: 2px;
}
}
.login-tip {
font-size: 13px;
text-align: center;
color: #bfbfbf;
}
.login-code {
width: 33%;
height: 38px;
float: right;
img {
cursor: pointer;
vertical-align: middle;
}
}
.el-login-footer {
height: 40px;
line-height: 40px;
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
color: #fff;
font-family: Arial;
font-size: 12px;
letter-spacing: 1px;
}
.login-code-img {
height: 38px;
}
</style>