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

126 lines
3.3 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
// Client 实例
2 years ago
type Client struct {
2 years ago
requestClient *gorequest.App // 请求服务
config struct {
clientId string // POP分配给应用的client_id
clientSecret string // POP分配给应用的client_secret
mediaId string // 媒体ID
pid string // 推广位
}
log struct {
gormClient *dorm.GormClient // 日志数据库
gorm bool // 日志开关
logGormClient *golog.ApiClient // 日志服务
mongoClient *dorm.MongoClient // 日志数据库
mongo bool // 日志开关
logMongoClient *golog.ApiClient // 日志服务
}
2 years ago
}
2 years ago
// client *dorm.GormClient
type gormClientFun func() *dorm.GormClient
// client *dorm.MongoClient
// databaseName string
type mongoClientFun func() (*dorm.MongoClient, string)
// NewClient 创建实例化
// clientId POP分配给应用的client_id
// clientSecret POP分配给应用的client_secret
// mediaId 媒体ID
// pid 推广位
func NewClient(clientId, clientSecret, mediaId, pid string, gormClientFun gormClientFun, mongoClientFun mongoClientFun, debug bool) (*Client, error) {
2 years ago
2 years ago
var err error
2 years ago
c := &Client{}
c.config.clientId = clientId
c.config.clientSecret = clientSecret
c.config.mediaId = mediaId
c.config.pid = pid
2 years ago
2 years ago
c.requestClient = gorequest.NewHttp()
c.requestClient.Uri = apiUrl
2 years ago
2 years ago
gormClient := gormClientFun()
if gormClient.Db != nil {
c.log.logGormClient, err = golog.NewApiGormClient(func() (*dorm.GormClient, string) {
return gormClient, logTable
}, debug)
2 years ago
if err != nil {
return nil, err
}
2 years ago
c.log.gorm = true
2 years ago
}
2 years ago
c.log.gormClient = gormClient
2 years ago
2 years ago
mongoClient, databaseName := mongoClientFun()
if mongoClient.Db != nil {
c.log.logMongoClient, err = golog.NewApiMongoClient(func() (*dorm.MongoClient, string, string) {
return mongoClient, databaseName, logTable
}, debug)
if err != nil {
return nil, err
}
c.log.mongo = true
}
c.log.mongoClient = mongoClient
2 years ago
2 years ago
return c, nil
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
}