124 lines
3.6 KiB
Go
124 lines
3.6 KiB
Go
package api
|
||
|
||
import (
|
||
"database/sql/driver"
|
||
"encoding/json"
|
||
"fmt"
|
||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||
"go.uber.org/zap"
|
||
"time"
|
||
)
|
||
|
||
type (
|
||
ShopReq struct {
|
||
PageNum int `json:"pageNum"`
|
||
PageSize int `json:"pageSize"`
|
||
UserLongitude float64 `json:"userLongitude"`
|
||
UserLatitude float64 `json:"userLatitude"`
|
||
CityCode string `json:"cityCode"`
|
||
QuerySource int `json:"querySource"`
|
||
}
|
||
ShopResp struct {
|
||
Head struct {
|
||
Msg string `json:"msg"`
|
||
Code int `json:"code"`
|
||
} `json:"head"`
|
||
Data struct {
|
||
Pages int `json:"pages"`
|
||
TotalSize string `json:"totalSize"`
|
||
Items []Item `json:"items"`
|
||
} `json:"data"`
|
||
}
|
||
ShoListItem struct {
|
||
ShopId string `json:"shopId"`
|
||
ShopLogoUrl string `json:"shopLogoUrl"`
|
||
ShopName string `json:"shopName"`
|
||
ShopAddr string `json:"shopAddr"`
|
||
ShopLongitude string `json:"shopLongitude"`
|
||
ShopLatitude string `json:"shopLatitude"`
|
||
Distance int `json:"distance"`
|
||
ShopScore int `json:"shopScore"`
|
||
UserCouponCount int `json:"userCouponCount"`
|
||
WeekdayGroupPlayPrice string `json:"weekdayGroupPlayPrice"`
|
||
HolidayGroupPlayPrice string `json:"holidayGroupPlayPrice"`
|
||
ShopSource int `json:"shopSource"`
|
||
ShopHotValue int `json:"shopHotValue"`
|
||
CouponInfoList []struct {
|
||
CouponDiscountType int `json:"couponDiscountType"`
|
||
CouponDiscountRatio int `json:"couponDiscountRatio"`
|
||
CouponAmount string `json:"couponAmount"`
|
||
} `json:"couponInfoList"`
|
||
}
|
||
)
|
||
|
||
const shopListUrl = `https://api.h5.helloaba.cn/shop/v3/getScriptMatchShopList`
|
||
|
||
type ShopInfo struct {
|
||
ID int `json:"id" gorm:"column:id"`
|
||
Resp ShopResp `json:"resp" gorm:"column:resp"`
|
||
}
|
||
|
||
func (m *ShopInfo) TableName() string {
|
||
return "shop_info"
|
||
}
|
||
|
||
func (p ShopResp) Value() (driver.Value, error) {
|
||
return json.Marshal(p)
|
||
}
|
||
|
||
func (p *ShopResp) Scan(input interface{}) error {
|
||
return json.Unmarshal(input.([]byte), &p)
|
||
}
|
||
|
||
func ShopMarshal(req, body []byte) (resp interface{}, err error) {
|
||
r := new(ShopResp)
|
||
err = json.Unmarshal(body, r)
|
||
if err != nil {
|
||
ctxLogger.Error(nil, "json UnmarshalError", zap.String("resp body", string(body)))
|
||
}
|
||
if r.Head.Code != 200 || r.Head.Msg != "成功" {
|
||
ctxLogger.Error(nil, "net wrok error", zap.String("resq body", string(req)), zap.String("resp body", string(body)))
|
||
}
|
||
return r, err
|
||
}
|
||
|
||
// ShopLKvStr todo:抽象
|
||
// ShopLKvStr 按照顺序对查询的key排序
|
||
func ShopLKvStr(body ShopReq) string {
|
||
//strings := []string{"pageNum", "pageSize", "userLongitude", "userLatitude", "cityCode", "querySource"}
|
||
//sort.Strings(strings)
|
||
//cityCode pageNum pageSize querySource userLatitude userLongitude
|
||
var s = ""
|
||
s += fmt.Sprintf("%v=%v&", "cityCode", body.CityCode)
|
||
s += fmt.Sprintf("%v=%v&", "pageNum", body.PageNum)
|
||
s += fmt.Sprintf("%v=%v&", "pageSize", body.PageSize)
|
||
s += fmt.Sprintf("%v=%v&", "querySource", body.QuerySource)
|
||
return s
|
||
}
|
||
|
||
func shopList(body ShopReq) {
|
||
dto, err := Attach(shopListUrl, GetCheckSum(ShopLKvStr(body)), body, M, ShopMarshal)
|
||
if err != nil {
|
||
ctxLogger.Error(nil, "net error record")
|
||
}
|
||
if s, ok := dto.(*ShopResp); !ok {
|
||
ctxLogger.Error(nil, "assertion error")
|
||
} else {
|
||
info := ShopInfo{Resp: *s}
|
||
DB.Create(&info)
|
||
ctxLogger.Info(nil, "detailOk", zap.Int("page", s.Data.Pages))
|
||
time.Sleep(time.Duration(1) * time.Second)
|
||
}
|
||
}
|
||
func MultiShopList() {
|
||
req := ShopReq{
|
||
PageNum: 1,
|
||
PageSize: 10,
|
||
UserLongitude: 121.58372497558594,
|
||
UserLatitude: 31.347728729248047,
|
||
CityCode: "310000",
|
||
QuerySource: 1,
|
||
}
|
||
shopList(req)
|
||
}
|