|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <el-form :model="ldata" ref="ChangePasswordForm" :rules="rules"> |
| 4 | + <div class="icon-lable"> |
| 5 | + <i class="el-icon-lock" /> |
| 6 | + New Password |
| 7 | + </div> |
| 8 | + <el-form-item prop="newpassword"> |
| 9 | + <el-input type="password" v-model="ldata.newpassword"></el-input> |
| 10 | + </el-form-item> |
| 11 | + <div class="icon-lable"> |
| 12 | + <i class="el-icon-lock" /> |
| 13 | + Repeat New Password |
| 14 | + </div> |
| 15 | + <el-form-item prop="newpasswdrepeat"> |
| 16 | + <el-input type="password" v-model="ldata.newpasswdrepeat"></el-input> |
| 17 | + </el-form-item> |
| 18 | + <div v-if="old_password_required"> |
| 19 | + <div class="icon-lable"> |
| 20 | + <i class="el-icon-lock" /> |
| 21 | + Old Password |
| 22 | + </div> |
| 23 | + <el-form-item prop="oldpassword"> |
| 24 | + <el-input type="password" v-model="ldata.oldpassword"></el-input> |
| 25 | + </el-form-item> |
| 26 | + </div> |
| 27 | + <el-form-item> |
| 28 | + <el-button |
| 29 | + type="primary" |
| 30 | + v-on:click="onSubmit();" |
| 31 | + :loading="buttonLoading" |
| 32 | + > |
| 33 | + Change |
| 34 | + </el-button> |
| 35 | + <el-button v-on:click="reset();">Reset</el-button> |
| 36 | + </el-form-item> |
| 37 | + </el-form> |
| 38 | + </div> |
| 39 | +</template> |
| 40 | + |
| 41 | +<script> |
| 42 | +import apiurl from './../../apiurl'; |
| 43 | +
|
| 44 | +export default { |
| 45 | + name: 'UserChangePassword', |
| 46 | + data() { |
| 47 | + let validatePasswd = (rule, value, callback) => { |
| 48 | + if (value === '' || value === this.ldata.newpassword) { |
| 49 | + callback(); |
| 50 | + } else { |
| 51 | + callback(new Error('Password mismatch')); |
| 52 | + } |
| 53 | + }; |
| 54 | + let validateOldPasswd = (rule, value, callback) => { |
| 55 | + this.$axios |
| 56 | + .post(apiurl('/account/password'), { |
| 57 | + password: this.ldata.oldpassword |
| 58 | + }) |
| 59 | + .then(() => { |
| 60 | + callback(); |
| 61 | + }) |
| 62 | + .catch(err => { |
| 63 | + if(err.request.status === 403) { |
| 64 | + callback(new Error('Old Password Wrong')); |
| 65 | + } else { |
| 66 | + callback(new Error('Unkown Error')); |
| 67 | + } |
| 68 | + }); |
| 69 | + }; |
| 70 | + return { |
| 71 | + ldata: { |
| 72 | + oldpassword: '', |
| 73 | + newpassword: '', |
| 74 | + newpasswdrepeat: '' |
| 75 | + }, |
| 76 | + rules: { |
| 77 | + newpassword: [ |
| 78 | + { required: true, message: 'Input your password', trigger: 'blur' }, |
| 79 | + { min: 6, message: 'No less than 6 characters', trigger: 'blur' } |
| 80 | + ], |
| 81 | + newpasswdrepeat: [ |
| 82 | + { required: true, message: 'Repeat your password', trigger: 'blur' }, |
| 83 | + { validator: validatePasswd, trigger: 'blur' } |
| 84 | + ], |
| 85 | + oldpassword: [ |
| 86 | + { validator: validateOldPasswd, trigger: 'blur' } |
| 87 | + ] |
| 88 | + }, |
| 89 | + old_password_required: false, |
| 90 | + buttonLoading: false |
| 91 | + }; |
| 92 | + }, |
| 93 | + methods: { |
| 94 | + submit() { |
| 95 | + this.buttonLoading = true; |
| 96 | + this.$axios |
| 97 | + .patch(apiurl('/account/password'), { |
| 98 | + password: this.ldata.newpassword |
| 99 | + }) |
| 100 | + .then(() => { |
| 101 | + this.buttonLoading = false; |
| 102 | + this.$SegmentMessage.success(this, 'Changed successfully'); |
| 103 | + }) |
| 104 | + .catch(err => { |
| 105 | + if (err.request.status === 401) { |
| 106 | + this.$SegmentMessage.error(this, 'Please login first'); |
| 107 | + this.$store.state.user.showlogin = true; |
| 108 | + } |
| 109 | + if (err.request.status === 403) { |
| 110 | + this.$SegmentMessage.error(this, 'Please enter your old password'); |
| 111 | + this.old_password_required = true; |
| 112 | + } |
| 113 | + this.buttonLoading = false; |
| 114 | + }); |
| 115 | + }, |
| 116 | + onSubmit() { |
| 117 | + this.$refs['ChangePasswordForm'].validate((valid) => { |
| 118 | + if (valid) { |
| 119 | + this.submit(); |
| 120 | + } else { |
| 121 | + return false; |
| 122 | + } |
| 123 | + }); |
| 124 | + }, |
| 125 | + reset() { |
| 126 | + this.$refs['ChangePasswordForm'].resetFields(); |
| 127 | + this.old_password_required = false; |
| 128 | + } |
| 129 | + } |
| 130 | +}; |
| 131 | +</script> |
| 132 | + |
| 133 | +<style scoped> |
| 134 | +.icon-lable { |
| 135 | + margin-bottom: 5px; |
| 136 | +} |
| 137 | +</style> |
0 commit comments