feat(fulfillment): 支持共享账号操作并完善时间线
This commit is contained in:
@@ -19,7 +19,7 @@ from ..models import (
|
||||
LoginTask,
|
||||
User,
|
||||
)
|
||||
from ..permissions import user_has_permission
|
||||
from ..permissions import ROLE_LABELS, user_has_permission
|
||||
from ..schemas import (
|
||||
AccountAssign,
|
||||
AccountAutomationUpdate,
|
||||
@@ -1856,8 +1856,34 @@ def account_events(
|
||||
.all()
|
||||
)
|
||||
|
||||
items = [dict(row) for row in rows]
|
||||
actor_ids = {
|
||||
int(item["actor_id"])
|
||||
for item in items
|
||||
if item.get("actor_type") == "user"
|
||||
and item.get("actor_id") is not None
|
||||
}
|
||||
actors = {
|
||||
int(user.id): user
|
||||
for user in db.query(User).filter(User.id.in_(actor_ids)).all()
|
||||
} if actor_ids else {}
|
||||
for item in items:
|
||||
actor = (
|
||||
actors.get(int(item["actor_id"]))
|
||||
if item.get("actor_type") == "user"
|
||||
and item.get("actor_id") is not None
|
||||
else None
|
||||
)
|
||||
item["actor_name"] = (
|
||||
actor.deleted_username or actor.username
|
||||
) if actor is not None else ""
|
||||
item["actor_role"] = actor.role if actor is not None else ""
|
||||
item["actor_role_label"] = (
|
||||
ROLE_LABELS.get(actor.role, actor.role) if actor is not None else ""
|
||||
)
|
||||
|
||||
return {
|
||||
"items": [dict(row) for row in rows],
|
||||
"items": items,
|
||||
"total": int(total),
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
|
||||
@@ -37,6 +37,7 @@ from ..models import (
|
||||
DouyuWorkbench,
|
||||
DouyuWorkbenchAccount,
|
||||
DouyuXpdGoodsSnapshot,
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
)
|
||||
from ..permissions import user_has_permission
|
||||
@@ -70,6 +71,7 @@ from ..services.douyu_service import (
|
||||
douyu_task_payload,
|
||||
ensure_douyu_config,
|
||||
)
|
||||
from ..services.platform_fulfillment import service as platform_fulfillment_service
|
||||
|
||||
router = APIRouter(prefix="/api/douyu", tags=["斗鱼活动"])
|
||||
|
||||
@@ -159,6 +161,13 @@ async def supplier_recharge_callback(request: Request, db: Session = Depends(get
|
||||
)
|
||||
task.result = result
|
||||
callback_success = status not in {3, 4}
|
||||
if task.platform_task_id and status in {2, 3, 4}:
|
||||
platform_fulfillment_service.record_douyu_operation_result(
|
||||
db,
|
||||
task,
|
||||
status="success" if status == 2 else "failed",
|
||||
message=task.message,
|
||||
)
|
||||
# 回调说成功了就补一条充值事件。幂等键与 runner 那条**完全一致**
|
||||
# (business_events.task_ref),所以「runner 也观测到了」时不会重复记账;
|
||||
# 而「runner 已判超时/失败、回调才到」这个黑洞则能被补上。
|
||||
@@ -237,12 +246,21 @@ def _visible_tasks_query(db: Session, current: User):
|
||||
return query
|
||||
if user_has_permission(current, "account:view_assigned"):
|
||||
# 彻底消除全表 filesort:直接通过 douyu_tasks 上的 account_assigned_to 复合索引倒序定位
|
||||
return query.filter(DouyuTask.account_assigned_to == current.id)
|
||||
return query.filter(
|
||||
or_(
|
||||
DouyuTask.account_assigned_to == current.id,
|
||||
DouyuTask.created_by == current.id,
|
||||
)
|
||||
)
|
||||
raise HTTPException(status_code=403, detail="无权查看斗鱼任务")
|
||||
|
||||
|
||||
def _require_task_account_access(
|
||||
db: Session, current: User, account_ids: list[int]
|
||||
db: Session,
|
||||
current: User,
|
||||
account_ids: list[int],
|
||||
*,
|
||||
platform_task_id: int | None = None,
|
||||
) -> None:
|
||||
"""确保任务只会提交到当前用户可操作的账号。"""
|
||||
requested_ids = set(account_ids)
|
||||
@@ -252,8 +270,27 @@ def _require_task_account_access(
|
||||
raise HTTPException(status_code=403, detail="无权操作斗鱼账号")
|
||||
query = query.filter(Account.assigned_to == current.id)
|
||||
allowed_ids = {account_id for (account_id,) in query.all()}
|
||||
if allowed_ids != requested_ids:
|
||||
raise HTTPException(status_code=403, detail="包含无权操作的斗鱼账号")
|
||||
if allowed_ids == requested_ids:
|
||||
return
|
||||
if (
|
||||
platform_task_id
|
||||
and len(requested_ids) == 1
|
||||
and user_has_permission(current, "platform_fulfillment:use")
|
||||
):
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.id == int(platform_task_id),
|
||||
PlatformFulfillmentTask.account_id.in_(requested_ids),
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
["completed", "cancelled"]
|
||||
),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if task is not None:
|
||||
return
|
||||
raise HTTPException(status_code=403, detail="包含无权操作的斗鱼账号")
|
||||
|
||||
|
||||
def _require_batch_owner(db: Session, current: User, batch_id: str) -> None:
|
||||
@@ -731,7 +768,12 @@ async def create_task_batch(
|
||||
"""创建斗鱼任务记录并启动后台执行器。"""
|
||||
if not req.account_ids:
|
||||
raise HTTPException(status_code=400, detail="请选择斗鱼账号")
|
||||
_require_task_account_access(db, current, req.account_ids)
|
||||
_require_task_account_access(
|
||||
db,
|
||||
current,
|
||||
req.account_ids,
|
||||
platform_task_id=req.platform_task_id,
|
||||
)
|
||||
|
||||
cleanup_orphan_douyu_tasks(
|
||||
db,
|
||||
@@ -748,6 +790,7 @@ async def create_task_batch(
|
||||
req.handbook_scope,
|
||||
current.id,
|
||||
req.payload,
|
||||
platform_task_id=req.platform_task_id,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
@@ -755,6 +798,31 @@ async def create_task_batch(
|
||||
raise HTTPException(
|
||||
status_code=400, detail="没有可执行的斗鱼账号,请先登录获取 Cookie"
|
||||
)
|
||||
if req.platform_task_id:
|
||||
platform_task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(PlatformFulfillmentTask.id == int(req.platform_task_id))
|
||||
.first()
|
||||
)
|
||||
douyu_task_ids = [
|
||||
int(task_id)
|
||||
for (task_id,) in (
|
||||
db.query(DouyuTask.id)
|
||||
.filter(DouyuTask.batch_id == batch_id)
|
||||
.order_by(DouyuTask.id.asc())
|
||||
.all()
|
||||
)
|
||||
]
|
||||
if platform_task is not None:
|
||||
platform_fulfillment_service.record_douyu_operation_started(
|
||||
db,
|
||||
platform_task,
|
||||
current=current,
|
||||
batch_id=batch_id,
|
||||
task_type=req.task_type,
|
||||
douyu_task_ids=douyu_task_ids,
|
||||
payload=req.payload,
|
||||
)
|
||||
|
||||
if req.task_type == "create_gold_qr":
|
||||
config = ensure_douyu_config(db)
|
||||
@@ -783,6 +851,7 @@ async def create_task_batch(
|
||||
"batch_id": batch_id,
|
||||
"task_type": req.task_type,
|
||||
"count": count,
|
||||
"platform_task_id": req.platform_task_id,
|
||||
"account_count": len(req.account_ids),
|
||||
"handbook_scope": req.handbook_scope,
|
||||
"recharge_channel": recharge_channel,
|
||||
|
||||
Reference in New Issue
Block a user