package recook import ( "errors" "net/url" "strconv" ) type goods struct { } var Goods *goods const ( actionGoodsLists = "/lives/goods/list" actionGoodsInfos = "/lives/goods/infos" actionGoodsBrandList = "/lives/goods/brandlist" ) func init() { Goods = &goods{} } type GoodsInfo struct { ID uint `json:"id"` GoodsName string `json:"goodsName"` BrandImg string `json:"brandImg"` BrandName string `json:"brandName"` BrandId uint `json:"brandId"` Description string `json:"description"` Inventory uint `json:"inventory"` // 总库存 SalesVolume uint `json:"salesVolume"` // 总销量 MainPhotoURL string `json:"mainPhotoUrl"` // 主图链接 PromotionName string `json:"promotionName"` OriginalPrice string `json:"originalPrice"` DiscountPrice string `json:"discountPrice"` Commission string `json:"commission"` Tags []string `json:"tags"` // 为产品打上标签,比如 "新人特惠" "限时特卖" Percent uint `json:"percent"` // 活动的%-1代表不是活动 StartTime string `json:"startTime"` // 开始时间 EndTime string `json:"endTime"` // 结束时间 Coupon string `gorm:"coupon" json:"coupon"` } // @Title 根据商品id批量获取商品详情 func (g *goods) GetListByIds(ids []uint) (*[]GoodsInfo, error) { goodsInfos := &[]GoodsInfo{} if len(ids) == 0 { return nil, errors.New("商品id不能为空") } strIds := []string{} for _, id := range ids { strIds = append(strIds, strconv.FormatUint(uint64(id), 10)) } err := RecookClient.Exec(actionGoodsInfos, url.Values{ "ids": strIds, }, goodsInfos) return goodsInfos, err } type GoodsList struct { Total int `json:"total"` List []GoodsInfo `json:"list"` } // @Title 根据关键字获取商品列表 func (g *goods) GetListByKeyword(keyword string, page, limit int) (*GoodsList, error) { goodsInfos := &GoodsList{} if keyword == "" { return nil, errors.New("搜索关键字不能为空") } err := RecookClient.Exec(actionGoodsLists, url.Values{ "keyword": []string{keyword}, "page": []string{strconv.Itoa(page)}, "limit": []string{strconv.Itoa(limit)}, }, goodsInfos) return goodsInfos, err } // @Title 根据品牌id获取商品列表 func (g *goods) GetListByBrandId(brandId uint, page, limit int) (*GoodsList, error) { goodsInfos := &GoodsList{} if brandId == 0 { return nil, errors.New("品牌id不能为空") } err := RecookClient.Exec(actionGoodsLists, url.Values{ "brandId": []string{strconv.FormatUint(uint64(brandId), 10)}, "page": []string{strconv.Itoa(page)}, "limit": []string{strconv.Itoa(limit)}, }, goodsInfos) return goodsInfos, err } type BandInfo struct { Id uint `json:"id"` Name string `json:"name"` LogoUrl string `json:"logoUrl"` } type BrandList struct { Total int `json:"total"` List []BandInfo `json:"list"` } // @Title 获取品牌列表 func (g *goods) GetBrandList(page, limit int) (*BrandList, error) { brandInfos := &BrandList{} err := RecookClient.Exec(actionGoodsBrandList, url.Values{ "page": []string{strconv.Itoa(page)}, "limit": []string{strconv.Itoa(limit)}, }, brandInfos) return brandInfos, err }