feat(fulfillment): 增加工作台状态事件同步
This commit is contained in:
@@ -148,6 +148,8 @@ ORDER_SITE_LOOKUP_TIMEOUT_SECONDS=3
|
|||||||
|
|
||||||
# order_site -> live-hub 直播账号履约服务端集成
|
# order_site -> live-hub 直播账号履约服务端集成
|
||||||
PLATFORM_FULFILLMENT_INTEGRATION_KEY=
|
PLATFORM_FULFILLMENT_INTEGRATION_KEY=
|
||||||
|
# live-hub -> order_site 状态回调地址;生产示例 http://172.23.3.80
|
||||||
|
ORDER_SITE_PLATFORM_FULFILLMENT_URL=
|
||||||
# 可选:指定集成操作人;不填时使用最早创建的有效超级管理员
|
# 可选:指定集成操作人;不填时使用最早创建的有效超级管理员
|
||||||
# PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID=1
|
# PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID=1
|
||||||
# 客户绑定二维码有效期
|
# 客户绑定二维码有效期
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ services:
|
|||||||
- ORDER_SITE_LOOKUP_ENABLED=${ORDER_SITE_LOOKUP_ENABLED:-false}
|
- ORDER_SITE_LOOKUP_ENABLED=${ORDER_SITE_LOOKUP_ENABLED:-false}
|
||||||
- ORDER_SITE_LOOKUP_TIMEOUT_SECONDS=${ORDER_SITE_LOOKUP_TIMEOUT_SECONDS:-3}
|
- ORDER_SITE_LOOKUP_TIMEOUT_SECONDS=${ORDER_SITE_LOOKUP_TIMEOUT_SECONDS:-3}
|
||||||
- PLATFORM_FULFILLMENT_INTEGRATION_KEY=${PLATFORM_FULFILLMENT_INTEGRATION_KEY:-}
|
- PLATFORM_FULFILLMENT_INTEGRATION_KEY=${PLATFORM_FULFILLMENT_INTEGRATION_KEY:-}
|
||||||
|
- ORDER_SITE_PLATFORM_FULFILLMENT_URL=${ORDER_SITE_PLATFORM_FULFILLMENT_URL:-}
|
||||||
- PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID=${PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID:-}
|
- PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID=${PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID:-}
|
||||||
- PLATFORM_BINDING_QR_TTL_SECONDS=${PLATFORM_BINDING_QR_TTL_SECONDS:-600}
|
- PLATFORM_BINDING_QR_TTL_SECONDS=${PLATFORM_BINDING_QR_TTL_SECONDS:-600}
|
||||||
# 用户上传图片的桶(在线协作 / P2 资料收集共用;生产用内网 endpoint)。
|
# 用户上传图片的桶(在线协作 / P2 资料收集共用;生产用内网 endpoint)。
|
||||||
|
|||||||
@@ -464,8 +464,15 @@ class TestBindingAndCompletion(_Base):
|
|||||||
assert task.issue_code == ""
|
assert task.issue_code == ""
|
||||||
assert task.last_error == ""
|
assert task.last_error == ""
|
||||||
|
|
||||||
def test_refresh_binding_maps_role_ready(self):
|
def test_refresh_binding_maps_role_ready(self, monkeypatch):
|
||||||
|
notified: list[int] = []
|
||||||
|
monkeypatch.setattr(
|
||||||
|
service,
|
||||||
|
"notify_order_site_binding_state_async",
|
||||||
|
lambda task_id: notified.append(task_id),
|
||||||
|
)
|
||||||
task = self._allocated()
|
task = self._allocated()
|
||||||
|
notified.clear()
|
||||||
bind_task = DouyuTask(
|
bind_task = DouyuTask(
|
||||||
batch_id="bind-1",
|
batch_id="bind-1",
|
||||||
account_id=task.account_id,
|
account_id=task.account_id,
|
||||||
@@ -487,6 +494,7 @@ class TestBindingAndCompletion(_Base):
|
|||||||
|
|
||||||
assert detail["task"]["bindingStatus"] == "role_ready"
|
assert detail["task"]["bindingStatus"] == "role_ready"
|
||||||
assert detail["task"]["bindTask"]["roleName"] == "测试角色"
|
assert detail["task"]["bindTask"]["roleName"] == "测试角色"
|
||||||
|
assert notified == [task.id]
|
||||||
|
|
||||||
def test_refresh_binding_clears_stale_failure_when_qr_is_pending(self):
|
def test_refresh_binding_clears_stale_failure_when_qr_is_pending(self):
|
||||||
task = self._allocated()
|
task = self._allocated()
|
||||||
|
|||||||
@@ -97,6 +97,10 @@ def test_customer_binding_out_hides_internal_account_data(db):
|
|||||||
assert "points" not in payload
|
assert "points" not in payload
|
||||||
assert "lease" not in payload
|
assert "lease" not in payload
|
||||||
|
|
||||||
|
batch = service.customer_refresh_bindings(db, task_ids=[task.id, 999999])
|
||||||
|
assert [item["sessionId"] for item in batch["items"]] == [task.id]
|
||||||
|
assert batch["items"][0]["qrUrl"] == "https://livenlink.example/private-qr-token"
|
||||||
|
|
||||||
|
|
||||||
def test_binding_qr_expiry_uses_customer_ttl():
|
def test_binding_qr_expiry_uses_customer_ttl():
|
||||||
now = datetime.now(UTC)
|
now = datetime.now(UTC)
|
||||||
@@ -116,3 +120,22 @@ def test_binding_qr_expiry_uses_customer_ttl():
|
|||||||
task.created_at = now - timedelta(minutes=5)
|
task.created_at = now - timedelta(minutes=5)
|
||||||
|
|
||||||
assert service._binding_qr_expired(task, now=now) is True
|
assert service._binding_qr_expired(task, now=now) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_auto_order_sync_is_throttled(monkeypatch):
|
||||||
|
calls: list[int] = []
|
||||||
|
monkeypatch.setattr(service, "_AUTO_SYNC_LAST_ATTEMPT", float("-inf"))
|
||||||
|
monkeypatch.setattr(
|
||||||
|
service,
|
||||||
|
"sync_pending_tasks",
|
||||||
|
lambda _db, *, limit: calls.append(limit) or {"fetched": 1},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert service.sync_pending_tasks_if_due(object(), limit=100, now=100.0) == {
|
||||||
|
"fetched": 1
|
||||||
|
}
|
||||||
|
assert service.sync_pending_tasks_if_due(object(), limit=100, now=110.0) is None
|
||||||
|
assert service.sync_pending_tasks_if_due(object(), limit=100, now=161.0) == {
|
||||||
|
"fetched": 1
|
||||||
|
}
|
||||||
|
assert calls == [100, 100]
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Query
|
from fastapi import APIRouter, Depends, Query
|
||||||
@@ -20,6 +21,7 @@ from ..services.audit_service import record_audit
|
|||||||
from ..services.platform_fulfillment import service
|
from ..services.platform_fulfillment import service
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/platform-fulfillment", tags=["直播账号履约"])
|
router = APIRouter(prefix="/api/platform-fulfillment", tags=["直播账号履约"])
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/settings")
|
@router.get("/settings")
|
||||||
@@ -63,6 +65,10 @@ def list_tasks(
|
|||||||
current: User = Depends(require_permission("platform_fulfillment:view")),
|
current: User = Depends(require_permission("platform_fulfillment:view")),
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
del current
|
del current
|
||||||
|
try:
|
||||||
|
service.sync_pending_tasks_if_due(db, limit=100)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
logger.warning("自动同步待履约订单失败,继续返回本地任务: %s", exc)
|
||||||
return service.list_tasks(
|
return service.list_tasks(
|
||||||
db,
|
db,
|
||||||
status=status,
|
status=status,
|
||||||
@@ -97,6 +103,7 @@ def get_task(
|
|||||||
current: User = Depends(require_permission("platform_fulfillment:view")),
|
current: User = Depends(require_permission("platform_fulfillment:view")),
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
del current
|
del current
|
||||||
|
service.refresh_binding(db, task_id=task_id)
|
||||||
return service.get_task_detail(db, task_id)
|
return service.get_task_detail(db, task_id)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from sqlalchemy.orm import Session
|
|||||||
from ..database import get_db
|
from ..database import get_db
|
||||||
from ..schemas import (
|
from ..schemas import (
|
||||||
PlatformFulfillmentCustomerBindingAction,
|
PlatformFulfillmentCustomerBindingAction,
|
||||||
|
PlatformFulfillmentCustomerSessionBatchRequest,
|
||||||
PlatformFulfillmentCustomerSessionRequest,
|
PlatformFulfillmentCustomerSessionRequest,
|
||||||
)
|
)
|
||||||
from ..services.platform_fulfillment import service
|
from ..services.platform_fulfillment import service
|
||||||
@@ -32,6 +33,18 @@ def require_integration_key(
|
|||||||
raise HTTPException(status_code=401, detail="直播账号履约集成鉴权失败")
|
raise HTTPException(status_code=401, detail="直播账号履约集成鉴权失败")
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/events/order-sync",
|
||||||
|
dependencies=[Depends(require_integration_key)],
|
||||||
|
)
|
||||||
|
def sync_order_event(
|
||||||
|
payload: dict[str, Any],
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
del payload
|
||||||
|
return service.sync_pending_tasks(db, limit=500)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
"/customer-sessions/ensure", dependencies=[Depends(require_integration_key)]
|
"/customer-sessions/ensure", dependencies=[Depends(require_integration_key)]
|
||||||
)
|
)
|
||||||
@@ -73,6 +86,17 @@ def get_customer_binding_status(
|
|||||||
return service.customer_refresh_binding(db, task_id=task_id)
|
return service.customer_refresh_binding(db, task_id=task_id)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/customer-sessions/status/batch",
|
||||||
|
dependencies=[Depends(require_integration_key)],
|
||||||
|
)
|
||||||
|
def get_customer_binding_statuses(
|
||||||
|
req: PlatformFulfillmentCustomerSessionBatchRequest,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
return service.customer_refresh_bindings(db, task_ids=req.task_ids)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
"/customer-sessions/{task_id}/binding/confirm",
|
"/customer-sessions/{task_id}/binding/confirm",
|
||||||
dependencies=[Depends(require_integration_key)],
|
dependencies=[Depends(require_integration_key)],
|
||||||
|
|||||||
@@ -1589,6 +1589,10 @@ class PlatformFulfillmentCustomerBindingAction(BaseModel):
|
|||||||
force_refresh: bool = False
|
force_refresh: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class PlatformFulfillmentCustomerSessionBatchRequest(BaseModel):
|
||||||
|
task_ids: list[int] = Field(default_factory=list, max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class DouyuTaskBatchRequest(BaseModel):
|
class DouyuTaskBatchRequest(BaseModel):
|
||||||
account_ids: list[int]
|
account_ids: list[int]
|
||||||
task_type: str
|
task_type: str
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
"""把直播账号履约状态变化主动推送给 order_site。"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
from threading import Thread
|
||||||
|
from urllib.error import HTTPError, URLError
|
||||||
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
from ...database import SessionLocal
|
||||||
|
|
||||||
|
PUBLISH_ATTEMPTS = 3
|
||||||
|
PUBLISH_TIMEOUT_SECONDS = 5
|
||||||
|
|
||||||
|
|
||||||
|
def notify_order_site_binding_state_async(task_id: int) -> None:
|
||||||
|
"""状态提交后异步推送;失败由工作台低频对账补齐。"""
|
||||||
|
Thread(
|
||||||
|
target=_publish_binding_state,
|
||||||
|
args=(int(task_id),),
|
||||||
|
name=f"order-site-fulfillment-{task_id}",
|
||||||
|
daemon=True,
|
||||||
|
).start()
|
||||||
|
|
||||||
|
|
||||||
|
def _publish_binding_state(task_id: int) -> None:
|
||||||
|
base_url = os.getenv("ORDER_SITE_PLATFORM_FULFILLMENT_URL", "").strip().rstrip("/")
|
||||||
|
integration_key = os.getenv("PLATFORM_FULFILLMENT_INTEGRATION_KEY", "").strip()
|
||||||
|
if not base_url or not integration_key:
|
||||||
|
return
|
||||||
|
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
from . import service
|
||||||
|
|
||||||
|
payload = service.customer_binding_out(db, task_id)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
logger.warning("构造 order_site 履约状态事件失败 task_id={}: {}", task_id, exc)
|
||||||
|
return
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
body = json.dumps(
|
||||||
|
{
|
||||||
|
"eventId": str(uuid.uuid4()),
|
||||||
|
"occurredAt": datetime.now(UTC).isoformat(),
|
||||||
|
"items": [payload],
|
||||||
|
},
|
||||||
|
ensure_ascii=False,
|
||||||
|
).encode("utf-8")
|
||||||
|
endpoint = f"{base_url}/api/integration/platform-fulfillment/binding-state"
|
||||||
|
|
||||||
|
for attempt in range(1, PUBLISH_ATTEMPTS + 1):
|
||||||
|
request = Request(
|
||||||
|
endpoint,
|
||||||
|
data=body,
|
||||||
|
method="POST",
|
||||||
|
headers={
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-Integration-Key": integration_key,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with urlopen(request, timeout=PUBLISH_TIMEOUT_SECONDS) as response:
|
||||||
|
if 200 <= response.status < 300:
|
||||||
|
return
|
||||||
|
raise RuntimeError(f"order_site 返回 HTTP {response.status}")
|
||||||
|
except (HTTPError, URLError, OSError, RuntimeError) as exc:
|
||||||
|
if attempt >= PUBLISH_ATTEMPTS:
|
||||||
|
logger.warning(
|
||||||
|
"推送 order_site 履约状态失败 task_id={} attempts={}: {}",
|
||||||
|
task_id,
|
||||||
|
attempt,
|
||||||
|
exc,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
time.sleep(float(attempt))
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["notify_order_site_binding_state_async"]
|
||||||
@@ -3,8 +3,10 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, timedelta
|
||||||
|
from threading import Lock
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
@@ -38,6 +40,7 @@ from .account_selector import (
|
|||||||
release_lease,
|
release_lease,
|
||||||
renew_lease,
|
renew_lease,
|
||||||
)
|
)
|
||||||
|
from .order_site_notifier import notify_order_site_binding_state_async
|
||||||
|
|
||||||
TARGET_PLATFORM = "douyu"
|
TARGET_PLATFORM = "douyu"
|
||||||
SCOPE_ELITE = "elite"
|
SCOPE_ELITE = "elite"
|
||||||
@@ -52,6 +55,11 @@ ACTIVE_STATUSES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BINDING_QR_TTL_SECONDS = int(os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "600"))
|
BINDING_QR_TTL_SECONDS = int(os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "600"))
|
||||||
|
AUTO_SYNC_INTERVAL_SECONDS = 60.0
|
||||||
|
|
||||||
|
_SYNC_LOCK = Lock()
|
||||||
|
_AUTO_SYNC_LOCK = Lock()
|
||||||
|
_AUTO_SYNC_LAST_ATTEMPT = float("-inf")
|
||||||
|
|
||||||
|
|
||||||
def _now() -> datetime:
|
def _now() -> datetime:
|
||||||
@@ -251,7 +259,7 @@ def _apply_mapping(
|
|||||||
task.last_error = ""
|
task.last_error = ""
|
||||||
|
|
||||||
|
|
||||||
def sync_pending_tasks(db: Session, *, limit: int = 100) -> dict[str, Any]:
|
def _sync_pending_tasks(db: Session, *, limit: int = 100) -> dict[str, Any]:
|
||||||
"""从 order_site 拉取人工待履约订单项并幂等生成本地作业。"""
|
"""从 order_site 拉取人工待履约订单项并幂等生成本地作业。"""
|
||||||
try:
|
try:
|
||||||
rows = list_pending_manual_fulfillments(limit=limit)
|
rows = list_pending_manual_fulfillments(limit=limit)
|
||||||
@@ -317,6 +325,37 @@ def sync_pending_tasks(db: Session, *, limit: int = 100) -> dict[str, Any]:
|
|||||||
return {"fetched": len(rows), "created": created, "updated": updated}
|
return {"fetched": len(rows), "created": created, "updated": updated}
|
||||||
|
|
||||||
|
|
||||||
|
def sync_pending_tasks(db: Session, *, limit: int = 100) -> dict[str, Any]:
|
||||||
|
"""串行同步订单,避免自动同步与人工同步并发创建重复任务。"""
|
||||||
|
with _SYNC_LOCK:
|
||||||
|
return _sync_pending_tasks(db, limit=limit)
|
||||||
|
|
||||||
|
|
||||||
|
def sync_pending_tasks_if_due(
|
||||||
|
db: Session,
|
||||||
|
*,
|
||||||
|
limit: int = 100,
|
||||||
|
now: float | None = None,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""工作台读取时自动同步;按进程节流,不阻塞每 5 秒一次的列表轮询。"""
|
||||||
|
global _AUTO_SYNC_LAST_ATTEMPT
|
||||||
|
|
||||||
|
if not _AUTO_SYNC_LOCK.acquire(blocking=False):
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
current = time.monotonic() if now is None else now
|
||||||
|
if current - _AUTO_SYNC_LAST_ATTEMPT < AUTO_SYNC_INTERVAL_SECONDS:
|
||||||
|
return None
|
||||||
|
_AUTO_SYNC_LAST_ATTEMPT = current
|
||||||
|
try:
|
||||||
|
return sync_pending_tasks(db, limit=limit)
|
||||||
|
except Exception:
|
||||||
|
_AUTO_SYNC_LAST_ATTEMPT = float("-inf")
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
_AUTO_SYNC_LOCK.release()
|
||||||
|
|
||||||
|
|
||||||
def list_tasks(
|
def list_tasks(
|
||||||
db: Session,
|
db: Session,
|
||||||
*,
|
*,
|
||||||
@@ -609,6 +648,21 @@ def customer_refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
|||||||
return customer_binding_out(db, task_id)
|
return customer_binding_out(db, task_id)
|
||||||
|
|
||||||
|
|
||||||
|
def customer_refresh_bindings(db: Session, *, task_ids: list[int]) -> dict[str, Any]:
|
||||||
|
items: list[dict[str, Any]] = []
|
||||||
|
for task_id in dict.fromkeys(task_ids):
|
||||||
|
task = (
|
||||||
|
db.query(PlatformFulfillmentTask)
|
||||||
|
.filter(PlatformFulfillmentTask.id == task_id)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if task is None:
|
||||||
|
continue
|
||||||
|
refresh_binding(db, task_id=task.id)
|
||||||
|
items.append(customer_binding_out(db, task.id))
|
||||||
|
return {"items": items}
|
||||||
|
|
||||||
|
|
||||||
async def customer_confirm_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
async def customer_confirm_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||||
refresh_binding(db, task_id=task_id)
|
refresh_binding(db, task_id=task_id)
|
||||||
await confirm_binding(db, task_id=task_id, current=_integration_actor(db))
|
await confirm_binding(db, task_id=task_id, current=_integration_actor(db))
|
||||||
@@ -839,6 +893,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
task.account_id = selected.account_id
|
task.account_id = selected.account_id
|
||||||
@@ -882,6 +937,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
|
|
||||||
@@ -917,6 +973,7 @@ def release_task_account(
|
|||||||
detail={"reason": reason},
|
detail={"reason": reason},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
|
|
||||||
@@ -988,6 +1045,7 @@ async def start_binding(
|
|||||||
detail={"bindTaskId": bind_task.id},
|
detail={"bindTaskId": bind_task.id},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
start_douyu_batch(
|
start_douyu_batch(
|
||||||
batch_id=batch_id,
|
batch_id=batch_id,
|
||||||
task_type="get_bind_qr",
|
task_type="get_bind_qr",
|
||||||
@@ -1004,6 +1062,13 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
|||||||
bind_task = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
bind_task = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||||
if bind_task is None:
|
if bind_task is None:
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
before = (
|
||||||
|
task.binding_status,
|
||||||
|
task.execution_status,
|
||||||
|
task.exchange_status,
|
||||||
|
task.issue_code,
|
||||||
|
task.last_error,
|
||||||
|
)
|
||||||
result = bind_task.result or {}
|
result = bind_task.result or {}
|
||||||
if bind_task.status in {"failed", "error", "stopped"}:
|
if bind_task.status in {"failed", "error", "stopped"}:
|
||||||
task.binding_status = "failed"
|
task.binding_status = "failed"
|
||||||
@@ -1027,6 +1092,15 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
|||||||
task.last_error = ""
|
task.last_error = ""
|
||||||
task.updated_at = _now()
|
task.updated_at = _now()
|
||||||
db.commit()
|
db.commit()
|
||||||
|
after = (
|
||||||
|
task.binding_status,
|
||||||
|
task.execution_status,
|
||||||
|
task.exchange_status,
|
||||||
|
task.issue_code,
|
||||||
|
task.last_error,
|
||||||
|
)
|
||||||
|
if after != before:
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
|
|
||||||
@@ -1072,6 +1146,7 @@ async def confirm_binding(
|
|||||||
detail={"confirmTaskId": confirm_task.id},
|
detail={"confirmTaskId": confirm_task.id},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
start_douyu_batch(
|
start_douyu_batch(
|
||||||
batch_id=batch_id,
|
batch_id=batch_id,
|
||||||
task_type="confirm_bind",
|
task_type="confirm_bind",
|
||||||
@@ -1118,6 +1193,7 @@ def refresh_exchange_eligibility(
|
|||||||
detail={"error": str(exc)},
|
detail={"error": str(exc)},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
if task.commodity_id in eligibility.limited_ids:
|
if task.commodity_id in eligibility.limited_ids:
|
||||||
@@ -1153,6 +1229,7 @@ def refresh_exchange_eligibility(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
|
|
||||||
@@ -1193,6 +1270,7 @@ def complete_manual_exchange(
|
|||||||
detail={"note": note[:1000]},
|
detail={"note": note[:1000]},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
|
|
||||||
@@ -1218,6 +1296,7 @@ def mark_manual_required(
|
|||||||
detail={"reason": task.last_error},
|
detail={"reason": task.last_error},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
|
|
||||||
@@ -1248,6 +1327,7 @@ def cancel_task(
|
|||||||
detail={"reason": task.last_error},
|
detail={"reason": task.last_error},
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
notify_order_site_binding_state_async(task.id)
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
|
||||||
|
|
||||||
@@ -1256,6 +1336,7 @@ __all__ = [
|
|||||||
"cancel_task",
|
"cancel_task",
|
||||||
"complete_manual_exchange",
|
"complete_manual_exchange",
|
||||||
"confirm_binding",
|
"confirm_binding",
|
||||||
|
"customer_refresh_bindings",
|
||||||
"get_task_detail",
|
"get_task_detail",
|
||||||
"list_mappings",
|
"list_mappings",
|
||||||
"list_tasks",
|
"list_tasks",
|
||||||
@@ -1265,5 +1346,6 @@ __all__ = [
|
|||||||
"release_task_account",
|
"release_task_account",
|
||||||
"start_binding",
|
"start_binding",
|
||||||
"sync_pending_tasks",
|
"sync_pending_tasks",
|
||||||
|
"sync_pending_tasks_if_due",
|
||||||
"upsert_mapping",
|
"upsert_mapping",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -242,8 +242,8 @@ export default function PlatformFulfillmentPage() {
|
|||||||
const [leaseDurationValue, setLeaseDurationValue] = useState<number | null>(60);
|
const [leaseDurationValue, setLeaseDurationValue] = useState<number | null>(60);
|
||||||
const [savingSettings, setSavingSettings] = useState(false);
|
const [savingSettings, setSavingSettings] = useState(false);
|
||||||
|
|
||||||
const loadTasks = useCallback(async () => {
|
const loadTasks = useCallback(async (silent = false) => {
|
||||||
setLoading(true);
|
if (!silent) setLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await platformFulfillmentApi.listTasks({
|
const result = await platformFulfillmentApi.listTasks({
|
||||||
status: status || undefined,
|
status: status || undefined,
|
||||||
@@ -254,21 +254,21 @@ export default function PlatformFulfillmentPage() {
|
|||||||
setRows(result.items);
|
setRows(result.items);
|
||||||
setTotal(result.pagination.total);
|
setTotal(result.pagination.total);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
message.error(getErrorMessage(error));
|
if (!silent) message.error(getErrorMessage(error));
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
if (!silent) setLoading(false);
|
||||||
}
|
}
|
||||||
}, [keyword, page, pageSize, status]);
|
}, [keyword, page, pageSize, status]);
|
||||||
|
|
||||||
const loadDetail = useCallback(async (taskId: number) => {
|
const loadDetail = useCallback(async (taskId: number, silent = false) => {
|
||||||
setDetailLoading(true);
|
if (!silent) setDetailLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await platformFulfillmentApi.getTask(taskId);
|
const result = await platformFulfillmentApi.getTask(taskId);
|
||||||
setDetail(result);
|
setDetail(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
message.error(getErrorMessage(error));
|
if (!silent) message.error(getErrorMessage(error));
|
||||||
} finally {
|
} finally {
|
||||||
setDetailLoading(false);
|
if (!silent) setDetailLoading(false);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -276,6 +276,13 @@ export default function PlatformFulfillmentPage() {
|
|||||||
void loadTasks();
|
void loadTasks();
|
||||||
}, [loadTasks]);
|
}, [loadTasks]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const timer = window.setInterval(() => {
|
||||||
|
if (document.visibilityState === 'visible') void loadTasks(true);
|
||||||
|
}, 5000);
|
||||||
|
return () => window.clearInterval(timer);
|
||||||
|
}, [loadTasks]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void platformFulfillmentApi
|
void platformFulfillmentApi
|
||||||
.getSettings()
|
.getSettings()
|
||||||
@@ -287,6 +294,14 @@ export default function PlatformFulfillmentPage() {
|
|||||||
if (selectedId) void loadDetail(selectedId);
|
if (selectedId) void loadDetail(selectedId);
|
||||||
}, [loadDetail, selectedId]);
|
}, [loadDetail, selectedId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!selectedId) return;
|
||||||
|
const timer = window.setInterval(() => {
|
||||||
|
if (document.visibilityState === 'visible') void loadDetail(selectedId, true);
|
||||||
|
}, 3000);
|
||||||
|
return () => window.clearInterval(timer);
|
||||||
|
}, [loadDetail, selectedId]);
|
||||||
|
|
||||||
const selected = detail?.task || null;
|
const selected = detail?.task || null;
|
||||||
const qrValue = selected?.bindTask?.qrUrl || '';
|
const qrValue = selected?.bindTask?.qrUrl || '';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user