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:
bujinyuan
2026-03-02 18:48:28 +08:00
parent 8ec0261427
commit 2c59500e27
26 changed files with 632 additions and 153 deletions

View File

@@ -1,41 +1,56 @@
// Package controller 提供 admin 模块的 HTTP 接口处理
package controller
import (
"strconv"
"github.com/echochat/backend/app/admin/service"
"github.com/echochat/backend/pkg/logs"
"github.com/echochat/backend/pkg/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// ContactManageController 联系人关系管理控制器(管理端)
type ContactManageController struct {
contactManageService *service.ContactManageService
}
// NewContactManageController 创建 ContactManageController 实例
func NewContactManageController(contactManageService *service.ContactManageService) *ContactManageController {
return &ContactManageController{contactManageService: contactManageService}
}
// GetAllContacts GET /api/v1/admin/contacts
// GetAllContacts 获取所有好友关系列表(分页)
// GET /api/v1/admin/contacts
func (ctl *ContactManageController) GetAllContacts(c *gin.Context) {
funcName := "admin.contact_manage_controller.GetAllContacts"
ctx := c.Request.Context()
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
contacts, total, err := ctl.contactManageService.GetAllFriendships(ctx, page, pageSize)
if err != nil {
logs.Error(ctx, funcName, "获取好友关系列表失败", zap.Error(err))
utils.ResponseError(c, "获取好友关系列表失败")
return
}
logs.Info(ctx, funcName, "获取好友关系列表成功",
zap.Int64("total", total), zap.Int("page", page))
utils.ResponseOK(c, gin.H{
"list": contacts,
"total": total,
})
}
// DeleteContact DELETE /api/v1/admin/contacts/:id
// DeleteContact 删除好友关系
// DELETE /api/v1/admin/contacts/:id
func (ctl *ContactManageController) DeleteContact(c *gin.Context) {
funcName := "admin.contact_manage_controller.DeleteContact"
ctx := c.Request.Context()
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
utils.ResponseBadRequest(c, "ID 格式错误")
@@ -43,8 +58,12 @@ func (ctl *ContactManageController) DeleteContact(c *gin.Context) {
}
if err := ctl.contactManageService.DeleteFriendship(ctx, id); err != nil {
logs.Error(ctx, funcName, "删除好友关系失败",
zap.Int64("friendship_id", id), zap.Error(err))
utils.ResponseError(c, "删除好友关系失败")
return
}
logs.Info(ctx, funcName, "删除好友关系成功", zap.Int64("friendship_id", id))
utils.ResponseOK(c, nil)
}