添加授权列表
This commit is contained in:
@@ -0,0 +1,310 @@
|
||||
import os
|
||||
import hashlib
|
||||
import base64
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.generated_resource import GeneratedResource
|
||||
from app.models.user_oauth import UserOAuth
|
||||
from app.models.resources_material import ResourcesMaterial
|
||||
from app.utils.id_gen import generate_id
|
||||
from app.utils.douyinApi import DouyinApi
|
||||
|
||||
|
||||
douyin_api = DouyinApi()
|
||||
|
||||
|
||||
async def upload_material_to_platform(
|
||||
resource_ids: list[str],
|
||||
advertiser_ids: list[str],
|
||||
oauth_id: str,
|
||||
db: AsyncSession,
|
||||
current_user_id: str,
|
||||
) -> any:
|
||||
oauth = await db.execute(
|
||||
select(UserOAuth).where(
|
||||
UserOAuth.id == oauth_id,
|
||||
UserOAuth.deleted_at.is_(None),
|
||||
UserOAuth.user_id == current_user_id,
|
||||
)
|
||||
)
|
||||
oauth = oauth.scalar_one_or_none()
|
||||
if not oauth:
|
||||
raise ValueError("授权记录不存在")
|
||||
|
||||
port_type = oauth.port_type
|
||||
access_token = oauth.access_token
|
||||
|
||||
if not access_token:
|
||||
raise ValueError("授权token不存在")
|
||||
|
||||
resources = await db.execute(
|
||||
select(GeneratedResource).where(
|
||||
GeneratedResource.id.in_(resource_ids),
|
||||
GeneratedResource.user_id == current_user_id,
|
||||
GeneratedResource.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
resources = resources.scalars().all()
|
||||
|
||||
resource_dict = {r.id: r for r in resources}
|
||||
for resource_id in resource_ids:
|
||||
if resource_id not in resource_dict:
|
||||
raise ValueError(f"资源 {resource_id} 不存在或不属于当前用户")
|
||||
|
||||
all_upload_results = []
|
||||
success_count = 0
|
||||
fail_count = 0
|
||||
|
||||
for resource_id in resource_ids:
|
||||
resource = resource_dict[resource_id]
|
||||
resource_type = resource.resource_type
|
||||
storage_path = resource.storage_path
|
||||
|
||||
if resource_type not in ["image", "video"]:
|
||||
raise ValueError(f"资源 {resource_id} 不支持的资源类型,仅支持image和video")
|
||||
|
||||
if not storage_path:
|
||||
raise ValueError(f"资源 {resource_id} 本地存储路径为空")
|
||||
|
||||
for advertiser_id in advertiser_ids:
|
||||
result = await _upload_to_juliang(
|
||||
oauth_id, storage_path, resource_type, advertiser_id, resource, db, current_user_id
|
||||
)
|
||||
all_upload_results.append(result)
|
||||
if result.get("success"):
|
||||
success_count += 1
|
||||
else:
|
||||
fail_count += 1
|
||||
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"success_count": success_count,
|
||||
"fail_count": fail_count,
|
||||
"total_count": len(resource_ids) * len(advertiser_ids),
|
||||
"results": all_upload_results,
|
||||
}
|
||||
|
||||
|
||||
async def _upload_to_juliang(
|
||||
oauth_id: str,
|
||||
storage_path: str,
|
||||
resource_type: str,
|
||||
advertiser_id: str,
|
||||
resource: GeneratedResource,
|
||||
db: AsyncSession,
|
||||
current_user_id: str,
|
||||
) -> any:
|
||||
filename = os.path.basename(storage_path)
|
||||
|
||||
if resource_type == "image":
|
||||
if resource.file_size_bytes > 5 * 1024 * 1024:
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": False,
|
||||
"error": "图片大小超过5M,不支持上传",
|
||||
}
|
||||
|
||||
with open(storage_path, "rb") as f:
|
||||
file_content = f.read()
|
||||
image_signature = hashlib.md5(file_content).hexdigest()
|
||||
|
||||
data = {
|
||||
"advertiser_id": advertiser_id,
|
||||
"upload_type": "UPLOAD_BY_FILE",
|
||||
"image_signature": image_signature,
|
||||
"filename": filename,
|
||||
}
|
||||
|
||||
files = {
|
||||
"image_file": (filename, file_content, "image/png"),
|
||||
}
|
||||
|
||||
response = await douyin_api.upload_image_material(oauth_id, data, files)
|
||||
|
||||
if response["code"] != 0:
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": False,
|
||||
"error": response.get("message", "上传失败"),
|
||||
}
|
||||
|
||||
image_id = response["data"]["id"]
|
||||
material_id = response["data"]["material_id"]
|
||||
|
||||
existing = await db.execute(
|
||||
select(ResourcesMaterial).where(
|
||||
ResourcesMaterial.oauth_id == oauth_id,
|
||||
ResourcesMaterial.advertiser_id == advertiser_id,
|
||||
ResourcesMaterial.material_id == str(material_id),
|
||||
ResourcesMaterial.upload_id == str(image_id),
|
||||
ResourcesMaterial.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
existing = existing.scalar_one_or_none()
|
||||
|
||||
if existing:
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": True,
|
||||
"material_id": str(material_id),
|
||||
"upload_id": str(image_id),
|
||||
"message": "素材已存在,跳过添加",
|
||||
}
|
||||
|
||||
db.add(ResourcesMaterial(
|
||||
id=generate_id(),
|
||||
oauth_id=oauth_id,
|
||||
advertiser_id=advertiser_id,
|
||||
target_table="generated_resources",
|
||||
target_id=resource.id,
|
||||
material_id=str(material_id),
|
||||
upload_id=str(image_id),
|
||||
resource_type=resource_type,
|
||||
user_id=current_user_id,
|
||||
))
|
||||
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": True,
|
||||
"material_id": str(material_id),
|
||||
"upload_id": str(image_id),
|
||||
}
|
||||
|
||||
elif resource_type == "video":
|
||||
if resource.file_size_bytes > 500 * 1024 * 1024:
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": False,
|
||||
"error": "视频大小超过500M,不支持上传",
|
||||
}
|
||||
|
||||
with open(storage_path, "rb") as f:
|
||||
file_content = f.read()
|
||||
video_signature = hashlib.md5(file_content).hexdigest()
|
||||
|
||||
data = {
|
||||
"advertiser_id": advertiser_id,
|
||||
"upload_type": "UPLOAD_BY_FILE",
|
||||
"video_signature": video_signature,
|
||||
"filename": filename,
|
||||
}
|
||||
|
||||
files = {
|
||||
"video_file": (filename, file_content, "video/mp4"),
|
||||
}
|
||||
|
||||
response = await douyin_api.upload_video_material(oauth_id, data, files)
|
||||
|
||||
if response["code"] != 0:
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": False,
|
||||
"error": response.get("message", "上传失败"),
|
||||
}
|
||||
|
||||
video_id = response["data"]["video_id"]
|
||||
material_id = response["data"]["material_id"]
|
||||
|
||||
existing = await db.execute(
|
||||
select(ResourcesMaterial).where(
|
||||
ResourcesMaterial.oauth_id == oauth_id,
|
||||
ResourcesMaterial.advertiser_id == advertiser_id,
|
||||
ResourcesMaterial.material_id == str(material_id),
|
||||
ResourcesMaterial.upload_id == str(video_id),
|
||||
ResourcesMaterial.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
existing = existing.scalar_one_or_none()
|
||||
|
||||
if existing:
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": True,
|
||||
"material_id": str(material_id),
|
||||
"upload_id": str(video_id),
|
||||
"message": "素材已存在,跳过添加",
|
||||
}
|
||||
|
||||
db.add(ResourcesMaterial(
|
||||
id=generate_id(),
|
||||
oauth_id=oauth_id,
|
||||
advertiser_id=advertiser_id,
|
||||
target_table="generated_resources",
|
||||
target_id=resource.id,
|
||||
material_id=str(material_id),
|
||||
upload_id=str(video_id),
|
||||
resource_type=resource_type,
|
||||
user_id=current_user_id,
|
||||
))
|
||||
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": True,
|
||||
"material_id": str(material_id),
|
||||
"upload_id": str(video_id),
|
||||
}
|
||||
|
||||
else:
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": False,
|
||||
"error": f"不支持的资源类型: {resource_type}",
|
||||
}
|
||||
|
||||
|
||||
async def _upload_to_kuaishou(
|
||||
access_token: str,
|
||||
storage_path: str,
|
||||
resource_type: str,
|
||||
advertiser_id: str,
|
||||
resource: GeneratedResource,
|
||||
db: AsyncSession,
|
||||
current_user_id: str,
|
||||
) -> dict:
|
||||
filename = os.path.basename(storage_path)
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": False,
|
||||
"error": "快手平台上传接口暂未开通",
|
||||
}
|
||||
|
||||
|
||||
async def _upload_to_tencent(
|
||||
access_token: str,
|
||||
storage_path: str,
|
||||
resource_type: str,
|
||||
advertiser_id: str,
|
||||
resource: GeneratedResource,
|
||||
db: AsyncSession,
|
||||
current_user_id: str,
|
||||
) -> dict:
|
||||
filename = os.path.basename(storage_path)
|
||||
return {
|
||||
"resource_id": resource.id,
|
||||
"advertiser_id": advertiser_id,
|
||||
"filename": filename,
|
||||
"success": False,
|
||||
"error": "腾讯平台上传接口暂未开通",
|
||||
}
|
||||
@@ -75,7 +75,7 @@ async def create_user_oauth_app(
|
||||
app_id=app_id,
|
||||
secret=secret,
|
||||
open_type=open_type,
|
||||
count=count,
|
||||
max_count=count,
|
||||
auth_url=auth_url,
|
||||
company=company,
|
||||
create_by=create_by,
|
||||
@@ -109,7 +109,7 @@ async def update_user_oauth_app(
|
||||
if status is not None:
|
||||
app.status = status
|
||||
if count is not None:
|
||||
app.count = count
|
||||
app.max_count = count
|
||||
if auth_url is not None:
|
||||
app.auth_url = auth_url
|
||||
if company is not None:
|
||||
|
||||
@@ -10,20 +10,6 @@ from app.models.user_oauth import UserOAuth
|
||||
from app.models.user_oauth_app import UserOAuthApp
|
||||
from app.utils.id_gen import generate_id
|
||||
|
||||
|
||||
OAUTH_TYPE_CONFIG = {
|
||||
1: {"port_type": 1, "name": "千川", "app_type": "juliang_qianchuan"},
|
||||
2: {"port_type": 1, "name": "广告", "app_type": "juliang_ad"},
|
||||
3: {"port_type": 1, "name": "本地推", "app_type": "juliang_ad"},
|
||||
4: {"port_type": 1, "name": "星图", "app_type": "juliang_ad"},
|
||||
5: {"port_type": 2, "name": "快手代理商", "app_type": "kuaishou"},
|
||||
6: {"port_type": 3, "name": "巨量星图", "app_type": "juliang_ad"},
|
||||
7: {"port_type": 4, "name": "巨量服务单", "app_type": "juliang_ad"},
|
||||
8: {"port_type": 4, "name": "腾讯服务单", "app_type": "tencent"},
|
||||
9: {"port_type": 5, "name": "腾讯营销K2", "app_type": "tencent"},
|
||||
10: {"port_type": 5, "name": "腾讯营销K3", "app_type": "tencent"},
|
||||
}
|
||||
|
||||
#随机获取一个可用的应用配置
|
||||
async def get_available_app(open_type: int, db: AsyncSession) -> dict:
|
||||
# 从 user_oauth_app 表查询可用应用
|
||||
@@ -54,7 +40,7 @@ async def get_available_app(open_type: int, db: AsyncSession) -> dict:
|
||||
count = count_result.scalar() or 0
|
||||
|
||||
# 检查是否达到最大授权数
|
||||
max_users = app.count
|
||||
max_users = app.max_count
|
||||
if count < max_users:
|
||||
available_apps.append({
|
||||
"app_id": app.app_id,
|
||||
@@ -266,5 +252,52 @@ async def get_juliang_token(app_id: str, secret: str, code: str, open_type: int,
|
||||
|
||||
async def get_kuaishou_token(app_id: str, secret: str, code: str, oauth_type: int, user_id: str, db: AsyncSession) -> dict:
|
||||
return "未配置"
|
||||
|
||||
|
||||
async def get_tencent_token(app_id: str, secret: str, code: str, oauth_type: int, user_id: str, db: AsyncSession) -> str:
|
||||
return "未配置"
|
||||
return "未配置"
|
||||
|
||||
|
||||
async def get_oauth_list(
|
||||
user_id: str,
|
||||
db: AsyncSession,
|
||||
account_userid: str | None = None,
|
||||
open_type: int | None = None,
|
||||
account_id: str | None = None,
|
||||
page: int = 1,
|
||||
page_size: int = 10,
|
||||
) -> dict:
|
||||
if page < 1:
|
||||
page = 1
|
||||
if page_size < 1:
|
||||
page_size = 10
|
||||
|
||||
query = select(UserOAuth).where(
|
||||
UserOAuth.user_id == user_id,
|
||||
UserOAuth.deleted_at.is_(None),
|
||||
)
|
||||
|
||||
if account_userid:
|
||||
query = query.where(UserOAuth.account_userid == account_userid)
|
||||
if open_type:
|
||||
query = query.where(UserOAuth.open_type == open_type)
|
||||
if account_id:
|
||||
query = query.where(UserOAuth.account_id == account_id)
|
||||
|
||||
query = query.order_by(UserOAuth.created_at.desc())
|
||||
|
||||
total_result = await db.execute(query.with_only_columns(UserOAuth.id))
|
||||
total = len(total_result.scalars().all())
|
||||
|
||||
offset = (page - 1) * page_size
|
||||
query = query.offset(offset).limit(page_size)
|
||||
|
||||
result = await db.execute(query)
|
||||
oauth_list = result.scalars().all()
|
||||
|
||||
return {
|
||||
"data": oauth_list,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
}
|
||||
Reference in New Issue
Block a user