feat: 增加使用期限激活限制

This commit is contained in:
2026-05-20 11:27:07 +08:00
parent fcac708814
commit 90023663fd
12 changed files with 508 additions and 22 deletions
+26
View File
@@ -0,0 +1,26 @@
from fastapi import APIRouter, Body, HTTPException
from app.services.license import InvalidActivationCodeError, activate, get_license_info
router = APIRouter(tags=["license"])
@router.get("/api/license/status")
async def get_license_status():
info = get_license_info()
return {"success": True, **info.to_dict()}
@router.post("/api/license/activate")
async def activate_license(code: str = Body(..., embed=True)):
try:
info = activate(code)
except InvalidActivationCodeError as exc:
raise HTTPException(status_code=400, detail=exc.to_detail()) from exc
return {
"success": True,
"message": "激活成功,到期日期已延长 30 天",
**info.to_dict(),
}
+21 -2
View File
@@ -8,6 +8,7 @@ from fastapi import APIRouter, Body, HTTPException
from app import state
from app.config import AppConfig, CACHE_DIR, RemoteDataConfig
from app.processor import DataProcessor, ProcessLogger
from app.services.license import LicenseError, LicenseInfo, check_processing_allowed
from app.services.remote_download import RemoteDataDownloader
@@ -115,11 +116,13 @@ def _run_remote_processing(
raise RuntimeError("远程目录中未下载到任何文件")
state.history_manager.update(task_id, file_count=download_result.file_count)
logger.set_stage("extracting")
logger.set_stage("license")
_log_license_check(logger, check_processing_allowed(work_dir))
processor = DataProcessor(app_config, work_dir, logger)
result = processor.process()
status = "completed" if result.get("success") else "failed"
error = result.get("error")
if status == "completed" and remote_config.auto_delete_source:
try:
deleted_count = downloader.delete_source_files(download_result.remote_files)
@@ -131,21 +134,25 @@ def _run_remote_processing(
task_id,
status=status,
elapsed_time=result.get("elapsed_time", 0),
error=result.get("error"),
error=error,
result_tables=["4G_结果表", "5G_结果表"],
)
state.processing_tasks[task_id] = {
"logs": state.history_manager.get_logs(task_id),
"status": status,
"stage": status,
"error": error,
}
except Exception as exc:
error_detail = exc.to_detail() if isinstance(exc, LicenseError) else None
logger.error(f"远程自动化任务失败: {exc}")
state.history_manager.update(task_id, status="failed", error=str(exc))
state.processing_tasks[task_id] = {
"logs": state.history_manager.get_logs(task_id),
"status": "failed",
"stage": "failed",
"error": str(exc),
"error_detail": error_detail,
}
finally:
try:
@@ -162,3 +169,15 @@ def _format_bytes(size: int) -> str:
return f"{value:.1f} {unit}"
value /= 1024
return f"{value:.1f} TB"
def _log_license_check(logger: ProcessLogger, info: LicenseInfo) -> None:
if info.current_date:
logger.info(
f"授权校验通过,数据日期: {info.current_date.isoformat()},"
f"到期日期: {info.expires_on.isoformat()}"
)
elif info.zip_count:
logger.warning("未从 ZIP 文件名识别到日期,已跳过授权日期比对")
else:
logger.warning("未找到 ZIP 文件,已跳过授权日期比对")
+25 -1
View File
@@ -8,6 +8,7 @@ from fastapi import APIRouter, Body, HTTPException
from app import state
from app.config import AppConfig
from app.processor import DataProcessor, ProcessLogger
from app.services.license import LicenseError, LicenseInfo, check_processing_allowed
router = APIRouter(tags=["tasks"])
@@ -133,6 +134,8 @@ async def get_processing_status(task_id: str = Body(..., embed=True)):
"status": task_info["status"],
"stage": task_info.get("stage"),
"logs": logs,
"error": task_info.get("error"),
"error_detail": task_info.get("error_detail"),
}
record = state.history_manager.get(task_id)
@@ -170,27 +173,36 @@ def _set_task_stage(task_id: str, stage: str, logs: list[str], status: str = "pr
def _run_processing(task_id: str, work_dir: Path, logger: ProcessLogger, app_config: AppConfig) -> None:
try:
logger.set_stage("license")
_log_license_check(logger, check_processing_allowed(work_dir))
processor = DataProcessor(app_config, work_dir, logger)
result = processor.process()
status = "completed" if result.get("success") else "failed"
error = result.get("error")
state.history_manager.update(
task_id,
status=status,
elapsed_time=result.get("elapsed_time", 0),
error=result.get("error"),
error=error,
result_tables=["4G_结果表", "5G_结果表"],
)
state.processing_tasks[task_id] = {
"logs": state.history_manager.get_logs(task_id),
"status": status,
"stage": status,
"error": error,
}
except Exception as exc:
error_detail = exc.to_detail() if isinstance(exc, LicenseError) else None
logger.error(str(exc))
state.history_manager.update(task_id, status="failed", error=str(exc))
state.processing_tasks[task_id] = {
"logs": state.history_manager.get_logs(task_id),
"status": "failed",
"stage": "failed",
"error": str(exc),
"error_detail": error_detail,
}
finally:
try:
@@ -198,3 +210,15 @@ def _run_processing(task_id: str, work_dir: Path, logger: ProcessLogger, app_con
except Exception as exc:
print(f"自动清理处理历史失败: {exc}")
state.reset_task_lock()
def _log_license_check(logger: ProcessLogger, info: LicenseInfo) -> None:
if info.current_date:
logger.info(
f"授权校验通过,数据日期: {info.current_date.isoformat()},"
f"到期日期: {info.expires_on.isoformat()}"
)
elif info.zip_count:
logger.warning("未从 ZIP 文件名识别到日期,已跳过授权日期比对")
else:
logger.warning("未找到 ZIP 文件,已跳过授权日期比对")