Skip to content

Commit

Permalink
限购商品
Browse files Browse the repository at this point in the history
  • Loading branch information
iscod committed May 2, 2022
1 parent 42c5747 commit de7bd95
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
28 changes: 21 additions & 7 deletions dd/cart.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,30 @@ func (s *DingdongSession) GetCart(result gjson.Result) error {
return nil
}

type GetCartPram struct {
Uid string `json:"uid"`
DeviceType string `json:"deviceType"`
StoreList []Store `json:"storeList"`
DeliveryType int `json:"deliveryType"`
HomePagelongitude string `json:"homePagelongitude"`
HomePagelatitude string `json:"homePagelatitude"`
}

func (s *DingdongSession) CheckCart() error {
urlPath := "https://api-sams.walmartmobile.cn/api/v1/sams/trade/cart/getUserCart"

data := make(map[string]interface{})
data["uid"] = ""
data["deviceType"] = "ios"
data["storeList"] = s.StoreList
data["deliveryType"] = s.Conf.DeliveryType
data["homePagelongitude"] = s.Address.Longitude
data["homePagelatitude"] = s.Address.Latitude
data := GetCartPram{
Uid: "",
DeviceType: "ios",
StoreList: make([]Store, 0),
DeliveryType: s.Conf.DeliveryType,
HomePagelongitude: s.Address.Longitude,
HomePagelatitude: s.Address.Latitude,
}

for _, store := range s.StoreList {
data.StoreList = append(data.StoreList, store)
}

dataStr, _ := json.Marshal(data)
req := s.NewRequest("POST", urlPath, dataStr)
Expand Down
8 changes: 5 additions & 3 deletions dd/goods.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type NormalGoods struct {
StockStatus bool `json:"stockStatus"`
IsPutOnSale bool `json:"isPutOnSale"`
IsAvailable bool `json:"isAvailable"`
LimitNum int `json:"limitNum"`
}

func (this NormalGoods) ToGoods() Goods {
Expand All @@ -57,9 +58,10 @@ func parseNormalGoods(g gjson.Result) (error, NormalGoods) {
InvalidReason: g.Get("invalidReason").Str,
Quantity: int(g.Get("quantity").Num),
StockQuantity: int(g.Get("stockQuantity").Num),
StockStatus: g.Get("stockStatus").Bool(),
IsPutOnSale: g.Get("isPutOnSale").Bool(),
IsAvailable: g.Get("isAvailable").Bool(),
StockStatus: g.Get("stockStatus").Bool(),
IsPutOnSale: g.Get("isPutOnSale").Bool(),
IsAvailable: g.Get("isAvailable").Bool(),
LimitNum: int(g.Get("purchaseLimitVO.limitNum").Int()),
}
return nil, r
}
Expand Down
2 changes: 1 addition & 1 deletion dd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type DingdongSession struct {
SettleDeliveryInfo map[int]SettleDeliveryInfo `json:"settleDeliveryInfo"`
GoodsList []Goods `json:"goods"`
FloorInfo FloorInfo `json:"floorInfo"`
StoreList []Store `json:"store"`
StoreList map[string]Store `json:"store"`
OrderInfo OrderInfo `json:"orderInfo"`
Client *http.Client `json:"client"`
Cart Cart `json:"cart"`
Expand Down
18 changes: 8 additions & 10 deletions dd/sotre.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ type Store struct {
DeliveryModeId string `json:"deliveryModeId"`
}

func (s *DingdongSession) GetStoreList(result gjson.Result) error {
func (s *DingdongSession) GetStoreList(result gjson.Result) []Store {
c := make([]Store, 0)

for _, v := range result.Get("data.storeList").Array() {
c = append(c, Store{
StoreId: v.Get("storeId").Str,
Expand All @@ -36,12 +35,11 @@ func (s *DingdongSession) GetStoreList(result gjson.Result) error {
DeliveryModeId: v.Get("storeDeliveryModeVerifyData.deliveryModeId").Str,
})
}
s.StoreList = c
return nil

return c
}

func (s *DingdongSession) CheckStore() error {
func (s *DingdongSession) CheckStore() ([]Store, error) {
urlPath := "https://api-sams.walmartmobile.cn/api/v1/sams/merchant/storeApi/getRecommendStoreListByLocation"

data := StoreListParam{
Expand All @@ -54,22 +52,22 @@ func (s *DingdongSession) CheckStore() error {

resp, err := s.Client.Do(req)
if err != nil {
return err
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
return nil, err
}
resp.Body.Close()
if resp.StatusCode == 200 {
result := gjson.Parse(string(body))
switch result.Get("code").Str {
case "Success":
return s.GetStoreList(result)
return s.GetStoreList(result), nil
default:
return errors.New(result.Get("msg").Str)
return nil, errors.New(result.Get("msg").Str)
}
} else {
return errors.New(fmt.Sprintf("[%v] %s", resp.StatusCode, body))
return nil, errors.New(fmt.Sprintf("[%v] %s", resp.StatusCode, body))
}
}
20 changes: 16 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func main() {
return c == ','
}

session := dd.DingdongSession{}
session := dd.DingdongSession{
SettleDeliveryInfo: map[int]dd.SettleDeliveryInfo{},
StoreList: map[string]dd.Store{},
}
conf := dd.Config{
AuthToken: *authToken, //HTTP头部auth-token
BarkId: *barkId, //通知用的bark id,下载bark后从app界面获取, 如果不需要可以填空字符串
Expand Down Expand Up @@ -75,14 +78,17 @@ func main() {
}
StoreLoop:
fmt.Println("########## 获取地址附近可用商店 ###########")
err = session.CheckStore()
stores, err := session.CheckStore()
if err != nil {
fmt.Printf("%s", err)
goto StoreLoop
}

for index, store := range session.StoreList {
fmt.Printf("[%v] Id:%s 名称:%s, 类型 :%s\n", index, store.StoreId, store.StoreName, store.StoreType)
for index, store := range stores {
if _, ok := session.StoreList[store.StoreId]; !ok {
session.StoreList[store.StoreId] = store
fmt.Printf("[%v] Id:%s 名称:%s, 类型 :%s\n", index, store.StoreId, store.StoreName, store.StoreType)
}
}
CartLoop:
fmt.Printf("########## 获取购物车中有效商品【%s】 ###########\n", time.Now().Format("15:04:05"))
Expand All @@ -95,6 +101,9 @@ func main() {
if goods.StockQuantity <= goods.Quantity {
goods.Quantity = goods.StockQuantity
}
if goods.LimitNum > 0 && goods.Quantity > goods.LimitNum {
goods.Quantity = goods.LimitNum
}
session.GoodsList = append(session.GoodsList, goods.ToGoods())
}
}
Expand All @@ -104,6 +113,9 @@ func main() {
if goods.StockQuantity <= goods.Quantity {
goods.Quantity = goods.StockQuantity
}
if goods.LimitNum > 0 && goods.Quantity > goods.LimitNum {
goods.Quantity = goods.LimitNum
}
session.GoodsList = append(session.GoodsList, goods.ToGoods())
}
}
Expand Down

0 comments on commit de7bd95

Please sign in to comment.