fix(phase2a): 代码审查修复 - 8项关键/重要问题
安全修复: - WebSocket Token 增加 Redis 有效性校验(已登出用户无法建立 WS) 功能修复: - GetRecommendFriends 改为批量查询,正确返回用户名/昵称/头像 - 上下线通知:OnlineService 通过接口注入获取好友列表推送状态变更 - 管理端在线用户 API 补充用户名信息 代码质量: - 所有 json.Marshal/Redis 错误增加检查与日志 - ContactController 13 个 endpoint 统一走 handleError 业务错误映射 - 管理端 Controller 补全包注释、函数注释和结构化日志 - 前端 5 个联系人页面 avatar 工具函数抽取到 utils/avatar.js Made-with: Cursor
This commit is contained in:
@@ -3,24 +3,72 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
authModel "github.com/echochat/backend/app/auth/model"
|
||||
wsApp "github.com/echochat/backend/app/ws"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OnlineUserInfo 在线用户信息(包含用户名,用于管理端展示)
|
||||
type OnlineUserInfo struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// OnlineManageService 管理端在线状态查询服务
|
||||
type OnlineManageService struct {
|
||||
onlineService *wsApp.OnlineService
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewOnlineManageService(onlineService *wsApp.OnlineService) *OnlineManageService {
|
||||
return &OnlineManageService{onlineService: onlineService}
|
||||
// NewOnlineManageService 创建 OnlineManageService 实例
|
||||
func NewOnlineManageService(onlineService *wsApp.OnlineService, db *gorm.DB) *OnlineManageService {
|
||||
return &OnlineManageService{
|
||||
onlineService: onlineService,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *OnlineManageService) GetOnlineUsers(ctx context.Context) ([]int64, error) {
|
||||
// GetOnlineUsers 获取在线用户列表(含用户名)
|
||||
func (s *OnlineManageService) GetOnlineUsers(ctx context.Context) ([]OnlineUserInfo, error) {
|
||||
funcName := "service.online_manage_service.GetOnlineUsers"
|
||||
logs.Debug(ctx, funcName, "获取在线用户列表")
|
||||
return s.onlineService.GetOnlineUserIDs(ctx)
|
||||
|
||||
ids, err := s.onlineService.GetOnlineUserIDs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return []OnlineUserInfo{}, nil
|
||||
}
|
||||
|
||||
var users []authModel.User
|
||||
if err := s.db.WithContext(ctx).
|
||||
Model(&authModel.User{}).
|
||||
Select("id, username").
|
||||
Where("id IN ?", ids).
|
||||
Find(&users).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "查询在线用户信息失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userMap := make(map[int64]string, len(users))
|
||||
for _, u := range users {
|
||||
userMap[u.ID] = u.Username
|
||||
}
|
||||
|
||||
result := make([]OnlineUserInfo, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
result = append(result, OnlineUserInfo{
|
||||
UserID: id,
|
||||
Username: userMap[id],
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetOnlineCount 获取在线用户总数
|
||||
func (s *OnlineManageService) GetOnlineCount(ctx context.Context) (int64, error) {
|
||||
return s.onlineService.GetOnlineCount(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user