Ai_GirlFriend/lover/routers/huanxin.py

58 lines
1.8 KiB
Python
Raw Normal View History

from typing import Optional
2026-02-02 20:08:28 +08:00
from fastapi import APIRouter, Depends
from lover.deps import get_current_user, AuthedUser
from lover.response import success_response
router = APIRouter()
@router.post("/api/huanxin/getToken")
def get_huanxin_token(user: AuthedUser = Depends(get_current_user)):
"""获取环信token"""
return success_response({
"token": "mock_huanxin_token_" + str(user.id),
"expires_in": 3600
})
@router.post("/api/huanxin/register")
def register_huanxin_user(user: AuthedUser = Depends(get_current_user)):
"""注册环信用户"""
return success_response({"message": "注册成功"})
@router.post("/api/huanxin/online")
def set_huanxin_online(
payload: Optional[dict] = None,
user: AuthedUser = Depends(get_current_user),
):
"""获取环信用户在线状态(兼容前端批量查询)"""
payload = payload or {}
user_ids_raw = payload.get("user_ids")
# 前端好友列表会传入 user_ids=36,40,41期待返回数组并支持 .find()
if user_ids_raw:
ids = []
for part in str(user_ids_raw).split(","):
part = part.strip()
if not part:
continue
try:
ids.append(int(part))
except ValueError:
continue
return success_response([
{"id": uid, "is_online": False} for uid in ids
])
# 兼容旧调用:不传 user_ids 时,返回当前用户在线信息
return success_response({"status": "online", "user_id": user.id})
2026-02-02 20:08:28 +08:00
@router.get("/api/huanxin/user_info")
def get_huanxin_user_info(user: AuthedUser = Depends(get_current_user)):
"""获取环信用户信息"""
return success_response({
"username": f"user_{user.id}",
"nickname": user.nickname,
"avatar": ""
})