添加授权列表
This commit is contained in:
@@ -20,6 +20,7 @@ from app.api.v1.shot_replicate import router as shot_replicate_router
|
||||
from app.api.v1.test import router as test_router
|
||||
from app.api.v1.user_oauth import router as user_oauth_router
|
||||
from app.api.v1.user_oauth_app import router as user_oauth_app_router
|
||||
from app.api.v1.upload_material import router as upload_material_router
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(auth_router)
|
||||
@@ -42,3 +43,4 @@ api_router.include_router(shot_replicate_router)
|
||||
api_router.include_router(test_router)
|
||||
api_router.include_router(user_oauth_router)
|
||||
api_router.include_router(user_oauth_app_router)
|
||||
api_router.include_router(upload_material_router)
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.dependencies import get_current_user, get_db
|
||||
from app.models.user import User
|
||||
from app.services.upload_material_service import upload_material_to_platform
|
||||
|
||||
router = APIRouter(prefix="/upload-material", tags=["上传素材"])
|
||||
|
||||
|
||||
class UploadTask(BaseModel):
|
||||
advertiser_ids: list[str] = Field(..., description="广告主id数组,支持多条")
|
||||
resource_ids: list[str] = Field(..., description="资源id数组(generated_resources表主键)")
|
||||
oauth_id: str = Field(..., description="授权表id")
|
||||
is_pre_test: Optional[str] = Field(None, description="是否开启前测:是/否/0/1,预留字段")
|
||||
pre_test_template: Optional[str] = Field(None, description="前测模板id,预留字段")
|
||||
|
||||
|
||||
class BatchUploadRequest(BaseModel):
|
||||
tasks: list[UploadTask] = Field(..., description="批量上传任务列表")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/batch-upload",
|
||||
summary="批量上传素材到平台",
|
||||
description="支持批量上传多个授权账户下的资源到素材库,预留下前测功能",
|
||||
)
|
||||
async def batch_upload_material(
|
||||
req: BatchUploadRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
if not req.tasks:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="上传任务列表不能为空",
|
||||
)
|
||||
|
||||
all_results = []
|
||||
total_success = 0
|
||||
total_fail = 0
|
||||
|
||||
for task_index, task in enumerate(req.tasks, 1):
|
||||
task_result = {
|
||||
"task_index": task_index,
|
||||
"oauth_id": task.oauth_id,
|
||||
"advertiser_ids": task.advertiser_ids,
|
||||
"resource_ids": task.resource_ids,
|
||||
"is_pre_test": task.is_pre_test,
|
||||
"pre_test_template": task.pre_test_template,
|
||||
"result": None,
|
||||
}
|
||||
|
||||
try:
|
||||
if not task.advertiser_ids:
|
||||
raise ValueError("广告主id数组不能为空")
|
||||
if not task.resource_ids:
|
||||
raise ValueError("资源id数组不能为空")
|
||||
|
||||
result = await upload_material_to_platform(
|
||||
task.resource_ids,
|
||||
task.advertiser_ids,
|
||||
task.oauth_id,
|
||||
db,
|
||||
current_user.id,
|
||||
)
|
||||
|
||||
task_result["result"] = {
|
||||
"success": True,
|
||||
**result,
|
||||
}
|
||||
total_success += result["success_count"]
|
||||
total_fail += result["fail_count"]
|
||||
|
||||
if task.is_pre_test and task.is_pre_test in ["是", "1", "true", True]:
|
||||
task_result["pre_test_reserved"] = {
|
||||
"status": "reserved",
|
||||
"message": "前测功能已预留,待后续开通",
|
||||
"template": task.pre_test_template,
|
||||
}
|
||||
|
||||
except ValueError as e:
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
}
|
||||
total_fail += len(task.resource_ids) * len(task.advertiser_ids)
|
||||
except Exception as e:
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": f"上传失败: {str(e)}",
|
||||
}
|
||||
total_fail += len(task.resource_ids) * len(task.advertiser_ids)
|
||||
|
||||
all_results.append(task_result)
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "批量上传完成",
|
||||
"summary": {
|
||||
"total_tasks": len(req.tasks),
|
||||
"total_success_count": total_success,
|
||||
"total_fail_count": total_fail,
|
||||
"total_requested": sum(len(t.resource_ids) * len(t.advertiser_ids) for t in req.tasks),
|
||||
},
|
||||
"details": all_results,
|
||||
}
|
||||
@@ -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