feat: AI套图分层方案 + Gemini集成 - 4种图案类型处理 + 正片叠底 + 宽高比 + 模型选择
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
记录和分析用户操作
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi import APIRouter, HTTPException, Depends, Header
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, Any
|
||||
from datetime import datetime
|
||||
from app.core.security import get_current_user
|
||||
from app.core.config import settings
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -14,28 +16,28 @@ router = APIRouter()
|
||||
action_logs = []
|
||||
|
||||
class ActionLog(BaseModel):
|
||||
username: str
|
||||
device_id: str
|
||||
action: str
|
||||
details: Optional[Any] = None
|
||||
timestamp: int
|
||||
session_id: str
|
||||
device_id: Optional[str] = None
|
||||
|
||||
class ActionLogResponse(BaseModel):
|
||||
success: bool
|
||||
message: str
|
||||
|
||||
@router.post("/log", response_model=ActionLogResponse)
|
||||
async def log_action(log: ActionLog):
|
||||
"""
|
||||
记录用户行为
|
||||
"""
|
||||
async def log_action(log: ActionLog, current_username: str = Depends(get_current_user)):
|
||||
"""记录用户行为(仅记录当前登录用户的操作)"""
|
||||
try:
|
||||
# 添加服务器时间
|
||||
log_entry = {
|
||||
**log.dict(),
|
||||
"username": current_username,
|
||||
"action": log.action,
|
||||
"details": log.details,
|
||||
"timestamp": log.timestamp,
|
||||
"session_id": log.session_id,
|
||||
"device_id": log.device_id,
|
||||
"server_time": datetime.now().isoformat(),
|
||||
"ip": "unknown" # 可以从请求中获取
|
||||
}
|
||||
|
||||
action_logs.append(log_entry)
|
||||
@@ -44,40 +46,37 @@ async def log_action(log: ActionLog):
|
||||
if len(action_logs) > 10000:
|
||||
action_logs.pop(0)
|
||||
|
||||
# 可以在这里添加异常检测逻辑
|
||||
# 例如:检测同一用户短时间内的大量操作
|
||||
|
||||
return ActionLogResponse(success=True, message="已记录")
|
||||
except Exception as e:
|
||||
return ActionLogResponse(success=False, message=str(e))
|
||||
|
||||
@router.get("/stats/{username}")
|
||||
async def get_user_stats(username: str):
|
||||
"""
|
||||
获取用户统计信息
|
||||
"""
|
||||
user_logs = [log for log in action_logs if log.get("username") == username]
|
||||
@router.get("/stats/me")
|
||||
async def get_my_stats(current_username: str = Depends(get_current_user)):
|
||||
"""获取当前用户的统计信息"""
|
||||
user_logs = [log for log in action_logs if log.get("username") == current_username]
|
||||
|
||||
# 统计各操作类型的次数
|
||||
action_counts = {}
|
||||
action_counts: dict[str, int] = {}
|
||||
for log in user_logs:
|
||||
action = log.get("action", "unknown")
|
||||
action_counts[action] = action_counts.get(action, 0) + 1
|
||||
|
||||
return {
|
||||
"username": username,
|
||||
"username": current_username,
|
||||
"total_actions": len(user_logs),
|
||||
"action_counts": action_counts,
|
||||
"recent_actions": user_logs[-10:] # 最近 10 条
|
||||
"recent_actions": user_logs[-10:]
|
||||
}
|
||||
|
||||
@router.get("/recent")
|
||||
async def get_recent_logs(limit: int = 100):
|
||||
"""
|
||||
获取最近的操作日志(管理员用)
|
||||
"""
|
||||
async def get_recent_logs(
|
||||
limit: int = 100,
|
||||
admin_token: Optional[str] = Header(None, alias="X-Admin-Token")
|
||||
):
|
||||
"""获取最近的操作日志(仅管理员)"""
|
||||
if admin_token != settings.ADMIN_TOKEN:
|
||||
raise HTTPException(status_code=403, detail="需要管理员权限")
|
||||
|
||||
return {
|
||||
"total": len(action_logs),
|
||||
"logs": action_logs[-limit:]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user