fix
This commit is contained in:
@@ -56,6 +56,7 @@ func NewProductHandler(
|
||||
// @Param is_enabled query bool false "是否启用"
|
||||
// @Param is_visible query bool false "是否可见"
|
||||
// @Param is_package query bool false "是否组合包"
|
||||
// @Param is_subscribed query bool false "是否已订阅(需要认证)"
|
||||
// @Param sort_by query string false "排序字段"
|
||||
// @Param sort_order query string false "排序方向" Enums(asc, desc)
|
||||
// @Success 200 {object} responses.ProductListResponse "获取产品列表成功"
|
||||
@@ -63,6 +64,9 @@ func NewProductHandler(
|
||||
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
|
||||
// @Router /api/v1/products [get]
|
||||
func (h *ProductHandler) ListProducts(c *gin.Context) {
|
||||
// 获取当前用户ID(可选认证)
|
||||
userID := h.getCurrentUserID(c)
|
||||
|
||||
// 解析查询参数
|
||||
page := h.getIntQuery(c, "page", 1)
|
||||
pageSize := h.getIntQuery(c, "page_size", 10)
|
||||
@@ -101,6 +105,17 @@ func (h *ProductHandler) ListProducts(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 订阅状态筛选(需要认证)
|
||||
if userID != "" {
|
||||
if isSubscribed := c.Query("is_subscribed"); isSubscribed != "" {
|
||||
if subscribed, err := strconv.ParseBool(isSubscribed); err == nil {
|
||||
filters["is_subscribed"] = subscribed
|
||||
}
|
||||
}
|
||||
// 添加用户ID到筛选条件
|
||||
filters["user_id"] = userID
|
||||
}
|
||||
|
||||
// 排序字段
|
||||
sortBy := c.Query("sort_by")
|
||||
if sortBy == "" {
|
||||
@@ -141,6 +156,16 @@ func (h *ProductHandler) getIntQuery(c *gin.Context, key string, defaultValue in
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// getCurrentUserID 获取当前用户ID
|
||||
func (h *ProductHandler) getCurrentUserID(c *gin.Context) string {
|
||||
if userID, exists := c.Get("user_id"); exists {
|
||||
if id, ok := userID.(string); ok {
|
||||
return id
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetProductDetail 获取产品详情
|
||||
// @Summary 获取产品详情
|
||||
// @Description 根据产品ID获取产品详细信息
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
type ProductRoutes struct {
|
||||
productHandler *handlers.ProductHandler
|
||||
auth *middleware.JWTAuthMiddleware
|
||||
optionalAuth *middleware.OptionalAuthMiddleware
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
@@ -19,11 +20,13 @@ type ProductRoutes struct {
|
||||
func NewProductRoutes(
|
||||
productHandler *handlers.ProductHandler,
|
||||
auth *middleware.JWTAuthMiddleware,
|
||||
optionalAuth *middleware.OptionalAuthMiddleware,
|
||||
logger *zap.Logger,
|
||||
) *ProductRoutes {
|
||||
return &ProductRoutes{
|
||||
productHandler: productHandler,
|
||||
auth: auth,
|
||||
optionalAuth: optionalAuth,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
@@ -36,7 +39,7 @@ func (r *ProductRoutes) Register(router *sharedhttp.GinRouter) {
|
||||
products := engine.Group("/api/v1/products")
|
||||
{
|
||||
// 获取产品列表(分页+筛选)
|
||||
products.GET("", r.productHandler.ListProducts)
|
||||
products.GET("", r.optionalAuth.Handle(), r.productHandler.ListProducts)
|
||||
|
||||
// 获取产品统计
|
||||
products.GET("/stats", r.productHandler.GetProductStats)
|
||||
|
||||
Reference in New Issue
Block a user