feat(user): user reset password

pull/56/head
Angelo 2022-05-11 12:08:46 +08:00
parent b4f7d65ca2
commit f88b90388c
2 changed files with 117 additions and 95 deletions

View File

@ -241,32 +241,32 @@ class UserViewSet(CustomModelViewSet):
else: else:
return ErrorResponse(msg="未获取到用户") return ErrorResponse(msg="未获取到用户")
@action(methods=["PUT"], detail=True, permission_classes=[IsAuthenticated]) # @action(methods=["PUT"], detail=True, permission_classes=[IsAuthenticated])
def reset_password(self, request, *args, **kwargs): # def reset_password(self, request, *args, **kwargs):
"""重置密码""" # """重置密码"""
instance = Users.objects.filter(id=kwargs.get("pk")).first() # instance = Users.objects.filter(id=kwargs.get("pk")).first()
if instance:
instance.set_password(settings.DEFAULT_PASSWORD)
instance.save()
return DetailResponse(data=None, msg="密码重置成功")
else:
return ErrorResponse(msg="未获取到用户")
# @action(methods=['PUT'], detail=True)
# def reset_password(self, request, pk):
# """
# 密码重置
# """
# instance = Users.objects.filter(id=pk).first()
# data = request.data
# new_pwd = data.get('newPassword')
# new_pwd2 = data.get('newPassword2')
# if instance: # if instance:
# if new_pwd != new_pwd2: # instance.set_password(settings.DEFAULT_PASSWORD)
# return ErrorResponse(msg="两次密码不匹配") # instance.save()
# else: # return DetailResponse(data=None, msg="密码重置成功")
# instance.password = make_password(new_pwd)
# instance.save()
# return DetailResponse(data=None, msg="修改成功")
# else: # else:
# return ErrorResponse(msg="未获取到用户") # return ErrorResponse(msg="未获取到用户")
@action(methods=["PUT"], detail=True)
def reset_password(self, request, pk):
"""
密码重置
"""
instance = Users.objects.filter(id=pk).first()
data = request.data
new_pwd = data.get("newPassword")
new_pwd2 = data.get("newPassword2")
if instance:
if new_pwd != new_pwd2:
return ErrorResponse(msg="两次密码不匹配")
else:
instance.password = make_password(new_pwd)
instance.save()
return DetailResponse(data=None, msg="修改成功")
else:
return ErrorResponse(msg="未获取到用户")

View File

@ -31,20 +31,36 @@
/> />
</div> </div>
</d2-crud-x> </d2-crud-x>
<!-- <el-dialog title="密码重置" :visible.sync="dialogFormVisible" :close-on-click-modal="false"> <el-dialog
<el-form :model="resetPwdForm" ref="resetPwdForm" :rules="passwordRules"> title="密码重置"
:visible.sync="dialogFormVisible"
:close-on-click-modal="false"
>
<el-form :model="resetPwdForm" ref="resetPwdForm" :rules="passwordRules">
<el-form-item label="密码" prop="pwd"> <el-form-item label="密码" prop="pwd">
<el-input v-model="resetPwdForm.pwd" type="password" show-password clearable autocomplete="off"></el-input> <el-input
v-model="resetPwdForm.pwd"
type="password"
show-password
clearable
autocomplete="off"
></el-input>
</el-form-item> </el-form-item>
<el-form-item label="再次输入密码" prop="pwd2"> <el-form-item label="再次输入密码" prop="pwd2">
<el-input v-model="resetPwdForm.pwd2" type="password" show-password clearable autocomplete="off"></el-input> <el-input
v-model="resetPwdForm.pwd2"
type="password"
show-password
clearable
autocomplete="off"
></el-input>
</el-form-item> </el-form-item>
</el-form> </el-form>
<div slot="footer" class="dialog-footer"> <div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false"> </el-button> <el-button @click="dialogFormVisible = false"> </el-button>
<el-button type="primary" @click="resetPwdSubmit"> </el-button> <el-button type="primary" @click="resetPwdSubmit"> </el-button>
</div> </div>
</el-dialog> --> </el-dialog>
</d2-container> </d2-container>
</template> </template>
@ -57,39 +73,45 @@ export default {
name: 'user', name: 'user',
mixins: [d2CrudPlus.crud], mixins: [d2CrudPlus.crud],
data () { data () {
// var validatePass = (rule, value, callback) => { var validatePass = (rule, value, callback) => {
// const pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}') const pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}')
// if (value === '') { if (value === '') {
// callback(new Error('')) callback(new Error('请输入密码'))
// } else if (!pwdRegex.test(value)) { } else if (!pwdRegex.test(value)) {
// callback(new Error('()')) callback(new Error('您的密码复杂度太低(密码中必须包含字母、数字)'))
// } else { } else {
// if (this.resetPwdForm.pwd2 !== '') { if (this.resetPwdForm.pwd2 !== '') {
// this.$refs.resetPwdForm.validateField('pwd2') this.$refs.resetPwdForm.validateField('pwd2')
// } }
// callback() callback()
// } }
// } }
// var validatePass2 = (rule, value, callback) => { var validatePass2 = (rule, value, callback) => {
// if (value === '') { if (value === '') {
// callback(new Error('')) callback(new Error('请再次输入密码'))
// } else if (value !== this.resetPwdForm.pwd) { } else if (value !== this.resetPwdForm.pwd) {
// callback(new Error('!')) callback(new Error('两次输入密码不一致!'))
// } else { } else {
// callback() callback()
// } }
// } }
return { return {
// dialogFormVisible: false, dialogFormVisible: false,
// resetPwdForm: { resetPwdForm: {
// id: null, id: null,
// pwd: null, pwd: null,
// pwd2: null pwd2: null
// }, },
// passwordRules: { passwordRules: {
// pwd: [{ required: true, message: '' }, { validator: validatePass, trigger: 'blur' }], pwd: [
// pwd2: [{ required: true, message: '' }, { validator: validatePass2, trigger: 'blur' }] { required: true, message: '必填项' },
// } { validator: validatePass, trigger: 'blur' }
],
pwd2: [
{ required: true, message: '必填项' },
{ validator: validatePass2, trigger: 'blur' }
]
}
} }
}, },
methods: { methods: {
@ -109,40 +131,40 @@ export default {
delRequest (row) { delRequest (row) {
return api.DelObj(row.id) return api.DelObj(row.id)
}, },
resetPassword (scope) { // resetPassword (scope) {
api.ResetPwd(scope.row).then((res) => { // api.ResetPwd(scope.row).then((res) => {
this.$message.success('密码重置成功') // this.$message.success('')
})
}
// //
// resetPwd ({ row }) {
// this.dialogFormVisible = true
// this.resetPwdForm.id = row.id
// },
// //
// resetPwdSubmit () {
// const that = this
// that.$refs.resetPwdForm.validate((valid) => {
// if (valid) {
// const params = {
// id: that.resetPwdForm.id,
// newPassword: that.$md5(that.resetPwdForm.pwd),
// newPassword2: that.$md5(that.resetPwdForm.pwd2)
// }
// api.ResetPwd(params).then(res => {
// that.dialogFormVisible = false
// that.resetPwdForm = {
// id: null,
// pwd: null,
// pwd2: null
// }
// that.$message.success('')
// })
// } else {
// that.$message.error('')
// }
// }) // })
// } // }
//
resetPassword ({ row }) {
this.dialogFormVisible = true
this.resetPwdForm.id = row.id
},
//
resetPwdSubmit () {
const that = this
that.$refs.resetPwdForm.validate((valid) => {
if (valid) {
const params = {
id: that.resetPwdForm.id,
newPassword: that.$md5(that.resetPwdForm.pwd),
newPassword2: that.$md5(that.resetPwdForm.pwd2)
}
api.ResetPwd(params).then((res) => {
that.dialogFormVisible = false
that.resetPwdForm = {
id: null,
pwd: null,
pwd2: null
}
that.$message.success('修改成功')
})
} else {
that.$message.error('表单校验失败,请检查')
}
})
}
} }
} }
</script> </script>