From c7d5be908e24ee7677140c1cf983836f2e413cf0 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期二, 10 十二月 2024 18:26:20 +0800 Subject: [PATCH] 会话菜单增加 --- app/service/auth.py | 38 ++++++++++++++++++++++---------------- 1 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/service/auth.py b/app/service/auth.py index d843adb..46c42dd 100644 --- a/app/service/auth.py +++ b/app/service/auth.py @@ -9,6 +9,7 @@ from Log import logger from app.config.config import settings +from app.models import RoleModel, GroupModel from app.models.user_model import UserModel, UserAppModel SECRET_KEY = settings.secret_key @@ -71,29 +72,30 @@ db_user = UserModel(username=username, hashed_password=hashed_password, email=email) pwd = db_user.encrypted_password(password) db_user.password = pwd - db.add(db_user) + db_user.roles = [db.query(RoleModel).filter(RoleModel.role_type == 2).first()] + db_user.groups = [db.query(GroupModel).filter(GroupModel.group_type == 2).first()] db.add(db_user) db.commit() db.refresh(db_user) user_id = db_user.id for k, v in register_dict.items(): - UserAppDao(db).update_and_insert_token(v.get("name"), pwd, v.get("email"), user_id, str(v.get("id")), k) + await UserAppDao(db).update_and_insert_data(v.get("name"), pwd, v.get("email"), user_id, str(v.get("id")), k) except Exception as e: logger.error(e) # db.roolback() if user_id: db.query(UserModel).filter(UserModel.id == user_id).delete() + db.commit return False return True async def update_user_token(db, user_id, token_dict): - try: for k, v in token_dict.items(): - UserAppDao(db).update_user_app_data({"user_id": user_id, "app_type": k}, - {"access_token": v, "token_at": datetime.now()}) + await UserAppDao(db).update_user_app_data({"user_id": user_id, "app_type": k}, + {"access_token": v, "token_at": datetime.now()}) except Exception as e: logger.error(e) @@ -105,11 +107,11 @@ def __init__(self, db: Session): self.db = db - def get_data_by_id(self, user_id: int, app_type: int) -> Type[UserAppModel] | None: + async def get_data_by_id(self, user_id: int, app_type: int) -> Type[UserAppModel] | None: session = self.db.query(UserAppModel).filter_by(user_id=user_id, app_type=app_type).first() return session - def update_user_app_data(self, query: int, update_data: str): + async def update_user_app_data(self, query: dict, update_data: dict): logger.error("鏇存柊鏁版嵁df update_app_data---------------------------") try: @@ -120,7 +122,8 @@ self.db.rollback() raise Exception("鏇存柊澶辫触锛�") - def insert_user_app_data(self, username: str, password: str, email: str, user_id: int, app_id: str, app_type: int): + async def insert_user_app_data(self, username: str, password: str, email: str, user_id: int, app_id: str, + app_type: int): logger.error("鏂板鏁版嵁df insert_user_app_data---------------------------") new_session = UserAppModel( username=username, @@ -135,15 +138,18 @@ self.db.refresh(new_session) return new_session - def update_and_insert_token(self, username: str, password: str, email: str, user_id: int, app_id: str, - app_type: int): + async def update_and_insert_data(self, username: str, password: str, email: str, user_id: int, app_id: str, + app_type: int): logger.error("鏇存柊鎴栬�呮坊鍔犳暟鎹� update_and_insert_token---------------------------") - token_boj = self.get_data_by_id(user_id, app_type) + token_boj = await self.get_data_by_id(user_id, app_type) if token_boj: - self.update_user_app_data({"id": token_boj.id}, {"username": username, - "password": password, "email": email, "username": username, - "updated_at": datetime.now(), - }) + await self.update_user_app_data({"id": token_boj.id}, {"username": username, + "password": password, "email": email, + "updated_at": datetime.now(), + }) else: - self.insert_user_app_data(username, password, email, user_id, app_id, app_type) + await self.insert_user_app_data(username, password, email, user_id, app_id, app_type) + + async def get_user_datas(self, user_id: int): + return self.db.query(UserAppModel).filter_by(user_id=user_id).all() -- Gitblit v1.8.0