添加用户登录接口
This commit is contained in:
parent
8f000fe60a
commit
3a271cd11a
|
|
@ -6,12 +6,18 @@
|
||||||
|
|
||||||
from fastapi import APIRouter, Body, Depends, Query
|
from fastapi import APIRouter, Body, Depends, Query
|
||||||
from typing_extensions import Annotated
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
from config import settings
|
||||||
|
from config.factory import AutomationResponse
|
||||||
from entity.user_entity import UserFrom
|
from entity.user_entity import UserFrom
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from config.db_session import get_db
|
from config.db_session import get_db
|
||||||
|
from repository.user_repository import login_user
|
||||||
from service.user_service import user_service
|
from service.user_service import user_service
|
||||||
|
from utils.jwt_token import create_token
|
||||||
from utils.response import success_200,error_211
|
from utils.response import success_200,error_211
|
||||||
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
|
from datetime import timedelta
|
||||||
router = APIRouter(prefix="/api", tags=["用户模块"])
|
router = APIRouter(prefix="/api", tags=["用户模块"])
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -22,3 +28,12 @@ def register(user: Annotated[UserFrom, Body(...)], db: Session = Depends(get_db)
|
||||||
return success_200(data=user, message="用户注册成功")
|
return success_200(data=user, message="用户注册成功")
|
||||||
return error_211(message=user)
|
return error_211(message=user)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/login", summary="用户登录", name="用户登录")
|
||||||
|
def login(user: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
|
||||||
|
user = login_user(db, user.username, user.password)
|
||||||
|
if user[0]:
|
||||||
|
expire_time = timedelta(minutes=settings.ACCESS.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
|
token = create_token(AutomationResponse.model_to_dict(user[1], "password"), expire_time)
|
||||||
|
return {"code": 200, "message": "登录成功", "access_token": token, "user": user[1], "token_type": "Bearer"}
|
||||||
|
return error_211(message=user[1])
|
||||||
|
|
|
||||||
|
|
@ -372,3 +372,35 @@
|
||||||
2022-05-29 11:57:02 | MainThread:8676804096 | <module>:__init__:22 - INFO - loading environment configuration file
|
2022-05-29 11:57:02 | MainThread:8676804096 | <module>:__init__:22 - INFO - loading environment configuration file
|
||||||
2022-05-29 11:57:36 | MainThread:8639112704 | <module>:__init__:16 - INFO - start the automation service development environment
|
2022-05-29 11:57:36 | MainThread:8639112704 | <module>:__init__:16 - INFO - start the automation service development environment
|
||||||
2022-05-29 11:57:36 | MainThread:8639112704 | <module>:__init__:22 - INFO - loading environment configuration file
|
2022-05-29 11:57:36 | MainThread:8639112704 | <module>:__init__:22 - INFO - loading environment configuration file
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | <module>:__init__:16 - INFO - start the automation service development environment
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | <module>:__init__:22 - INFO - loading environment configuration file
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:15 - INFO - loading application configuration
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:16 - INFO - create FastApi app object
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:25 - INFO - Adding a New route success
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:26 - INFO - Start registering middleware
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:36 - INFO - Application started successfully:CORSMiddleware
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:37 - INFO - Server address http://192.168.1.107:8000
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:38 - INFO - Api doc address http://192.168.1.107:8000/docs
|
||||||
|
2022-05-29 11:58:43 | MainThread:8643393024 | create_app:app:39 - INFO - Api redoc address http://192.168.1.107:8000/redoc
|
||||||
|
2022-05-29 11:58:44 | MainThread:8627394048 | <module>:__init__:16 - INFO - start the automation service development environment
|
||||||
|
2022-05-29 11:58:44 | MainThread:8627394048 | <module>:__init__:22 - INFO - loading environment configuration file
|
||||||
|
2022-05-29 11:58:44 | MainThread:8627394048 | create_app:app:15 - INFO - loading application configuration
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:16 - INFO - create FastApi app object
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:25 - INFO - Adding a New route success
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:26 - INFO - Start registering middleware
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:36 - INFO - Application started successfully:CORSMiddleware
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:37 - INFO - Server address http://192.168.1.107:8000
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:38 - INFO - Api doc address http://192.168.1.107:8000/docs
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:39 - INFO - Api redoc address http://192.168.1.107:8000/redoc
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:15 - INFO - loading application configuration
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:16 - INFO - create FastApi app object
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:25 - INFO - Adding a New route success
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:26 - INFO - Start registering middleware
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:36 - INFO - Application started successfully:CORSMiddleware
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:37 - INFO - Server address http://192.168.1.107:8000
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:38 - INFO - Api doc address http://192.168.1.107:8000/docs
|
||||||
|
2022-05-29 11:58:45 | MainThread:8627394048 | create_app:app:39 - INFO - Api redoc address http://192.168.1.107:8000/redoc
|
||||||
|
2022-05-29 12:10:12 | MainThread:8635991552 | <module>:__init__:16 - INFO - start the automation service development environment
|
||||||
|
2022-05-29 12:10:12 | MainThread:8635991552 | <module>:__init__:22 - INFO - loading environment configuration file
|
||||||
|
2022-05-29 12:10:17 | MainThread:8604014080 | <module>:__init__:16 - INFO - start the automation service development environment
|
||||||
|
2022-05-29 12:10:17 | MainThread:8604014080 | <module>:__init__:22 - INFO - loading environment configuration file
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,9 @@ from sqlalchemy.orm import Session
|
||||||
from config.db_session import db_add
|
from config.db_session import db_add
|
||||||
from config.factory import AutomationResponse
|
from config.factory import AutomationResponse
|
||||||
from entity.user_entity import UserFrom
|
from entity.user_entity import UserFrom
|
||||||
|
from enums.enums import RegisterUser
|
||||||
from models.model import User
|
from models.model import User
|
||||||
|
from utils.jwt_token import get_md5_pwd
|
||||||
|
|
||||||
|
|
||||||
def check_user_email(db: Session, user: UserFrom) -> bool:
|
def check_user_email(db: Session, user: UserFrom) -> bool:
|
||||||
|
|
@ -19,7 +21,15 @@ def check_user_email(db: Session, user: UserFrom) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def register_user(db: Session, user: UserFrom)->dict:
|
def register_user(db: Session, user: UserFrom) -> dict:
|
||||||
us_er = User(**user.dict())
|
us_er = User(**user.dict())
|
||||||
db_add(db, us_er)
|
db_add(db, us_er)
|
||||||
return AutomationResponse.model_to_dict(us_er, "password")
|
return AutomationResponse.model_to_dict(us_er, "password")
|
||||||
|
|
||||||
|
|
||||||
|
def login_user(db: Session, username, password):
|
||||||
|
password = get_md5_pwd(password)
|
||||||
|
user = db.query(User).filter(User.username == username, User.password == password).first()
|
||||||
|
if user:
|
||||||
|
return True, user
|
||||||
|
return False, RegisterUser.LOGIN_USER_ERROR.value
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue