diff --git a/server/marketserver/api/v1/shopcart/shopcart.go b/server/marketserver/api/v1/shopcart/shopcart.go index 2ff035df..87752323 100644 --- a/server/marketserver/api/v1/shopcart/shopcart.go +++ b/server/marketserver/api/v1/shopcart/shopcart.go @@ -139,7 +139,6 @@ func (this *ShopCartApi) Add(c *gin.Context) { cartDb := common.NewShopCart() dirty := false if q5.DecodeJson(ds.GetByName("data"), cartDb) == nil { - //newGoods := []*common.ShopCartGoods{} for _, val := range reqJson.Tokens { netId := q5.SafeToInt32(reqJson.NetId) contractAddress := val.ContractAddress @@ -165,6 +164,14 @@ func (this *ShopCartApi) Add(c *gin.Context) { err1 = err if err == nil { if ds.Next() { + cartDb.CurrSortIdx += 1 + g := common.NewShopCartGoods() + g.NetId = netId + g.ContractAddress = contractAddress + g.TokenId = tokenId + g.SortIdx = cartDb.CurrSortIdx + g.AddTime = q5.ToInt32(f5.GetApp().GetRealSeconds()) + q5.AppendSlice(&cartDb.Goods, g) } else { } } @@ -180,6 +187,7 @@ func (this *ShopCartApi) Add(c *gin.Context) { } } if dirty { + cartDb.DeleteExcessData() this.save(openId, cartDb) } c.JSON(200, rspObj) diff --git a/server/marketserver/common/types.go b/server/marketserver/common/types.go index 4463d6f8..e503567e 100644 --- a/server/marketserver/common/types.go +++ b/server/marketserver/common/types.go @@ -126,8 +126,20 @@ func (this* ShopCart) GetGoods(netId int32, contractAddress string, tokenId stri return nil } +func (this* ShopCart) DeleteExcessData() { + q5.Sort[ShopCartGoods](this.Goods, + func (a *ShopCartGoods, b *ShopCartGoods) bool{ + return a.SortIdx < b.SortIdx + }) +} + func NewShopCart() *ShopCart { p := new(ShopCart) q5.NewSlice(&p.Goods, 0, 10) return p } + +func NewShopCartGoods() *ShopCartGoods { + p := new(ShopCartGoods) + return p +} diff --git a/server/marketserver/constant/constant.go b/server/marketserver/constant/constant.go index ef999ac8..2aab0e07 100644 --- a/server/marketserver/constant/constant.go +++ b/server/marketserver/constant/constant.go @@ -33,3 +33,7 @@ const ( CONTRACT_NAME_CFHero = "CFHero" CONTRACT_NAME_GoldBrick = "GoldBrick" ) + +const ( + SHOPCART_MAX_GOODS_NUM = 20 +)