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.
wikeyun/wikeyun.go

80 lines
1.6 KiB

2 years ago
package wikeyun
import (
"fmt"
"go.dtapp.net/goip"
"go.dtapp.net/golog"
"go.dtapp.net/gorequest"
"gorm.io/gorm"
)
2 years ago
type ConfigClient struct {
StoreId int // 店铺ID
AppKey int // key
AppSecret string // secret
PgsqlDb *gorm.DB // pgsql数据库
}
type Client struct {
2 years ago
client *gorequest.App // 请求客户端
2 years ago
clientIp string // Ip
2 years ago
log *golog.Api // 日志服务
logTableName string // 日志表名
logStatus bool // 日志状态
2 years ago
config *ConfigClient
2 years ago
}
2 years ago
func NewClient(config *ConfigClient) *Client {
c := &Client{config: config}
c.config = config
c.client = gorequest.NewHttp()
if c.config.PgsqlDb != nil {
c.logStatus = true
c.logTableName = "wikeyun"
c.log = golog.NewApi(&golog.ApiConfig{
Db: c.config.PgsqlDb,
TableName: c.logTableName,
2 years ago
})
}
xip := goip.GetOutsideIp()
if xip != "" && xip != "0.0.0.0" {
2 years ago
c.clientIp = xip
2 years ago
}
2 years ago
return c
2 years ago
}
// 请求接口
2 years ago
func (c *Client) request(url string, params map[string]interface{}) (resp gorequest.Response, err error) {
2 years ago
// 签名
2 years ago
sign := c.sign(params)
2 years ago
// 创建请求
2 years ago
client := c.client
2 years ago
// 设置请求地址
2 years ago
client.SetUri(fmt.Sprintf("%s?app_key=%d&timestamp=%s&client=%s&format=%s&v=%s&sign=%s", url, c.config.AppKey, sign.Timestamp, sign.Client, sign.Format, sign.V, sign.Sign))
2 years ago
// 设置FORM格式
client.SetContentTypeForm()
// 设置参数
client.SetParams(params)
// 发起请求
request, err := client.Post()
if err != nil {
return gorequest.Response{}, err
}
// 日志
2 years ago
if c.logStatus == true {
go c.postgresqlLog(request)
2 years ago
}
return request, err
}