package api import ( "encoding/json" "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/context" "go.uber.org/zap" "io" "pet-house.com/core/server/web/web_iris" "pet-house.com/core/server/zap_server" "strconv" ) type GoodsListRequest struct { Pid int64 } type GoodsDetail struct { Id int64 `json:"id"` //商品ID Name string `json:"name"` //商品名称 GoodsType int `json:"goodsType"` //商品类型 1基础服务 2附加产品 3附加服务 Time string `json:"time"` //时间 Price int32 `json:"price"` //价格 ParentId int64 `json:"parentId"` //商品父ID BuyMany int `json:"buyMany"` //同类型是否可购买多个 0否 1是 GoodsDetail string `json:"goodsDetail"` //商品详情 图片URL GoodsIcon string `json:"goodsIcon"` //iconUrl GoodsSubList []GoodsDetail `json:"goodsSubList,omitempty"` } type GoodsListResponse struct { GoodsList []GoodsDetail `json:"goodsList"` } // 商品列表 func (p DefParty) goodsList() web_iris.Party { return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) { index.Post(GoodsBase+"/goodsList", func(ctx *context.Context) { headerBaseInfo := GetHeaderBaseInfo(ctx) body, _ := io.ReadAll(ctx.Request().Body) var goodsListRequest GoodsListRequest json.Unmarshal(body, &goodsListRequest) userPetInfo := GetUserPet(headerBaseInfo.Uid, goodsListRequest.Pid) if userPetInfo.PetInfo.Id == 0 { PetNotExistError.Fail(ctx, goodsListRequest) return } //先取默认的商品 key := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + "0" + "0" goodsList := getPetGoodsList(key) key1 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + "0" goodsList = append(goodsList, getPetGoodsList(key1)...) key2 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + strconv.Itoa(userPetInfo.PetBaseInfo.Hair) goodsList = append(goodsList, getPetGoodsList(key2)...) key3 := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + strconv.Itoa(userPetInfo.PetBaseInfo.Hair) goodsList = append(goodsList, getPetGoodsList(key3)...) key4 := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + "0" goodsList = append(goodsList, getPetGoodsList(key4)...) var goodsT1 []GoodsDetail var goodsT2 []GoodsDetail var goodsT3 []GoodsDetail for _, value := range goodsList { if value.GoodsType == 2 { goodsT2 = append(goodsT2, value) } if value.GoodsType == 3 { goodsT3 = append(goodsT3, value) } if value.GoodsType == 1 { goodsT1 = append(goodsT1, value) } } goodsT1Main := GoodsDetail{ Name: "基础服务", GoodsSubList: goodsT1, GoodsType: 1, } goodsT2Main := GoodsDetail{ Name: "附加产品", GoodsSubList: goodsT2, GoodsType: 2, } goodsT3Main := GoodsDetail{ Name: "附加服务", GoodsSubList: goodsT3, GoodsType: 3, } var goodsTs []GoodsDetail goodsTs = append(goodsTs, goodsT1Main, goodsT2Main, goodsT3Main) zap_server.ZAPLOG.Info("key", zap.Any("key1", key), zap.Any("key2", key1), zap.Any("key3", key2), zap.Any("key4", key3), zap.Any("key5", key4)) Success(ctx, goodsListRequest, GoodsListResponse{goodsTs}) }) }} } func getPetGoodsList(key string) []GoodsDetail { var goodsList []GoodsDetail baseGoodsList := PetGoodsMap[key] for _, value := range baseGoodsList { goods := GoodsMap[value.GoodsId] goodsDetail := GoodsDetail{ Id: goods.Id, Name: goods.Name, GoodsType: goods.GoodsType, Time: goods.Time, Price: goods.Price, BuyMany: goods.BuyMany, GoodsDetail: goods.GoodsDetail, GoodsIcon: goods.GoodsIcon, GoodsSubList: nil, } for _, goodsSub := range GoodsMap { if goodsSub.ParentId == goodsDetail.Id { goodsDetail.GoodsSubList = append(goodsDetail.GoodsSubList, GoodsDetail{ Id: goodsSub.Id, Name: goodsSub.Name, GoodsType: goodsSub.GoodsType, Time: goodsSub.Time, Price: goodsSub.Price, BuyMany: goodsSub.BuyMany, GoodsDetail: goodsSub.GoodsDetail, GoodsIcon: goodsSub.GoodsIcon, }) } } goodsList = append(goodsList, goodsDetail) } return goodsList } // 商品详情 func (p DefParty) goodsDetail() web_iris.Party { return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) { index.Post(GoodsBase+"/goodsDetail", func(ctx *context.Context) { //暂留 Success(ctx, nil, nil) }) }} }