package api import ( "encoding/json" "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/context" "io" "pet-house.com/core/server/web/web_iris" "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) + strconv.Itoa(userPetInfo.PetBaseInfo.Size) goodsList := getPetGoodsList(key) key1 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetBaseInfo.Size) goodsList = append(goodsList, getPetGoodsList(key1)...) Success(ctx, goodsListRequest, GoodsListResponse{goodsList}) }) }} } 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) }) }} }