You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pinduoduo/client.go

100 lines
2.5 KiB

2 years ago
package pinduoduo
import (
"fmt"
2 years ago
"go.dtapp.net/dorm"
2 years ago
"go.dtapp.net/golog"
"go.dtapp.net/gorequest"
"go.dtapp.net/gostring"
"regexp"
"strconv"
"strings"
)
2 years ago
type ConfigClient struct {
2 years ago
ClientId string // POP分配给应用的client_id
ClientSecret string // POP分配给应用的client_secret
MediaId string // 媒体ID
Pid string // 推广位
GormClient *dorm.GormClient // 日志数据库
2 years ago
LogClient *golog.ZapLog // 日志驱动
2 years ago
LogDebug bool // 日志开关
2 years ago
}
type Client struct {
2 years ago
requestClient *gorequest.App // 请求服务
logClient *golog.ApiClient // 日志服务
config *ConfigClient // 配置
2 years ago
}
2 years ago
func NewClient(config *ConfigClient) (*Client, error) {
2 years ago
2 years ago
var err error
2 years ago
c := &Client{config: config}
2 years ago
c.requestClient = gorequest.NewHttp()
c.requestClient.Uri = apiUrl
2 years ago
2 years ago
if c.config.GormClient.Db != nil {
c.logClient, err = golog.NewApiClient(&golog.ApiClientConfig{
GormClient: c.config.GormClient,
TableName: logTable,
LogClient: c.config.LogClient,
LogDebug: c.config.LogDebug,
})
2 years ago
if err != nil {
return nil, err
}
2 years ago
}
2 years ago
2 years ago
return c, nil
}
func (c *Client) ConfigPid(pid string) *Client {
n := c
n.config.Pid = pid
return n
2 years ago
}
type ErrResp struct {
ErrorResponse struct {
ErrorMsg string `json:"error_msg"`
SubMsg string `json:"sub_msg"`
SubCode interface{} `json:"sub_code"`
ErrorCode int `json:"error_code"`
RequestId string `json:"request_id"`
} `json:"error_response"`
}
type CustomParametersResult struct {
Sid string `json:"sid"`
Uid string `json:"uid"`
}
2 years ago
func (c *Client) SalesTipParseInt64(salesTip string) int64 {
2 years ago
parseInt, err := strconv.ParseInt(salesTip, 10, 64)
if err != nil {
salesTipStr := salesTip
if strings.Contains(salesTip, "万+") {
salesTipStr = strings.Replace(salesTip, "万+", "0000", -1)
} else if strings.Contains(salesTip, "万") {
salesTipStr = strings.Replace(salesTip, "万", "000", -1)
}
re := regexp.MustCompile("[0-9]+")
SalesTipMap := re.FindAllString(salesTipStr, -1)
if len(SalesTipMap) == 2 {
return gostring.ToInt64(fmt.Sprintf("%s%s", SalesTipMap[0], SalesTipMap[1]))
} else if len(SalesTipMap) == 1 {
return gostring.ToInt64(SalesTipMap[0])
} else {
return 0
}
} else {
return parseInt
}
}
2 years ago
func (c *Client) CommissionIntegralToInt64(GoodsPrice, CouponProportion int64) int64 {
2 years ago
return (GoodsPrice * CouponProportion) / 1000
}