forked from iti-framework/iTi-Flask
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
7.0 KiB
Python
243 lines
7.0 KiB
Python
from dataclasses import field
|
|
from marshmallow_dataclass import dataclass
|
|
from marshmallow import validates_schema, ValidationError
|
|
from iti.applications.common.enums import GenderEnum, StatusEnum
|
|
from iti.applications.common.utils.schema import BaseSchema
|
|
from iti.applications.service.sys.verification_code import VerificationCodeUsage
|
|
from typing import ClassVar
|
|
|
|
|
|
@dataclass(base_schema=BaseSchema)
|
|
class PasswordLoginRequest:
|
|
"""
|
|
密码登录请求
|
|
"""
|
|
|
|
class Meta:
|
|
unknown = "INCLUDE"
|
|
|
|
password: str = field(
|
|
metadata={
|
|
"required": True,
|
|
"metadata": {"example": "123456", "description": "密码"},
|
|
}
|
|
)
|
|
|
|
username: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "admin", "description": "用户名"},
|
|
},
|
|
default="",
|
|
)
|
|
phone: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "18888888888", "description": "手机号"},
|
|
},
|
|
default="",
|
|
)
|
|
email: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "admin@example.com", "description": "邮箱"},
|
|
},
|
|
default="",
|
|
)
|
|
autoRegister: bool = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": True, "description": "未注册用户自动注册"},
|
|
},
|
|
default=False,
|
|
)
|
|
|
|
Schema: ClassVar[BaseSchema] = BaseSchema # For the type check
|
|
|
|
@validates_schema
|
|
def validate_schema(self, data, **kwargs):
|
|
if not data.get("username") and not data.get("phone") and not data.get("email"):
|
|
raise ValidationError(
|
|
"至少需要提供一种登录主体(用户名、手机号、邮箱)", field_name="custom"
|
|
)
|
|
return data
|
|
|
|
|
|
@dataclass(base_schema=BaseSchema)
|
|
class CodeLoginRequest:
|
|
"""
|
|
验证码登录请求
|
|
"""
|
|
|
|
code: str = field(
|
|
metadata={
|
|
"required": True,
|
|
"metadata": {"example": "123456", "description": "验证码"},
|
|
},
|
|
default="",
|
|
)
|
|
phone: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "18888888888", "description": "手机号"},
|
|
},
|
|
default="",
|
|
)
|
|
email: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "admin@example.com", "description": "邮箱"},
|
|
},
|
|
default="",
|
|
)
|
|
autoRegister: bool = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": True, "description": "注册成功自动登录"},
|
|
},
|
|
default=False,
|
|
)
|
|
|
|
Schema: ClassVar[BaseSchema] = BaseSchema # For the type check
|
|
|
|
@validates_schema
|
|
def validate_schema(self, data, **kwargs):
|
|
if not data.get("phone") and not data.get("email"):
|
|
raise ValidationError(
|
|
"至少需要提供一种登录主体(手机号、邮箱)", field_name="custom"
|
|
)
|
|
return data
|
|
|
|
|
|
@dataclass(base_schema=BaseSchema)
|
|
class RegisterRequest:
|
|
username: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "admin", "description": "用户名"},
|
|
},
|
|
default="",
|
|
)
|
|
phone: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "18888888888", "description": "手机号"},
|
|
},
|
|
default="",
|
|
)
|
|
email: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "admin@example.com", "description": "邮箱"},
|
|
},
|
|
default="",
|
|
)
|
|
password: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "123456", "description": "密码"},
|
|
},
|
|
default="",
|
|
)
|
|
realname: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "张三", "description": "真实姓名"},
|
|
},
|
|
default="",
|
|
)
|
|
avatar: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {
|
|
"example": "https://example.com/avatar.jpg",
|
|
"description": "头像",
|
|
},
|
|
},
|
|
default="",
|
|
)
|
|
gender: GenderEnum = field(
|
|
metadata={
|
|
"required": False,
|
|
"by_value": True,
|
|
"metadata": {"example": "secure", "description": "性别"},
|
|
},
|
|
default=GenderEnum.SECURE,
|
|
)
|
|
status: StatusEnum = field(
|
|
metadata={
|
|
"required": False,
|
|
"by_value": True,
|
|
"metadata": {"example": "enabled", "description": "状态"},
|
|
},
|
|
default=StatusEnum.ENABLED,
|
|
)
|
|
code: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "123456", "description": "验证码"},
|
|
},
|
|
default="",
|
|
)
|
|
autoLogin: bool = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": True, "description": "注册成功自动登录"},
|
|
},
|
|
default=False,
|
|
)
|
|
Schema: ClassVar[BaseSchema] = BaseSchema # For the type check
|
|
|
|
@validates_schema
|
|
def validate_schema(self, data, **kwargs):
|
|
if not data.get("username") and not data.get("phone") and not data.get("email"):
|
|
raise ValidationError(
|
|
"至少需要提供一种注册主体(用户名、手机号、邮箱)", field_name="custom"
|
|
)
|
|
if not data.get("password") and not data.get("code"):
|
|
raise ValidationError("密码或验证码不能同时为空", field_name="custom")
|
|
if data.get("code") and not data.get("phone") and not data.get("email"):
|
|
raise ValidationError(
|
|
"验证码注册时,必须提供手机号或邮箱", field_name="custom"
|
|
)
|
|
return data
|
|
|
|
|
|
@dataclass(base_schema=BaseSchema)
|
|
class SendVerificationCodeRequest:
|
|
"""
|
|
发送验证码请求
|
|
"""
|
|
|
|
usage: VerificationCodeUsage = field(
|
|
metadata={
|
|
"required": False,
|
|
"by_value": True,
|
|
"metadata": {"example": "common", "description": "验证码用途"},
|
|
},
|
|
default=VerificationCodeUsage.COMMON,
|
|
)
|
|
phone: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "18888888888", "description": "手机号"},
|
|
},
|
|
default="",
|
|
)
|
|
email: str = field(
|
|
metadata={
|
|
"required": False,
|
|
"metadata": {"example": "admin@example.com", "description": "邮箱"},
|
|
},
|
|
default="",
|
|
)
|
|
Schema: ClassVar[BaseSchema] = BaseSchema # For the type check
|
|
|
|
@validates_schema
|
|
def validate_schema(self, data, **kwargs):
|
|
if not data.get("phone") and not data.get("email"):
|
|
raise ValidationError(
|
|
"至少需要提供一种发送验证码主体(手机号、邮箱)", field_name="custom"
|
|
)
|
|
return data
|