Ai_GirlFriend/lover/routers/huanxin.py

58 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Optional
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})
@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": ""
})