web
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"dianshang/pkg/response"
|
||||
"dianshang/pkg/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -26,11 +27,35 @@ func NewProductHandler(productService *service.ProductService) *ProductHandler {
|
||||
func (h *ProductHandler) GetProductList(c *gin.Context) {
|
||||
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
||||
pageSize := utils.StringToInt(c.DefaultQuery("page_size", "20"))
|
||||
categoryID := utils.StringToUint(c.Query("category_id"))
|
||||
|
||||
// 支持 category_ids (逗号分隔)或 category_id
|
||||
var categoryID uint
|
||||
if categoryIDsStr := c.Query("category_ids"); categoryIDsStr != "" {
|
||||
// 解析第一个分类ID
|
||||
ids := strings.Split(categoryIDsStr, ",")
|
||||
if len(ids) > 0 {
|
||||
categoryID = utils.StringToUint(strings.TrimSpace(ids[0]))
|
||||
}
|
||||
} else {
|
||||
categoryID = utils.StringToUint(c.Query("category_id"))
|
||||
}
|
||||
|
||||
keyword := c.Query("keyword")
|
||||
minPrice, _ := strconv.ParseFloat(c.Query("min_price"), 64)
|
||||
maxPrice, _ := strconv.ParseFloat(c.Query("max_price"), 64)
|
||||
|
||||
// 库存筛选参数
|
||||
var inStock *bool
|
||||
if inStockStr := c.Query("in_stock"); inStockStr != "" {
|
||||
if inStockStr == "true" {
|
||||
trueVal := true
|
||||
inStock = &trueVal
|
||||
} else if inStockStr == "false" {
|
||||
falseVal := false
|
||||
inStock = &falseVal
|
||||
}
|
||||
}
|
||||
|
||||
// 处理排序参数:将前端传递的数字参数转换为后端期望的字符串参数
|
||||
sortParam := c.Query("sort")
|
||||
sortTypeParam := c.Query("sortType")
|
||||
@@ -52,7 +77,7 @@ func (h *ProductHandler) GetProductList(c *gin.Context) {
|
||||
sortType = "desc"
|
||||
}
|
||||
|
||||
products, pagination, err := h.productService.GetProductList(page, pageSize, categoryID, keyword, minPrice, maxPrice, sort, sortType)
|
||||
products, pagination, err := h.productService.GetProductList(page, pageSize, categoryID, keyword, minPrice, maxPrice, inStock, sort, sortType)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
@@ -137,7 +162,20 @@ func (h *ProductHandler) DeleteProduct(c *gin.Context) {
|
||||
|
||||
// GetCategories 鑾峰彇鍒嗙被鍒楄〃
|
||||
func (h *ProductHandler) GetCategories(c *gin.Context) {
|
||||
categories, err := h.productService.GetCategories()
|
||||
// 支持平台筛选参数:platform=web 或 platform=miniprogram
|
||||
platform := c.Query("platform")
|
||||
|
||||
var categories []model.Category
|
||||
var err error
|
||||
|
||||
if platform != "" {
|
||||
// 根据平台获取分类
|
||||
categories, err = h.productService.GetCategoriesByPlatform(platform)
|
||||
} else {
|
||||
// 获取所有分类
|
||||
categories, err = h.productService.GetCategories()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user