添加授权列表
This commit is contained in:
@@ -10,8 +10,9 @@ from app.schemas.user_oauth import RequestOAuthRequest, RequestOAuthResponse, Us
|
||||
from app.services.user_oauth_service import (
|
||||
build_oauth_url,
|
||||
get_token,
|
||||
get_oauth_list,
|
||||
)
|
||||
from app.tasks.user_oauth_tasks import update_oauth_accounts
|
||||
from app.tasks.user_oauth_tasks import _update_oauth_accounts
|
||||
|
||||
router = APIRouter(prefix="/user-oauth", tags=["oauth"])
|
||||
|
||||
@@ -28,15 +29,17 @@ async def request_oauth(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
if req.open_type not in [1,2,3,4,5,6,7,8,9,10]:
|
||||
raise ValueError("open_type must be in [1,2,3,4,5,6,7,8,9,10]")
|
||||
|
||||
auth_url = await build_oauth_url(req.open_type, current_user.id, db)
|
||||
return {"auth_url": auth_url}
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=str(e),
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/juliang_callback",
|
||||
summary="巨量授权回调",
|
||||
@@ -102,64 +105,62 @@ async def juliang_callback(
|
||||
)
|
||||
|
||||
@router.get(
|
||||
"/update_account",
|
||||
summary="更新权限下的所有账户",
|
||||
description="用户提交授权登录账户id,或者授权id",
|
||||
"/oauth_list",
|
||||
summary="获取账户下所有授权列表",
|
||||
description="获取当前用户下所有授权账户列表,支持按授权登录账号、开户方式、授权账户id筛选",
|
||||
)
|
||||
async def update_account(
|
||||
account_id: str | None = Query(None, description="授权账户id"),
|
||||
async def oauth_list(
|
||||
account_userid: str | None = Query(None, description="授权登录账号id"),
|
||||
open_type: int | None = Query(None, description="开户方式open_type"),
|
||||
account_id: str | None = Query(None, description="授权账户id"),
|
||||
page: int = Query(1, description="页码"),
|
||||
page_size: int = Query(10, description="每页数量"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
if not account_id and not account_userid:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="请提交授权账户id或授权登录账号id",
|
||||
)
|
||||
#获取单独授权账号
|
||||
if account_id:
|
||||
exist = await db.execute(
|
||||
select(UserOAuth).where(
|
||||
UserOAuth.account_id == account_id,
|
||||
UserOAuth.user_id == current_user.id,
|
||||
UserOAuth.deleted_at.is_(None),
|
||||
).limit(1)
|
||||
)
|
||||
exist = exist.scalar_one_or_none()
|
||||
if not exist:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="授权账户不存在",
|
||||
)
|
||||
#获取授权登录账号的所有账号
|
||||
if account_userid:
|
||||
exist = await db.execute(
|
||||
select(UserOAuth).where(
|
||||
UserOAuth.account_userid == account_userid,
|
||||
UserOAuth.user_id == current_user.id,
|
||||
UserOAuth.deleted_at.is_(None),
|
||||
).limit(1)
|
||||
)
|
||||
exist = exist.scalar_one_or_none()
|
||||
if not exist:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="授权登录账号不存在",
|
||||
)
|
||||
await update_oauth_accounts(account_id, account_userid, current_user.id, db)
|
||||
return {"message": "提交成功,等待处理", "code": 0}
|
||||
result = await get_oauth_list(
|
||||
user_id=current_user.id,
|
||||
db=db,
|
||||
account_userid=account_userid,
|
||||
open_type=open_type,
|
||||
account_id=account_id,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "查询成功",
|
||||
"data": [
|
||||
{
|
||||
"id": oauth.id,
|
||||
"account_id": oauth.account_id,
|
||||
"account_name": oauth.account_name,
|
||||
"account_role": oauth.account_role,
|
||||
"account_username": oauth.account_username,
|
||||
"user_id": oauth.user_id,
|
||||
"open_type": oauth.open_type,
|
||||
"port_type": oauth.port_type,
|
||||
"appid": oauth.appid,
|
||||
"material_auth_status": oauth.material_auth_status,
|
||||
"created_at": oauth.created_at,
|
||||
"updated_at": oauth.updated_at,
|
||||
}
|
||||
for oauth in result["data"]
|
||||
],
|
||||
"pagination": {
|
||||
"page": result["page"],
|
||||
"page_size": result["page_size"],
|
||||
"total": result["total"],
|
||||
"total_pages": (result["total"] + result["page_size"] - 1) // result["page_size"],
|
||||
},
|
||||
}
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e),
|
||||
)
|
||||
except HTTPException as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=str(e),
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
|
||||
Reference in New Issue
Block a user