feat: AI虚拟用户新闻互动系统 v1.3.0 初始提交
- 虚拟用户管理(昵称/头像/性别/简介/邮箱同步到目标平台) - AI互动调度(点赞/收藏/评论/转发) - 日志时间改为北京时间 - 评论达上限后继续执行点赞收藏转发 - 一键登出全部功能 - 浅色主题UI
This commit is contained in:
12
backend/app/api/__init__.py
Normal file
12
backend/app/api/__init__.py
Normal file
@@ -0,0 +1,12 @@
|
||||
"""API路由汇总"""
|
||||
from fastapi import APIRouter
|
||||
from app.api.endpoints import users, interactions, ai_models, dashboard, system, logs
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
router.include_router(users.router, prefix="/users", tags=["虚拟用户管理"])
|
||||
router.include_router(interactions.router, prefix="/interactions", tags=["互动记录"])
|
||||
router.include_router(ai_models.router, prefix="/ai-models", tags=["AI模型配置"])
|
||||
router.include_router(dashboard.router, prefix="/dashboard", tags=["数据看板"])
|
||||
router.include_router(system.router, prefix="/system", tags=["系统设置"])
|
||||
router.include_router(logs.router, prefix="/logs", tags=["日志管理"])
|
||||
0
backend/app/api/endpoints/__init__.py
Normal file
0
backend/app/api/endpoints/__init__.py
Normal file
87
backend/app/api/endpoints/ai_models.py
Normal file
87
backend/app/api/endpoints/ai_models.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""AI模型配置接口"""
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select, update
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.schemas import ApiResponse, AIModelCreateRequest, AIModelUpdateRequest, AIModelTestRequest
|
||||
from app.models import AIModelConfig
|
||||
from app.utils.crypto import encrypt, decrypt
|
||||
from app.services.ai_service import ai_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_models(db=Depends(get_db)):
|
||||
result = await db.execute(select(AIModelConfig).order_by(AIModelConfig.created_at.desc()))
|
||||
models = result.scalars().all()
|
||||
items = [_format_model(m) for m in models]
|
||||
return ApiResponse(data=items)
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_model(req: AIModelCreateRequest, db=Depends(get_db)):
|
||||
if req.is_default:
|
||||
await db.execute(update(AIModelConfig).values(is_default=0))
|
||||
model = AIModelConfig(
|
||||
model_name=req.model_name,
|
||||
provider=req.provider,
|
||||
api_base_url=req.api_base_url,
|
||||
api_key_enc=encrypt(req.api_key) if req.api_key else None,
|
||||
model_version=req.model_version,
|
||||
temperature=req.temperature,
|
||||
max_tokens=req.max_tokens,
|
||||
timeout_seconds=req.timeout_seconds,
|
||||
is_default=req.is_default,
|
||||
is_enabled=1,
|
||||
)
|
||||
db.add(model)
|
||||
await db.commit()
|
||||
await db.refresh(model)
|
||||
return ApiResponse(data=_format_model(model), message="模型添加成功")
|
||||
|
||||
|
||||
@router.put("/{model_id}")
|
||||
async def update_model(model_id: int, req: AIModelUpdateRequest, db=Depends(get_db)):
|
||||
result = await db.execute(select(AIModelConfig).where(AIModelConfig.id == model_id))
|
||||
model = result.scalar_one_or_none()
|
||||
if not model:
|
||||
raise HTTPException(status_code=404, detail="模型不存在")
|
||||
if req.is_default:
|
||||
await db.execute(update(AIModelConfig).where(AIModelConfig.id != model_id).values(is_default=0))
|
||||
for field, val in req.model_dump(exclude_none=True).items():
|
||||
if field == "api_key":
|
||||
model.api_key_enc = encrypt(val) if val else None
|
||||
else:
|
||||
setattr(model, field, val)
|
||||
await db.commit()
|
||||
await db.refresh(model)
|
||||
return ApiResponse(data=_format_model(model), message="更新成功")
|
||||
|
||||
|
||||
@router.delete("/{model_id}")
|
||||
async def delete_model(model_id: int, db=Depends(get_db)):
|
||||
result = await db.execute(select(AIModelConfig).where(AIModelConfig.id == model_id))
|
||||
model = result.scalar_one_or_none()
|
||||
if not model:
|
||||
raise HTTPException(status_code=404, detail="模型不存在")
|
||||
await db.delete(model)
|
||||
await db.commit()
|
||||
return ApiResponse(message="删除成功")
|
||||
|
||||
|
||||
@router.post("/test")
|
||||
async def test_model(req: AIModelTestRequest, db=Depends(get_db)):
|
||||
result = await ai_service.test_model(db, req.model_id, req.test_prompt)
|
||||
return ApiResponse(data=result)
|
||||
|
||||
|
||||
def _format_model(m: AIModelConfig) -> dict:
|
||||
return {
|
||||
"id": m.id, "model_name": m.model_name, "provider": m.provider,
|
||||
"api_base_url": m.api_base_url, "has_api_key": bool(m.api_key_enc),
|
||||
"model_version": m.model_version, "temperature": m.temperature,
|
||||
"max_tokens": m.max_tokens, "timeout_seconds": m.timeout_seconds,
|
||||
"is_default": m.is_default, "is_enabled": m.is_enabled,
|
||||
"created_at": m.created_at.isoformat(),
|
||||
}
|
||||
25
backend/app/api/endpoints/dashboard.py
Normal file
25
backend/app/api/endpoints/dashboard.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""数据看板接口"""
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from app.core.database import get_db
|
||||
from app.schemas import ApiResponse
|
||||
from app.services.stats_service import stats_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def get_dashboard(db=Depends(get_db)):
|
||||
data = await stats_service.get_dashboard(db)
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/token-trend")
|
||||
async def get_token_trend(days: int = Query(default=30, ge=7, le=90), db=Depends(get_db)):
|
||||
trend = await stats_service.get_token_trend(db, days)
|
||||
return ApiResponse(data=trend)
|
||||
|
||||
|
||||
@router.get("/monthly-token-trend")
|
||||
async def get_monthly_token_trend(db=Depends(get_db)):
|
||||
trend = await stats_service.get_monthly_token_trend(db)
|
||||
return ApiResponse(data=trend)
|
||||
170
backend/app/api/endpoints/interactions.py
Normal file
170
backend/app/api/endpoints/interactions.py
Normal file
@@ -0,0 +1,170 @@
|
||||
"""互动记录接口"""
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, Query, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
import io, pandas as pd
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.schemas import ApiResponse
|
||||
from app.services.stats_service import stats_service
|
||||
from app.models import InteractionRecord
|
||||
from sqlalchemy import select, update
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_interactions(
|
||||
page: int = Query(default=1, ge=1),
|
||||
page_size: int = Query(default=20, ge=1, le=100),
|
||||
user_id: Optional[int] = None,
|
||||
interact_type: Optional[str] = None,
|
||||
status: Optional[int] = None,
|
||||
start_date: Optional[str] = None,
|
||||
end_date: Optional[str] = None,
|
||||
keyword: Optional[str] = None,
|
||||
db=Depends(get_db)
|
||||
):
|
||||
result = await stats_service.get_interaction_records(
|
||||
db, page, page_size, user_id, interact_type, status, start_date, end_date, keyword
|
||||
)
|
||||
return ApiResponse(data=result)
|
||||
|
||||
|
||||
@router.post("/{record_id}/retry")
|
||||
async def retry_interaction(record_id: int, db=Depends(get_db)):
|
||||
"""手动重试失败任务"""
|
||||
result = await db.execute(select(InteractionRecord).where(InteractionRecord.id == record_id))
|
||||
record = result.scalar_one_or_none()
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="记录不存在")
|
||||
if record.status != 2:
|
||||
raise HTTPException(status_code=400, detail="只能重试失败的任务")
|
||||
if record.retry_count >= 3:
|
||||
raise HTTPException(status_code=400, detail="已超过最大重试次数(3次)")
|
||||
|
||||
from app.services.news_service import news_service
|
||||
from app.services.ai_service import ai_service
|
||||
from app.models import VirtualUser, UserPersonality
|
||||
|
||||
user_result = await db.execute(select(VirtualUser).where(VirtualUser.id == record.user_id))
|
||||
user = user_result.scalar_one_or_none()
|
||||
if not user or user.status != 2:
|
||||
raise HTTPException(status_code=400, detail="用户未登录,无法重试")
|
||||
|
||||
success, err = False, "未知类型"
|
||||
if record.interact_type == "comment" and record.content:
|
||||
success, err = await news_service.post_comment(db, user, record.article_id, record.article_title or "", record.content)
|
||||
elif record.interact_type == "like":
|
||||
success, err = await news_service.like_news(db, user, record.article_id, org_id="", title=record.article_title or "")
|
||||
elif record.interact_type == "collect":
|
||||
success, err = await news_service.collect_news(db, user, record.article_id, title=record.article_title or "")
|
||||
elif record.interact_type == "forward":
|
||||
success, err = await news_service.forward_news(db, user, record.article_id)
|
||||
|
||||
await db.execute(
|
||||
update(InteractionRecord).where(InteractionRecord.id == record_id).values(
|
||||
status=1 if success else 2,
|
||||
error_msg=None if success else err,
|
||||
retry_count=record.retry_count + 1,
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
return ApiResponse(message="重试成功" if success else f"重试失败: {err}")
|
||||
|
||||
|
||||
@router.get("/export")
|
||||
async def export_interactions(
|
||||
user_id: Optional[int] = None,
|
||||
interact_type: Optional[str] = None,
|
||||
status: Optional[int] = None,
|
||||
start_date: Optional[str] = None,
|
||||
end_date: Optional[str] = None,
|
||||
db=Depends(get_db)
|
||||
):
|
||||
"""导出互动记录"""
|
||||
data = await stats_service.get_interaction_records(
|
||||
db, 1, 10000, user_id, interact_type, status, start_date, end_date
|
||||
)
|
||||
rows = [{
|
||||
"ID": r["id"], "用户昵称": r["user_nickname"], "用户账号": r["user_account"],
|
||||
"文章标题": r["article_title"], "互动类型": r["interact_type_label"],
|
||||
"内容": r["content"] or "", "Token消耗": r["token_consumed"],
|
||||
"状态": r["status_label"], "失败原因": r["error_msg"] or "",
|
||||
"重试次数": r["retry_count"], "执行时间": r["executed_at"],
|
||||
} for r in data["items"]]
|
||||
df = pd.DataFrame(rows)
|
||||
buf = io.BytesIO()
|
||||
df.to_excel(buf, index=False, sheet_name="互动记录")
|
||||
buf.seek(0)
|
||||
return StreamingResponse(
|
||||
buf,
|
||||
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
headers={"Content-Disposition": "attachment; filename=interactions_export.xlsx"}
|
||||
)
|
||||
|
||||
@router.post("/{record_id}/cancel")
|
||||
async def cancel_interaction(record_id: int, db=Depends(get_db)):
|
||||
"""取消互动(取消点赞/收藏/删除评论),转发不支持取消"""
|
||||
from sqlalchemy import select, update
|
||||
from app.models import InteractionRecord, VirtualUser
|
||||
from app.services.news_service import news_service
|
||||
|
||||
# 查找互动记录
|
||||
r = await db.execute(select(InteractionRecord).where(InteractionRecord.id == record_id))
|
||||
record = r.scalar_one_or_none()
|
||||
if not record:
|
||||
return ApiResponse(code=404, message="记录不存在")
|
||||
|
||||
if record.status != 1:
|
||||
return ApiResponse(code=400, message="只能取消成功的互动")
|
||||
|
||||
if record.interact_type == "forward":
|
||||
return ApiResponse(code=400, message="转发互动无法取消")
|
||||
|
||||
if record.interact_type == "read":
|
||||
return ApiResponse(code=400, message="阅读记录无法取消")
|
||||
|
||||
# 查找对应用户
|
||||
ur = await db.execute(select(VirtualUser).where(VirtualUser.id == record.user_id))
|
||||
user = ur.scalar_one_or_none()
|
||||
if not user:
|
||||
return ApiResponse(code=404, message="用户不存在")
|
||||
|
||||
# 执行取消
|
||||
ok = False
|
||||
err = ""
|
||||
if record.interact_type in ("like",):
|
||||
ok, err = await news_service.cancel_like(
|
||||
db, user,
|
||||
news_id=record.article_id or "",
|
||||
org_id=record.session_id or "", # session_id 字段暂存 org_id
|
||||
title=record.article_title or "",
|
||||
)
|
||||
elif record.interact_type == "collect":
|
||||
ok, err = await news_service.cancel_collect(
|
||||
db, user,
|
||||
news_id=record.article_id or "",
|
||||
title=record.article_title or "",
|
||||
)
|
||||
elif record.interact_type in ("comment", "reply"):
|
||||
comment_id = record.platform_record_id or ""
|
||||
if not comment_id:
|
||||
return ApiResponse(code=400, message="缺少评论ID,无法删除")
|
||||
ok, err = await news_service.cancel_comment(
|
||||
db, user,
|
||||
news_id=record.article_id or "",
|
||||
comment_id=comment_id,
|
||||
)
|
||||
|
||||
if ok:
|
||||
# 更新状态为手动取消(status=3)
|
||||
await db.execute(
|
||||
update(InteractionRecord).where(InteractionRecord.id == record_id).values(
|
||||
status=3, error_msg="手动取消"
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
return ApiResponse(message="取消成功")
|
||||
else:
|
||||
return ApiResponse(code=500, message=f"取消失败: {err}")
|
||||
83
backend/app/api/endpoints/logs.py
Normal file
83
backend/app/api/endpoints/logs.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""日志管理接口"""
|
||||
import os
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, Query, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from sqlalchemy import select, func, and_
|
||||
from app.core.database import get_db
|
||||
from app.schemas import ApiResponse
|
||||
from app.models import LoginLog
|
||||
from app.core.config import settings
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/login")
|
||||
async def get_login_logs(
|
||||
page: int = Query(default=1, ge=1),
|
||||
page_size: int = Query(default=50, ge=1, le=200),
|
||||
user_id: Optional[int] = None,
|
||||
action: Optional[str] = None,
|
||||
db=Depends(get_db)
|
||||
):
|
||||
query = select(LoginLog)
|
||||
conditions = []
|
||||
if user_id:
|
||||
conditions.append(LoginLog.user_id == user_id)
|
||||
if action:
|
||||
conditions.append(LoginLog.action == action)
|
||||
if conditions:
|
||||
query = query.where(and_(*conditions))
|
||||
|
||||
total = (await db.execute(select(func.count()).select_from(query.subquery()))).scalar()
|
||||
query = query.order_by(LoginLog.created_at.desc()).offset((page - 1) * page_size).limit(page_size)
|
||||
result = await db.execute(query)
|
||||
logs = result.scalars().all()
|
||||
|
||||
items = [{
|
||||
"id": l.id, "user_id": l.user_id, "user_account": l.user_account,
|
||||
"action": l.action, "session_id": l.session_id,
|
||||
"error_msg": l.error_msg, "created_at": l.created_at.isoformat()
|
||||
} for l in logs]
|
||||
return ApiResponse(data={"total": total, "page": page, "page_size": page_size, "items": items})
|
||||
|
||||
|
||||
@router.get("/files")
|
||||
async def list_log_files():
|
||||
"""列出日志文件"""
|
||||
log_dir = settings.LOG_DIR
|
||||
files = []
|
||||
if os.path.exists(log_dir):
|
||||
for fname in sorted(os.listdir(log_dir), reverse=True):
|
||||
if fname.endswith(".log"):
|
||||
fpath = os.path.join(log_dir, fname)
|
||||
size = os.path.getsize(fpath)
|
||||
files.append({"name": fname, "size": size,
|
||||
"size_kb": round(size / 1024, 1)})
|
||||
return ApiResponse(data=files)
|
||||
|
||||
|
||||
@router.get("/files/{filename}/tail")
|
||||
async def tail_log_file(filename: str, lines: int = Query(default=100, ge=10, le=1000)):
|
||||
"""读取日志文件末尾"""
|
||||
# 安全校验
|
||||
if ".." in filename or "/" in filename:
|
||||
raise HTTPException(status_code=400, detail="非法文件名")
|
||||
fpath = os.path.join(settings.LOG_DIR, filename)
|
||||
if not os.path.exists(fpath):
|
||||
raise HTTPException(status_code=404, detail="文件不存在")
|
||||
with open(fpath, "r", encoding="utf-8", errors="replace") as f:
|
||||
all_lines = f.readlines()
|
||||
tail = all_lines[-lines:]
|
||||
return ApiResponse(data={"filename": filename, "lines": tail, "total_lines": len(all_lines)})
|
||||
|
||||
|
||||
@router.get("/files/{filename}/download")
|
||||
async def download_log_file(filename: str):
|
||||
"""下载日志文件"""
|
||||
if ".." in filename or "/" in filename:
|
||||
raise HTTPException(status_code=400, detail="非法文件名")
|
||||
fpath = os.path.join(settings.LOG_DIR, filename)
|
||||
if not os.path.exists(fpath):
|
||||
raise HTTPException(status_code=404, detail="文件不存在")
|
||||
return FileResponse(fpath, filename=filename, media_type="text/plain")
|
||||
115
backend/app/api/endpoints/system.py
Normal file
115
backend/app/api/endpoints/system.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""系统设置接口"""
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy import select, update as sql_update
|
||||
from app.core.database import get_db
|
||||
from app.schemas import ApiResponse
|
||||
from app.models import SystemConfig
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/configs")
|
||||
async def get_configs(db=Depends(get_db)):
|
||||
result = await db.execute(select(SystemConfig).order_by(SystemConfig.config_key))
|
||||
configs = result.scalars().all()
|
||||
data = {c.config_key: {"value": c.config_value, "type": c.config_type, "desc": c.description}
|
||||
for c in configs}
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.put("/configs")
|
||||
async def update_configs(body: dict, db=Depends(get_db)):
|
||||
"""批量更新配置"""
|
||||
for key, value in body.items():
|
||||
result = await db.execute(select(SystemConfig).where(SystemConfig.config_key == key))
|
||||
cfg = result.scalar_one_or_none()
|
||||
if cfg:
|
||||
cfg.config_value = str(value)
|
||||
else:
|
||||
db.add(SystemConfig(config_key=key, config_value=str(value)))
|
||||
await db.commit()
|
||||
return ApiResponse(message="配置已保存")
|
||||
|
||||
|
||||
@router.post("/scheduler/toggle")
|
||||
async def toggle_scheduler(body: dict, db=Depends(get_db)):
|
||||
enabled = body.get("enabled", True)
|
||||
result = await db.execute(select(SystemConfig).where(SystemConfig.config_key == "scheduler_enabled"))
|
||||
cfg = result.scalar_one_or_none()
|
||||
if cfg:
|
||||
cfg.config_value = "true" if enabled else "false"
|
||||
await db.commit()
|
||||
return ApiResponse(message=f"调度器已{'启用' if enabled else '暂停'}")
|
||||
|
||||
|
||||
@router.post("/sessions/reset-all")
|
||||
async def reset_all_sessions(db=Depends(get_db)):
|
||||
"""重置所有用户会话"""
|
||||
from app.models import VirtualUser
|
||||
await db.execute(
|
||||
sql_update(VirtualUser).values(status=0, session_token=None, session_expires_at=None)
|
||||
)
|
||||
await db.commit()
|
||||
return ApiResponse(message="所有会话已重置")
|
||||
|
||||
|
||||
@router.post("/login/diagnose")
|
||||
async def diagnose_login(body: dict, db=Depends(get_db)):
|
||||
"""
|
||||
诊断登录接口原始响应 - 临时调试用
|
||||
传入: {"username": "xxx", "password": "xxx"}
|
||||
"""
|
||||
import httpx, hashlib, uuid
|
||||
from datetime import datetime
|
||||
from app.services.news_service import news_service
|
||||
|
||||
cfg = await news_service._client(db)
|
||||
auth = await news_service._auth_url(db)
|
||||
|
||||
username = body.get("username", "")
|
||||
password = body.get("password", "")
|
||||
|
||||
# 构建 formData(与真实登录完全一致)
|
||||
extra = {
|
||||
"username": username,
|
||||
"password": password,
|
||||
"loginType": "password",
|
||||
"grantType": "password",
|
||||
"isRegister": "false",
|
||||
}
|
||||
if cfg.get("clientCode"):
|
||||
extra["clientCode"] = cfg["clientCode"]
|
||||
|
||||
form = news_service._build_form(extra, cfg)
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=15) as c:
|
||||
resp = await c.post(f"{auth}/open/login/token", data=form)
|
||||
|
||||
# 返回完整诊断信息
|
||||
try:
|
||||
resp_json = resp.json()
|
||||
except Exception:
|
||||
resp_json = None
|
||||
|
||||
return ApiResponse(data={
|
||||
"status_code": resp.status_code,
|
||||
"response_text": resp.text[:2000],
|
||||
"response_json": resp_json,
|
||||
"request_url": f"{auth}/open/login/token",
|
||||
"request_form": {k: v if k not in ("password","accessSecret") else "***" for k, v in form.items()},
|
||||
"content_type": resp.headers.get("content-type", ""),
|
||||
})
|
||||
except Exception as e:
|
||||
return ApiResponse(code=500, message=str(e), data={"error": str(e)})
|
||||
|
||||
|
||||
@router.post("/interaction/run-now")
|
||||
async def run_interaction_now(db=Depends(get_db)):
|
||||
"""立即触发一次互动任务(不受时间段限制)"""
|
||||
from app.services.scheduler import scheduler_service
|
||||
try:
|
||||
result = await scheduler_service.run_once_now(db)
|
||||
return ApiResponse(data=result, message="互动任务已触发")
|
||||
except Exception as e:
|
||||
return ApiResponse(code=500, message=f"触发失败: {e}")
|
||||
370
backend/app/api/endpoints/users.py
Normal file
370
backend/app/api/endpoints/users.py
Normal file
@@ -0,0 +1,370 @@
|
||||
"""虚拟用户管理接口"""
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, Query, UploadFile, File, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
import io
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.schemas import ApiResponse, UserCreateRequest, UserUpdateRequest, UserBatchRequest, PersonalityUpdateRequest
|
||||
from app.services.user_service import user_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_users(
|
||||
page: int = Query(default=1, ge=1),
|
||||
page_size: int = Query(default=20, ge=1, le=100),
|
||||
keyword: Optional[str] = Query(default=None),
|
||||
status: Optional[int] = Query(default=None),
|
||||
is_enabled: Optional[int] = Query(default=None),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
"""获取虚拟用户列表"""
|
||||
total, items = await user_service.get_users(db, page, page_size, keyword, status, is_enabled)
|
||||
return ApiResponse(data={"total": total, "page": page, "page_size": page_size, "items": items})
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_user(req: UserCreateRequest, db=Depends(get_db)):
|
||||
"""创建虚拟用户"""
|
||||
user = await user_service.create_user(db, req)
|
||||
return ApiResponse(data=user, message="用户创建成功")
|
||||
|
||||
|
||||
@router.get("/{user_id}")
|
||||
async def get_user(user_id: int, db=Depends(get_db)):
|
||||
"""获取单个用户详情"""
|
||||
total, items = await user_service.get_users(db, 1, 1)
|
||||
from sqlalchemy import select
|
||||
from app.models import VirtualUser, UserPersonality
|
||||
from app.services.user_service import user_service as svc
|
||||
result = await db.execute(select(VirtualUser).where(VirtualUser.id == user_id))
|
||||
user = result.scalar_one_or_none()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="用户不存在")
|
||||
p_result = await db.execute(select(UserPersonality).where(UserPersonality.user_id == user_id))
|
||||
personality = p_result.scalar_one_or_none()
|
||||
return ApiResponse(data=svc._format_user(user, personality))
|
||||
|
||||
|
||||
@router.put("/{user_id}")
|
||||
async def update_user(user_id: int, req: UserUpdateRequest, db=Depends(get_db)):
|
||||
"""更新用户信息(sync_to_platform=true 时同步到目标平台)"""
|
||||
result = await user_service.update_user(db, user_id, req)
|
||||
|
||||
if req.sync_to_platform:
|
||||
from sqlalchemy import select
|
||||
from app.models import VirtualUser as _VU
|
||||
from app.services.news_service import news_service
|
||||
ur = await db.execute(select(_VU).where(_VU.id == user_id))
|
||||
user = ur.scalar_one_or_none()
|
||||
if user and user.status == 2:
|
||||
ok, err = await news_service.update_user_profile(
|
||||
db, user,
|
||||
nick_name=req.nickname,
|
||||
real_name=req.real_name,
|
||||
sex=req.sex,
|
||||
description=req.description,
|
||||
email=req.email,
|
||||
)
|
||||
if not ok:
|
||||
return ApiResponse(data=result,
|
||||
message=f"本地已保存,同步到平台失败: {err}", code=206)
|
||||
|
||||
return ApiResponse(data=result, message="更新成功")
|
||||
|
||||
|
||||
@router.delete("/{user_id}")
|
||||
async def delete_user(user_id: int, db=Depends(get_db)):
|
||||
"""删除用户"""
|
||||
await user_service.delete_user(db, user_id)
|
||||
return ApiResponse(message="删除成功")
|
||||
|
||||
|
||||
@router.post("/batch/action")
|
||||
async def batch_action(req: UserBatchRequest, db=Depends(get_db)):
|
||||
"""批量操作用户"""
|
||||
result = await user_service.batch_action(db, req.user_ids, req.action)
|
||||
return ApiResponse(data=result, message="批量操作成功")
|
||||
|
||||
|
||||
@router.post("/{user_id}/login")
|
||||
async def manual_login(user_id: int, db=Depends(get_db)):
|
||||
"""手动触发用户登录"""
|
||||
from app.services.news_service import news_service
|
||||
from sqlalchemy import select
|
||||
from app.models import VirtualUser
|
||||
result = await db.execute(select(VirtualUser).where(VirtualUser.id == user_id))
|
||||
user = result.scalar_one_or_none()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="用户不存在")
|
||||
success = await news_service.login(db, user)
|
||||
if success:
|
||||
return ApiResponse(message="登录成功")
|
||||
raise HTTPException(status_code=400, detail="登录失败,请检查账号密码")
|
||||
|
||||
|
||||
@router.post("/{user_id}/logout")
|
||||
async def manual_logout(user_id: int, db=Depends(get_db)):
|
||||
"""手动登出"""
|
||||
from app.services.news_service import news_service
|
||||
await news_service.logout(db, user_id)
|
||||
return ApiResponse(message="已登出")
|
||||
|
||||
|
||||
@router.post("/{user_id}/personality/generate")
|
||||
async def generate_personality(user_id: int, db=Depends(get_db)):
|
||||
"""重新生成AI人格"""
|
||||
personality = await user_service.generate_personality(db, user_id)
|
||||
return ApiResponse(data=personality, message="人格生成成功")
|
||||
|
||||
|
||||
@router.put("/{user_id}/personality")
|
||||
async def update_personality(user_id: int, req: PersonalityUpdateRequest, db=Depends(get_db)):
|
||||
"""手动编辑人格属性"""
|
||||
personality = await user_service.update_personality(db, user_id, req)
|
||||
return ApiResponse(data=personality, message="人格更新成功")
|
||||
|
||||
|
||||
@router.get("/excel/template")
|
||||
async def download_template():
|
||||
"""下载Excel导入模板"""
|
||||
content = await user_service.get_excel_template()
|
||||
return StreamingResponse(
|
||||
io.BytesIO(content),
|
||||
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
headers={"Content-Disposition": "attachment; filename=virtual_users_template.xlsx"}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/excel/import")
|
||||
async def import_excel(file: UploadFile = File(...), db=Depends(get_db)):
|
||||
"""Excel批量导入"""
|
||||
if not file.filename.endswith((".xlsx", ".xls")):
|
||||
raise HTTPException(status_code=400, detail="仅支持Excel文件(.xlsx/.xls)")
|
||||
content = await file.read()
|
||||
result = await user_service.import_from_excel(db, content)
|
||||
return ApiResponse(data=result, message=f"导入完成:成功{result['success']}条,失败{result['failed']}条")
|
||||
|
||||
|
||||
@router.get("/excel/export")
|
||||
async def export_excel(db=Depends(get_db)):
|
||||
"""导出用户数据Excel"""
|
||||
content = await user_service.export_to_excel(db)
|
||||
return StreamingResponse(
|
||||
io.BytesIO(content),
|
||||
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
headers={"Content-Disposition": "attachment; filename=virtual_users_export.xlsx"}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/deduplicate")
|
||||
async def deduplicate_users(db=Depends(get_db)):
|
||||
"""删除重复用户(保留最早创建的一条)"""
|
||||
from sqlalchemy import text
|
||||
# 找出重复的账号,保留 id 最小的,删除其他的
|
||||
result = await db.execute(
|
||||
text("""
|
||||
DELETE FROM virtual_users
|
||||
WHERE id NOT IN (
|
||||
SELECT MIN(id) FROM virtual_users GROUP BY account
|
||||
)
|
||||
""")
|
||||
)
|
||||
await db.commit()
|
||||
deleted = result.rowcount
|
||||
return ApiResponse(data={"deleted": deleted}, message=f"已清理 {deleted} 条重复数据")
|
||||
|
||||
|
||||
@router.post("/clear-all")
|
||||
async def clear_all_users(db=Depends(get_db)):
|
||||
"""清空所有用户(慎用)"""
|
||||
from sqlalchemy import text
|
||||
from app.core.redis_client import get_redis
|
||||
await db.execute(text("DELETE FROM user_personalities"))
|
||||
await db.execute(text("DELETE FROM virtual_users"))
|
||||
await db.commit()
|
||||
return ApiResponse(message="已清空所有用户数据")
|
||||
|
||||
|
||||
@router.post("/login-all")
|
||||
async def batch_login_all(db=Depends(get_db)):
|
||||
"""一键登录所有未登录/登录失效的用户"""
|
||||
from sqlalchemy import select
|
||||
from app.services.news_service import news_service
|
||||
from app.models import VirtualUser as _VU
|
||||
from app.core.database import AsyncSessionLocal
|
||||
import asyncio
|
||||
|
||||
# 先用当前 session 查出所有待登录用户 ID
|
||||
result_r = await db.execute(
|
||||
select(_VU.id, _VU.account).where(
|
||||
_VU.is_enabled == 1,
|
||||
_VU.status.in_([0, 3])
|
||||
)
|
||||
)
|
||||
rows = result_r.all()
|
||||
if not rows:
|
||||
return ApiResponse(message="没有需要登录的用户", data={"count": 0})
|
||||
|
||||
user_ids = [r[0] for r in rows]
|
||||
total = len(user_ids)
|
||||
success = failed = 0
|
||||
|
||||
# 每个用户独立 session,避免事务污染
|
||||
async def login_one(uid: int):
|
||||
async with AsyncSessionLocal() as s:
|
||||
try:
|
||||
ur = await s.execute(select(_VU).where(_VU.id == uid))
|
||||
u = ur.scalar_one_or_none()
|
||||
if u:
|
||||
return await news_service.login(s, u)
|
||||
except Exception as e:
|
||||
logger.warning(f"login_one {uid} 异常: {e}")
|
||||
return False
|
||||
return False
|
||||
|
||||
batch_size = 5
|
||||
for i in range(0, total, batch_size):
|
||||
batch_ids = user_ids[i:i+batch_size]
|
||||
results = await asyncio.gather(*[login_one(uid) for uid in batch_ids], return_exceptions=True)
|
||||
for r in results:
|
||||
if r is True: success += 1
|
||||
else: failed += 1
|
||||
if i + batch_size < total:
|
||||
await asyncio.sleep(1) # 批次间隔避免过于集中
|
||||
|
||||
return ApiResponse(
|
||||
message=f"登录完成:成功 {success} 个,失败 {failed} 个",
|
||||
data={"success": success, "failed": failed, "total": total}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/sync-all-profiles")
|
||||
async def sync_all_profiles(db=Depends(get_db)):
|
||||
"""
|
||||
同步所有已登录用户的平台信息(昵称/真实姓名/性别/头像)到本系统
|
||||
从登录 session 中的 token 调用目标平台接口获取最新用户信息
|
||||
"""
|
||||
from sqlalchemy import select, update
|
||||
from app.models import VirtualUser as _VU
|
||||
from app.core.database import AsyncSessionLocal
|
||||
from app.core.redis_client import get_session
|
||||
import httpx, asyncio
|
||||
|
||||
# 查出所有已登录用户
|
||||
result_r = await db.execute(select(_VU).where(_VU.status == 2, _VU.is_enabled == 1))
|
||||
users = result_r.scalars().all()
|
||||
if not users:
|
||||
return ApiResponse(message="没有已登录的用户", data={"synced": 0})
|
||||
|
||||
synced = failed = 0
|
||||
|
||||
async def sync_one(uid: int):
|
||||
"""从登录 session 中提取已缓存的用户信息,直接写入数据库,无需调用外部接口"""
|
||||
async with AsyncSessionLocal() as s:
|
||||
try:
|
||||
sess = await get_session(uid)
|
||||
if not sess:
|
||||
return False
|
||||
platform_uid = sess.get("platform_uid", "")
|
||||
# 登录成功时 session 里已存有用户信息
|
||||
vals = {}
|
||||
if platform_uid: vals["platform_uid"] = platform_uid
|
||||
# session 里的字段(登录时写入)
|
||||
if sess.get("nickname"): vals["nickname"] = sess["nickname"]
|
||||
if sess.get("real_name"): vals["real_name"] = sess["real_name"]
|
||||
if sess.get("sex"): vals["sex"] = int(sess["sex"])
|
||||
if sess.get("avatar"): vals["avatar_url"] = sess["avatar"]
|
||||
if vals:
|
||||
await s.execute(update(_VU).where(_VU.id == uid).values(**vals))
|
||||
await s.commit()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"sync_one {uid} 失败: {e}")
|
||||
return False
|
||||
|
||||
results = await asyncio.gather(*[sync_one(u.id) for u in users], return_exceptions=True)
|
||||
for r in results:
|
||||
if r is True: synced += 1
|
||||
else: failed += 1
|
||||
|
||||
return ApiResponse(
|
||||
message=f"同步完成:成功 {synced} 个,失败/跳过 {failed} 个",
|
||||
data={"synced": synced, "failed": failed, "total": len(users)}
|
||||
)
|
||||
|
||||
@router.post("/{user_id}/upload-avatar")
|
||||
async def upload_avatar(
|
||||
user_id: int,
|
||||
file: UploadFile = File(...),
|
||||
sync_to_platform: bool = Query(default=True),
|
||||
db=Depends(get_db)
|
||||
):
|
||||
"""上传头像并可选同步到目标平台"""
|
||||
from sqlalchemy import select, update
|
||||
from app.models import VirtualUser as _VU
|
||||
from app.services.news_service import news_service
|
||||
|
||||
ur = await db.execute(select(_VU).where(_VU.id == user_id))
|
||||
user = ur.scalar_one_or_none()
|
||||
if not user:
|
||||
return ApiResponse(code=404, message="用户不存在")
|
||||
|
||||
# 读取文件内容
|
||||
file_bytes = await file.read()
|
||||
if len(file_bytes) > 5 * 1024 * 1024:
|
||||
return ApiResponse(code=400, message="头像文件不能超过5MB")
|
||||
|
||||
avatar_url = None
|
||||
|
||||
if sync_to_platform and user.status == 2:
|
||||
# 上传到目标平台
|
||||
ok, result = await news_service.upload_avatar(db, user, file_bytes, file.filename)
|
||||
if ok:
|
||||
avatar_url = result
|
||||
else:
|
||||
return ApiResponse(code=500, message=f"头像上传到平台失败: {result}")
|
||||
else:
|
||||
# 仅本地存储(转 base64 或存储到本地)
|
||||
import base64
|
||||
avatar_url = f"data:{file.content_type};base64,{base64.b64encode(file_bytes).decode()}"
|
||||
|
||||
# 更新数据库
|
||||
await db.execute(update(_VU).where(_VU.id == user_id).values(avatar_url=avatar_url))
|
||||
await db.commit()
|
||||
|
||||
# 如果已同步到平台,再调用 update_user_profile 更新头像字段
|
||||
if sync_to_platform and user.status == 2 and avatar_url:
|
||||
await news_service.update_user_profile(db, user, avatar=avatar_url)
|
||||
|
||||
return ApiResponse(data={"avatar_url": avatar_url}, message="头像更新成功")
|
||||
@router.post("/logout-all")
|
||||
async def batch_logout_all(db=Depends(get_db)):
|
||||
"""一键登出所有已登录用户"""
|
||||
from sqlalchemy import select, update
|
||||
from app.models import VirtualUser as _VU
|
||||
from app.core.redis_client import delete_session
|
||||
|
||||
result_r = await db.execute(
|
||||
select(_VU.id).where(_VU.status == 2, _VU.is_enabled == 1)
|
||||
)
|
||||
rows = result_r.all()
|
||||
if not rows:
|
||||
return ApiResponse(message="没有已登录的用户", data={"count": 0})
|
||||
|
||||
count = 0
|
||||
for row in rows:
|
||||
try:
|
||||
await delete_session(row[0])
|
||||
count += 1
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 更新所有用户状态为未登录
|
||||
await db.execute(
|
||||
update(_VU).where(_VU.status == 2).values(status=0)
|
||||
)
|
||||
await db.commit()
|
||||
return ApiResponse(message=f"已登出 {count} 个用户", data={"count": count})
|
||||
Reference in New Issue
Block a user