updpwd.vue 5.77 KB
<template>
    <div>
        <!-- 修改密码diglog -->
		<el-dialog
          :title="$t('dialog.changePW')"
          class="manage-dialog"
          :visible.sync="passwordDialogVisible"
          :close-on-click-modal="false"
          @close="cancledialog()">
			<el-form :model="passwordForm" status-icon ref="passwordForm" :rules="rules">
				<el-form-item :label="$t('dialog.oldPW')" prop="password">
					<el-input type="password" v-model="passwordForm.password"></el-input>
				</el-form-item>
				<el-form-item :label="$t('dialog.newPW')" prop="newpass">
					<el-input type="password" v-model="passwordForm.newpass"></el-input>
				</el-form-item>
				<el-form-item :label="$t('dialog.confirmPW')" prop="checkPass">
					<el-input type="password" v-model="passwordForm.checkPass"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="passwordDialogVisible = false" class="dialog-btn">{{$t('dialog.cancel')}}</el-button>
				<el-button type="primary" :disabled="
					passwordForm.password === '' || passwordForm.newpass === '' || passwordForm.checkPass === ''
					|| passwordStatus === 'error' || newpassStatus === 'error' || checkPassStatus === 'error'
				"
				@click="changeSubmit('passwordForm')" class="dialog-btn dialog-confirm-btn">{{$t('dialog.confirm')}}</el-button>
			</div>
		</el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        var validatePass = (rule, value, callback) => {
			//var exp = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{6,16}$/; //密码强度正则,最少6位最多16位,包括1个大写字母,1个小写字母,1个数字,1个特殊字符以上两种
			var exp = /^(?=.*[0-9])((?=.*[A-Z])|(?=.*[a-z]))(?=.*[^a-zA-Z0-9]).{8,30}$/;
            if (value === '') {
				callback(new Error('请输入旧密码'));
				this.passwordStatus = 'error'
			}else{
				callback()
				this.passwordStatus = 'success'
			}
		}
        var validatePass1 = (rule, value, callback) => {
			//var exp = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{6,16}$/; 
            var exp = /^(?=.*[0-9])((?=.*[A-Z])|(?=.*[a-z]))(?=.*[^a-zA-Z0-9]).{8,30}$/;
			if (!exp.test(value)) {
				this.newpassStatus = 'error'
				return callback(new Error(this.$t("rules.wordRules")));
			}
			if (value === '') {
				callback(new Error(this.$t("rules.password")));
				this.newpassStatus = 'error'
			}else if(value == this.passwordForm.password){
				callback(new Error(this.$t("rules.newOldSame")))
				this.newpassStatus = 'error'
			} else {
				if (this.passwordForm.checkPass !== '') {
					this.$refs.passwordForm.validateField('checkPass');
				}
				callback();
				this.newpassStatus = 'success'
			}
		};
		var validatePass2 = (rule, value, callback) => {
			//var exp = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{6,16}$/; 
            var exp = /^(?=.*[0-9])((?=.*[A-Z])|(?=.*[a-z]))(?=.*[^a-zA-Z0-9]).{8,30}$/;
			if (!exp.test(value)) {
				return callback(new Error(this.$t("rules.wordRules")));
				this.checkPassStatus = 'error'
			}
			if (value === '') {
				callback(new Error(this.$t("rules.checkWord")));
				this.checkPassStatus = 'error'
			} else if (value !== this.passwordForm.newpass) {
				callback(new Error(this.$t("rules.checkDifferent")));
				this.checkPassStatus = 'error'
			} else {
				callback();
				this.checkPassStatus = 'success'
			}
		};
        return {
            passwordDialogVisible: false,
            passwordForm:{
				password:'',
				newpass:'',
				checkPass:''
			},
            rules: {
				password:[
					{ validator:validatePass, trigger: 'change' },
					{ min: 6, max: 30, message: '密码长度不小于6位', trigger: 'blur' }
				],
				newpass: [
					{ validator: validatePass1, trigger: 'change' },
				],
				checkPass: [
					{ validator: validatePass2, trigger: 'change' },
				]
			},
			passwordStatus: 'error',
			newpassStatus: 'error',
			checkPassStatus: 'error',
        }
    },
    methods: {
        dialogInit(login_name) {
			this.passwordForm.loginName = login_name
            this.passwordDialogVisible = true;
        },
        cancledialog() {
			this.$refs.passwordForm.resetFields();
        },
        changeSubmit(formName) {
			if(!this.passwordForm.newpass || !this.passwordForm.checkPass || !this.passwordForm.password){
				this.$message({
					showClose: true,
					message: '密码不能为空!',
					type: 'warning'
			    });
			}else if(this.passwordForm.newpass == this.passwordForm.checkPass){
				if(this.passwordForm.password == this.passwordForm.newpass){
				this.$message({
					showClose: true,
					message: '新旧密码不能相同!',
					type: 'warning'
              });
			  }else{
				  this.$api.management.editUser({
						loginName:this.passwordForm.loginName,
						oldPassWord:this.passwordForm.password,
						password:this.passwordForm.newpass
				  })
				.then((res) => {
					this.passwordDialogVisible = false
					if(res.data.code == 200) {
						this.$message({
							showClose: true,
							message: this.$t('message.changePWSuccess'),
							type: 'success'
						})
						this.logout()
					}else{
						this.$message({
							showClose: true,
							message: this.$t('message.changePWFailed'),
							type: 'error'
						})
					}
				})
				.catch((err) => { 
				})
			}
			}else {
				this.$message({
					showClose: true,
					message: '两次输入密码不一致!',
					type: 'warning'
              });
			}
		},
		logout() {
			for(let i in window.localStorage) {
				if(i == 'lang' || i == 'oldAlarm'){
					
				}else{
					window.localStorage.removeItem(i);
				}
			}
			for(let i in window.sessionStorage) {
				window.sessionStorage.removeItem(i);
			}
			let cookieInfo = this.$cookie.get();
			for(let k in cookieInfo) {
				this.$cookie.remove(k);
			}
			this.$router.push('/');
		},
    }
}
</script>


<style scoped>

</style>