You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
4.6 KiB
172 lines
4.6 KiB
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"health-ai/internal/api/middleware"
|
|
"health-ai/internal/service"
|
|
"health-ai/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ProductHandler struct {
|
|
productService *service.ProductService
|
|
}
|
|
|
|
func NewProductHandler() *ProductHandler {
|
|
return &ProductHandler{
|
|
productService: service.NewProductService(),
|
|
}
|
|
}
|
|
|
|
// GetProducts 获取产品列表
|
|
// @Summary 获取产品列表
|
|
// @Tags 产品
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param page query int false "页码,默认1"
|
|
// @Param page_size query int false "每页数量,默认20"
|
|
// @Success 200 {object} response.Response{data=service.ProductListResponse}
|
|
// @Router /api/products [get]
|
|
func (h *ProductHandler) GetProducts(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
result, err := h.productService.GetProducts(page, pageSize)
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result)
|
|
}
|
|
|
|
// GetProduct 获取产品详情
|
|
// @Summary 获取产品详情
|
|
// @Tags 产品
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "产品ID"
|
|
// @Success 200 {object} response.Response{data=model.Product}
|
|
// @Router /api/products/{id} [get]
|
|
func (h *ProductHandler) GetProduct(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
if err != nil {
|
|
response.BadRequest(c, "无效的产品ID")
|
|
return
|
|
}
|
|
|
|
product, err := h.productService.GetProductByID(uint(id))
|
|
if err != nil {
|
|
response.Error(c, 404, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, product)
|
|
}
|
|
|
|
// GetProductsByCategory 按分类获取产品
|
|
// @Summary 按分类获取产品
|
|
// @Tags 产品
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param category query string true "分类名称"
|
|
// @Success 200 {object} response.Response{data=[]model.Product}
|
|
// @Router /api/products/category [get]
|
|
func (h *ProductHandler) GetProductsByCategory(c *gin.Context) {
|
|
category := c.Query("category")
|
|
if category == "" {
|
|
response.BadRequest(c, "请指定分类")
|
|
return
|
|
}
|
|
|
|
products, err := h.productService.GetProductsByCategory(category)
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, products)
|
|
}
|
|
|
|
// GetRecommendedProducts 获取推荐产品(基于用户体质)
|
|
// @Summary 获取推荐产品
|
|
// @Tags 产品
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Success 200 {object} response.Response{data=service.ProductRecommendResponse}
|
|
// @Router /api/products/recommend [get]
|
|
func (h *ProductHandler) GetRecommendedProducts(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
result, err := h.productService.GetRecommendedProducts(userID)
|
|
if err != nil {
|
|
response.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, result)
|
|
}
|
|
|
|
// SearchProducts 搜索产品
|
|
// @Summary 搜索产品
|
|
// @Tags 产品
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param keyword query string true "搜索关键词"
|
|
// @Success 200 {object} response.Response{data=[]model.Product}
|
|
// @Router /api/products/search [get]
|
|
func (h *ProductHandler) SearchProducts(c *gin.Context) {
|
|
keyword := c.Query("keyword")
|
|
if keyword == "" {
|
|
response.BadRequest(c, "请输入搜索关键词")
|
|
return
|
|
}
|
|
|
|
products, err := h.productService.SearchProducts(keyword)
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, products)
|
|
}
|
|
|
|
// SyncPurchase 同步商城购买记录
|
|
// @Summary 同步商城购买记录
|
|
// @Tags 商城同步
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body service.PurchaseSyncRequest true "购买记录"
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/sync/purchase [post]
|
|
func (h *ProductHandler) SyncPurchase(c *gin.Context) {
|
|
// TODO: 验证同步密钥
|
|
|
|
var req service.PurchaseSyncRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
|
|
if err := h.productService.SyncPurchase(&req); err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "同步成功", nil)
|
|
}
|
|
|
|
// GetPurchaseHistory 获取购买历史
|
|
// @Summary 获取购买历史
|
|
// @Tags 产品
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer Token"
|
|
// @Success 200 {object} response.Response{data=[]model.PurchaseHistory}
|
|
// @Router /api/user/purchase-history [get]
|
|
func (h *ProductHandler) GetPurchaseHistory(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
history, err := h.productService.GetPurchaseHistory(userID)
|
|
if err != nil {
|
|
response.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
response.Success(c, history)
|
|
}
|
|
|