feat: users create type

This commit is contained in:
xtaodada 2024-11-13 14:27:50 +08:00
parent 918ab92940
commit dbc3885f9f
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
8 changed files with 140 additions and 2 deletions

View File

@ -0,0 +1,30 @@
"""users_create_type
Revision ID: d24d89a9d9fa
Revises: a41d2f3443c8
Create Date: 2024-11-13 14:04:54.681837
"""
from alembic import op
import sqlalchemy as sa
import sqlmodel
# revision identifiers, used by Alembic.
revision = "d24d89a9d9fa"
down_revision = "a41d2f3443c8"
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("auth_user", sa.Column("create_type", sa.Integer(), nullable=True))
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("auth_user", "create_type")
# ### end Alembic commands ###

View File

@ -32,6 +32,19 @@
<label class="c-label">真实姓名:</label> <label class="c-label">真实姓名:</label>
<el-input v-model="p.real_name" placeholder="模糊查询"></el-input> <el-input v-model="p.real_name" placeholder="模糊查询"></el-input>
</div> </div>
<br />
<div class="c-item">
<label class="c-label">邮箱:</label>
<el-input v-model="p.email" placeholder="模糊查询"></el-input>
</div>
<div class="c-item">
<label class="c-label">手机号:</label>
<el-input v-model="p.phone" placeholder="模糊查询"></el-input>
</div>
<div class="c-item">
<label class="c-label">学号:</label>
<el-input v-model="p.student_id" placeholder="模糊查询"></el-input>
</div>
<div class="c-item"> <div class="c-item">
<label class="c-label">注册日期:</label> <label class="c-label">注册日期:</label>
<el-date-picker v-model="p.start_time" type="date" value-format="yyyy-MM-dd" placeholder="开始日期"></el-date-picker> - <el-date-picker v-model="p.start_time" type="date" value-format="yyyy-MM-dd" placeholder="开始日期"></el-date-picker> -
@ -41,6 +54,17 @@
<el-button type="primary" icon="el-icon-search" @click="p.pageNo = 1; f5()">查询</el-button> <el-button type="primary" icon="el-icon-search" @click="p.pageNo = 1; f5()">查询</el-button>
</div> </div>
<br /> <br />
<div class="c-item s-radio-text">
<label class="c-label">注册类型:</label>
<el-radio-group v-model="p.create_type">
<el-radio :label="0">所有</el-radio>
<el-radio :label="1">学号注册</el-radio>
<el-radio :label="2">手机号注册</el-radio>
<el-radio :label="3">管理员手动添加</el-radio>
<el-radio :label="4">管理员批量导入</el-radio>
</el-radio-group>
</div>
<br />
<div class="c-item s-radio-text"> <div class="c-item s-radio-text">
<label class="c-label">综合排序:</label> <label class="c-label">综合排序:</label>
<el-radio-group v-model="p.sortType"> <el-radio-group v-model="p.sortType">
@ -122,6 +146,9 @@
username: '', username: '',
nickname: '', nickname: '',
real_name: '', real_name: '',
email: '',
phone: '',
student_id: '',
create_type: 0, create_type: 0,
sortType: 1, sortType: 1,
start_time: new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-1', // 本月一号 start_time: new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-1', // 本月一号

View File

@ -16,6 +16,7 @@ from src.services.users.schemas import (
SystemUserEnum, SystemUserEnum,
UserLoginOut, UserLoginOut,
UserRoleEnum, UserRoleEnum,
CreateTypeEnum,
) )
from src.services.users.services import UserServices, UserRoleServices from src.services.users.services import UserServices, UserRoleServices
@ -40,6 +41,7 @@ class UserRoutes(Plugin):
if user: if user:
return BaseApiOut(status=500, msg="用户名已被注册") return BaseApiOut(status=500, msg="用户名已被注册")
role = UserRoleEnum.STUDENT.value role = UserRoleEnum.STUDENT.value
create_type = CreateTypeEnum.STUDENT
if not (data.student_id or data.phone): if not (data.student_id or data.phone):
return BaseApiOut(status=500, msg="学号或手机号至少填写一项") return BaseApiOut(status=500, msg="学号或手机号至少填写一项")
if data.student_id: if data.student_id:
@ -47,11 +49,13 @@ class UserRoutes(Plugin):
if user: if user:
return BaseApiOut(status=500, msg="学号已被注册") return BaseApiOut(status=500, msg="学号已被注册")
role = UserRoleEnum.STUDENT.value role = UserRoleEnum.STUDENT.value
create_type = CreateTypeEnum.STUDENT
if data.phone: if data.phone:
user = await self.user_services.get_user(phone=data.phone) user = await self.user_services.get_user(phone=data.phone)
if user: if user:
return BaseApiOut(status=500, msg="手机号已被注册") return BaseApiOut(status=500, msg="手机号已被注册")
role = UserRoleEnum.OUT.value role = UserRoleEnum.OUT.value
create_type = CreateTypeEnum.PHONE
# 检查通过,注册用户 # 检查通过,注册用户
try: try:
user = await self.user_services.register_user( user = await self.user_services.register_user(
@ -60,6 +64,7 @@ class UserRoutes(Plugin):
student_id=data.student_id, student_id=data.student_id,
phone=data.phone, phone=data.phone,
real_name=data.real_name, real_name=data.real_name,
create_type=create_type.value,
) )
if not await self.user_role_services.is_user_in_role_group( if not await self.user_role_services.is_user_in_role_group(
data.username, role data.username, role

View File

@ -20,6 +20,8 @@ class UserRoutes(Plugin):
@handler.post("/list", student=True) @handler.post("/list", student=True)
async def get_user_list(self, data: UserList): async def get_user_list(self, data: UserList):
username, nickname, real_name = data.username, data.nickname, data.real_name username, nickname, real_name = data.username, data.nickname, data.real_name
email, phone, student_id = data.email, data.phone, data.student_id
create_type = data.create_type.value
start_time, end_time = data.start, data.end start_time, end_time = data.start, data.end
page_no, page_size = data.pageNo, data.pageSize page_no, page_size = data.pageNo, data.pageSize
if page_no < 1: if page_no < 1:
@ -31,6 +33,10 @@ class UserRoutes(Plugin):
username, username,
nickname, nickname,
real_name, real_name,
email,
phone,
student_id,
create_type,
start_time, start_time,
end_time, end_time,
page_no, page_no,

View File

@ -11,6 +11,7 @@ __all__ = [
"PhoneMixin", "PhoneMixin",
"RealNameMixin", "RealNameMixin",
"SexMixin", "SexMixin",
"CreateTypeMixin",
] ]
@ -30,7 +31,28 @@ class SexMixin(SQLModel):
sex: Optional[str] = Field("", title="性别", max_length=15) sex: Optional[str] = Field("", title="性别", max_length=15)
class CreateTypeMixin(SQLModel):
"""
1. 学号注册
2. 手机号注册
3. 管理员手动添加
4. 管理员批量导入
"""
create_type: Optional[int] = Field(
default=None,
title="create_type",
nullable=True,
)
class UserModel( class UserModel(
BaseUser, StudentIdMixin, PhoneMixin, RealNameMixin, SexMixin, table=True BaseUser,
StudentIdMixin,
PhoneMixin,
RealNameMixin,
SexMixin,
CreateTypeMixin,
table=True,
): ):
__table_args__ = {"extend_existing": True} __table_args__ = {"extend_existing": True}

View File

@ -46,6 +46,7 @@ class UserRepo(AsyncInitializingComponent):
student_id: Optional[str], student_id: Optional[str],
phone: Optional[str], phone: Optional[str],
real_name: Optional[str], real_name: Optional[str],
create_type: int,
): ):
password = self.AUTH.pwd_context.hash(password.get_secret_value()) password = self.AUTH.pwd_context.hash(password.get_secret_value())
values = { values = {
@ -54,6 +55,7 @@ class UserRepo(AsyncInitializingComponent):
"student_id": student_id, "student_id": student_id,
"phone": phone, "phone": phone,
"real_name": real_name, "real_name": real_name,
"create_type": create_type,
} }
user = self.user_model.model_validate(values) user = self.user_model.model_validate(values)
async with AsyncSession(self.engine) as session: async with AsyncSession(self.engine) as session:
@ -153,6 +155,10 @@ class UserRepo(AsyncInitializingComponent):
username: str, username: str,
nickname: str, nickname: str,
real_name: str, real_name: str,
email: str,
phone: str,
student_id: str,
create_type: int,
start_time: datetime, start_time: datetime,
end_time: datetime, end_time: datetime,
page_no: int, page_no: int,
@ -172,6 +178,20 @@ class UserRepo(AsyncInitializingComponent):
statement = statement.where( statement = statement.where(
col(self.user_model.real_name).like(f"%{real_name}%") col(self.user_model.real_name).like(f"%{real_name}%")
) )
if email:
statement = statement.where(
col(self.user_model.email).like(f"%{email}%")
)
if phone:
statement = statement.where(
col(self.user_model.phone).like(f"%{phone}%")
)
if student_id:
statement = statement.where(
col(self.user_model.student_id).like(f"%{student_id}%")
)
if create_type:
statement = statement.where(self.user_model.create_type == create_type)
if start_time: if start_time:
statement = statement.where(self.user_model.create_time >= start_time) statement = statement.where(self.user_model.create_time >= start_time)
if end_time: if end_time:

View File

@ -110,11 +110,23 @@ class UserRoleEnum(str, Enum):
OUT = "out" # 校外人员 OUT = "out" # 校外人员
class CreateTypeEnum(int, Enum):
ALL = 0
STUDENT = 1
PHONE = 2
ADMIN = 3
ADMIN_MULT = 4
class UserList(BaseModel): class UserList(BaseModel):
username: str = "" username: str = ""
nickname: str = "" nickname: str = ""
real_name: str = "" real_name: str = ""
email: str = ""
phone: str = ""
student_id: str = ""
create_type: CreateTypeEnum = CreateTypeEnum.ALL
sortType: int = 1 sortType: int = 1
start_time: str start_time: str
end_time: str end_time: str

View File

@ -26,6 +26,7 @@ class UserServices(AsyncInitializingComponent):
student_id: Optional[str], student_id: Optional[str],
phone: Optional[str], phone: Optional[str],
real_name: Optional[str], real_name: Optional[str],
create_type: int = 0,
): ):
return await self.repo.register_user( return await self.repo.register_user(
username, username,
@ -33,6 +34,7 @@ class UserServices(AsyncInitializingComponent):
student_id, student_id,
phone, phone,
real_name, real_name,
create_type,
) )
async def login_user(self, user: "UserModel") -> str: async def login_user(self, user: "UserModel") -> str:
@ -102,13 +104,27 @@ class UserServices(AsyncInitializingComponent):
username: str, username: str,
nickname: str, nickname: str,
real_name: str, real_name: str,
email: str,
phone: str,
student_id: str,
create_type: int,
start_time: datetime, start_time: datetime,
end_time: datetime, end_time: datetime,
page_no: int, page_no: int,
page_size: int, page_size: int,
) -> Tuple[Sequence[UserModel], int]: ) -> Tuple[Sequence[UserModel], int]:
return await self.repo.get_user_list( return await self.repo.get_user_list(
username, nickname, real_name, start_time, end_time, page_no, page_size username,
nickname,
real_name,
email,
phone,
student_id,
create_type,
start_time,
end_time,
page_no,
page_size,
) )