This commit is contained in:
2025-07-28 01:46:39 +08:00
parent b03129667a
commit 357639462a
219 changed files with 21634 additions and 8138 deletions

View File

@@ -1,11 +1,14 @@
package handlers
import (
"strconv"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"tyapi-server/internal/application/user"
"tyapi-server/internal/application/user/dto/commands"
"tyapi-server/internal/application/user/dto/queries"
"tyapi-server/internal/shared/interfaces"
"tyapi-server/internal/shared/middleware"
)
@@ -240,6 +243,109 @@ func (h *UserHandler) ResetPassword(c *gin.Context) {
h.response.Success(c, nil, "密码重置成功")
}
// ListUsers 管理员查看用户列表
// @Summary 管理员查看用户列表
// @Description 管理员查看用户列表,支持分页和筛选
// @Tags 用户管理
// @Accept json
// @Produce json
// @Security Bearer
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页数量" default(10)
// @Param phone query string false "手机号筛选"
// @Param user_type query string false "用户类型筛选" Enums(user,admin)
// @Param is_active query bool false "是否激活筛选"
// @Param is_certified query bool false "是否已认证筛选"
// @Param company_name query string false "企业名称筛选"
// @Param start_date query string false "开始日期" format(date)
// @Param end_date query string false "结束日期" format(date)
// @Success 200 {object} responses.UserListResponse "用户列表"
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 403 {object} map[string]interface{} "权限不足"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/users/admin/list [get]
func (h *UserHandler) ListUsers(c *gin.Context) {
// 检查管理员权限
userID := h.getCurrentUserID(c)
if userID == "" {
h.response.Unauthorized(c, "用户未登录")
return
}
// 构建查询参数
query := &queries.ListUsersQuery{
Page: 1,
PageSize: 10,
}
// 从查询参数中获取筛选条件
if page := c.Query("page"); page != "" {
if pageNum, err := strconv.Atoi(page); err == nil && pageNum > 0 {
query.Page = pageNum
}
}
if pageSize := c.Query("page_size"); pageSize != "" {
if size, err := strconv.Atoi(pageSize); err == nil && size > 0 && size <= 100 {
query.PageSize = size
}
}
query.Phone = c.Query("phone")
query.UserType = c.Query("user_type")
query.CompanyName = c.Query("company_name")
query.StartDate = c.Query("start_date")
query.EndDate = c.Query("end_date")
// 处理布尔值参数
if isActive := c.Query("is_active"); isActive != "" {
if active, err := strconv.ParseBool(isActive); err == nil {
query.IsActive = &active
}
}
if isCertified := c.Query("is_certified"); isCertified != "" {
if certified, err := strconv.ParseBool(isCertified); err == nil {
query.IsCertified = &certified
}
}
// 调用应用服务
resp, err := h.appService.ListUsers(c.Request.Context(), query)
if err != nil {
h.logger.Error("获取用户列表失败", zap.Error(err))
h.response.BadRequest(c, "获取用户列表失败")
return
}
h.response.Success(c, resp, "获取用户列表成功")
}
// GetUserStats 管理员获取用户统计信息
// @Summary 管理员获取用户统计信息
// @Description 管理员获取用户统计信息,包括总用户数、活跃用户数、已认证用户数
// @Tags 用户管理
// @Accept json
// @Produce json
// @Security Bearer
// @Success 200 {object} responses.UserStatsResponse "用户统计信息"
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 403 {object} map[string]interface{} "权限不足"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/users/admin/stats [get]
func (h *UserHandler) GetUserStats(c *gin.Context) {
// 调用应用服务
resp, err := h.appService.GetUserStats(c.Request.Context())
if err != nil {
h.logger.Error("获取用户统计信息失败", zap.Error(err))
h.response.BadRequest(c, "获取用户统计信息失败")
return
}
h.response.Success(c, resp, "获取用户统计信息成功")
}
// getCurrentUserID 获取当前用户ID
func (h *UserHandler) getCurrentUserID(c *gin.Context) string {
if userID, exists := c.Get("user_id"); exists {