feat(user): user reset password
parent
b4f7d65ca2
commit
f88b90388c
|
@ -241,32 +241,32 @@ class UserViewSet(CustomModelViewSet):
|
|||
else:
|
||||
return ErrorResponse(msg="未获取到用户")
|
||||
|
||||
@action(methods=["PUT"], detail=True, permission_classes=[IsAuthenticated])
|
||||
def reset_password(self, request, *args, **kwargs):
|
||||
"""重置密码"""
|
||||
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')
|
||||
# @action(methods=["PUT"], detail=True, permission_classes=[IsAuthenticated])
|
||||
# def reset_password(self, request, *args, **kwargs):
|
||||
# """重置密码"""
|
||||
# instance = Users.objects.filter(id=kwargs.get("pk")).first()
|
||||
# if instance:
|
||||
# if new_pwd != new_pwd2:
|
||||
# return ErrorResponse(msg="两次密码不匹配")
|
||||
# else:
|
||||
# instance.password = make_password(new_pwd)
|
||||
# instance.save()
|
||||
# return DetailResponse(data=None, msg="修改成功")
|
||||
# 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 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="未获取到用户")
|
||||
|
|
|
@ -31,20 +31,36 @@
|
|||
/>
|
||||
</div>
|
||||
</d2-crud-x>
|
||||
<!-- <el-dialog title="密码重置" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
|
||||
<el-form :model="resetPwdForm" ref="resetPwdForm" :rules="passwordRules">
|
||||
<el-dialog
|
||||
title="密码重置"
|
||||
:visible.sync="dialogFormVisible"
|
||||
:close-on-click-modal="false"
|
||||
>
|
||||
<el-form :model="resetPwdForm" ref="resetPwdForm" :rules="passwordRules">
|
||||
<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 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>
|
||||
<div slot="footer" class="dialog-footer">
|
||||
<el-button @click="dialogFormVisible = false">取 消</el-button>
|
||||
<el-button type="primary" @click="resetPwdSubmit">确 定</el-button>
|
||||
</div>
|
||||
</el-dialog> -->
|
||||
</el-dialog>
|
||||
</d2-container>
|
||||
</template>
|
||||
|
||||
|
@ -57,39 +73,45 @@ export default {
|
|||
name: 'user',
|
||||
mixins: [d2CrudPlus.crud],
|
||||
data () {
|
||||
// var validatePass = (rule, value, callback) => {
|
||||
// const pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}')
|
||||
// if (value === '') {
|
||||
// callback(new Error('请输入密码'))
|
||||
// } else if (!pwdRegex.test(value)) {
|
||||
// callback(new Error('您的密码复杂度太低(密码中必须包含字母、数字)'))
|
||||
// } else {
|
||||
// if (this.resetPwdForm.pwd2 !== '') {
|
||||
// this.$refs.resetPwdForm.validateField('pwd2')
|
||||
// }
|
||||
// callback()
|
||||
// }
|
||||
// }
|
||||
// var validatePass2 = (rule, value, callback) => {
|
||||
// if (value === '') {
|
||||
// callback(new Error('请再次输入密码'))
|
||||
// } else if (value !== this.resetPwdForm.pwd) {
|
||||
// callback(new Error('两次输入密码不一致!'))
|
||||
// } else {
|
||||
// callback()
|
||||
// }
|
||||
// }
|
||||
var validatePass = (rule, value, callback) => {
|
||||
const pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}')
|
||||
if (value === '') {
|
||||
callback(new Error('请输入密码'))
|
||||
} else if (!pwdRegex.test(value)) {
|
||||
callback(new Error('您的密码复杂度太低(密码中必须包含字母、数字)'))
|
||||
} else {
|
||||
if (this.resetPwdForm.pwd2 !== '') {
|
||||
this.$refs.resetPwdForm.validateField('pwd2')
|
||||
}
|
||||
callback()
|
||||
}
|
||||
}
|
||||
var validatePass2 = (rule, value, callback) => {
|
||||
if (value === '') {
|
||||
callback(new Error('请再次输入密码'))
|
||||
} else if (value !== this.resetPwdForm.pwd) {
|
||||
callback(new Error('两次输入密码不一致!'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
return {
|
||||
// dialogFormVisible: false,
|
||||
// resetPwdForm: {
|
||||
// id: null,
|
||||
// pwd: null,
|
||||
// pwd2: null
|
||||
// },
|
||||
// passwordRules: {
|
||||
// pwd: [{ required: true, message: '必填项' }, { validator: validatePass, trigger: 'blur' }],
|
||||
// pwd2: [{ required: true, message: '必填项' }, { validator: validatePass2, trigger: 'blur' }]
|
||||
// }
|
||||
dialogFormVisible: false,
|
||||
resetPwdForm: {
|
||||
id: null,
|
||||
pwd: null,
|
||||
pwd2: null
|
||||
},
|
||||
passwordRules: {
|
||||
pwd: [
|
||||
{ required: true, message: '必填项' },
|
||||
{ validator: validatePass, trigger: 'blur' }
|
||||
],
|
||||
pwd2: [
|
||||
{ required: true, message: '必填项' },
|
||||
{ validator: validatePass2, trigger: 'blur' }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
|
@ -109,40 +131,40 @@ export default {
|
|||
delRequest (row) {
|
||||
return api.DelObj(row.id)
|
||||
},
|
||||
resetPassword (scope) {
|
||||
api.ResetPwd(scope.row).then((res) => {
|
||||
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 (scope) {
|
||||
// api.ResetPwd(scope.row).then((res) => {
|
||||
// this.$message.success('密码重置成功')
|
||||
// })
|
||||
// }
|
||||
// 重置密码弹框
|
||||
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>
|
||||
|
|
Loading…
Reference in New Issue