def reset_password(self, request): """ 重置密码 """ # todo: 此处token为空??? token = request.query_params.get('token', None) if token is None: return Response({'detail': ['参数错误']}, status=status.HTTP_400_BAD_REQUEST) token_obj = Token() serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) password = serializer.validated_data["password"] try: email = token_obj.confirm_validate_token(token, expiration=600) user_obj = User.objects.get(email=email) user_obj.set_password(password) user_obj.save() except Exception as e: return Response({'detail': ['token错误']}, status=status.HTTP_400_BAD_REQUEST) return Response({'status': 'ok'})
def email_reset_password(self, request): """ 通过邮箱重置密码 """ # todo: 此接口暂时不需要 serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.validated_data["email"] try: user_obj = User.objects.get(email=email) token_obj = Token() token = token_obj.generate_validate_token(email) reset_url = '{}/#/resetpwd?token={}'.format(os.environ.get('PROJECT_URL'), token) subject = '重置您的密码' content = '<br>{},您好:<br><br> 点击以下链接重置您的密码: {} <br><br>如果您没有请求重置密码,请忽略该邮件。'.format( user_obj.username, reset_url) send_html_mail(user_obj.email, subject, content) except Exception as e: return Response({'detail': ['邮箱不存在']}, status=status.HTTP_400_BAD_REQUEST) return Response({'status': 'ok'})