parent
b9ced960c0
commit
03adb53e41
@ -0,0 +1 @@
|
|||||||
|
package wnfuwu
|
@ -0,0 +1,38 @@
|
|||||||
|
package wnfuwu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/dtapps/go-library/utils/golog"
|
||||||
|
"github.com/dtapps/go-library/utils/gorequest"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ClientConfig 实例配置
|
||||||
|
type ClientConfig struct {
|
||||||
|
UserId int64 // 商户ID
|
||||||
|
ApiKey string // 秘钥
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client 实例
|
||||||
|
type Client struct {
|
||||||
|
requestClient *gorequest.App // 请求服务
|
||||||
|
config struct {
|
||||||
|
userId int64 // 商户ID
|
||||||
|
apiKey string // 秘钥
|
||||||
|
}
|
||||||
|
log struct {
|
||||||
|
status bool // 状态
|
||||||
|
client *golog.ApiClient // 日志服务
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient 创建实例化
|
||||||
|
func NewClient(config *ClientConfig) (*Client, error) {
|
||||||
|
|
||||||
|
c := &Client{}
|
||||||
|
|
||||||
|
c.config.userId = config.UserId
|
||||||
|
c.config.apiKey = config.ApiKey
|
||||||
|
|
||||||
|
c.requestClient = gorequest.NewHttp()
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package wnfuwu
|
||||||
|
|
||||||
|
import "github.com/dtapps/go-library/utils/golog"
|
||||||
|
|
||||||
|
// ConfigApp 配置
|
||||||
|
func (c *Client) ConfigApp(userId int64, apiKey string) *Client {
|
||||||
|
c.config.userId = userId
|
||||||
|
c.config.apiKey = apiKey
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigApiClientFun 日志配置
|
||||||
|
func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) {
|
||||||
|
apiClient := apiClientFun()
|
||||||
|
if apiClient != nil {
|
||||||
|
c.log.client = apiClient
|
||||||
|
c.log.status = true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package wnfuwu
|
||||||
|
|
||||||
|
const (
|
||||||
|
apiUrl = "http://wn.wnfuwu.cn/yrapi.php"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LogTable = "wnfuwu"
|
||||||
|
)
|
@ -0,0 +1,15 @@
|
|||||||
|
package wnfuwu
|
||||||
|
|
||||||
|
import "github.com/dtapps/go-library/utils/golog"
|
||||||
|
|
||||||
|
func (c *Client) GetUserId() int64 {
|
||||||
|
return c.config.userId
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetApiKey() string {
|
||||||
|
return c.config.apiKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetLog() *golog.ApiClient {
|
||||||
|
return c.log.client
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package wnfuwu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/dtapps/go-library"
|
||||||
|
"github.com/dtapps/go-library/utils/gorequest"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 请求接口
|
||||||
|
func (c *Client) request(ctx context.Context, url string, params map[string]interface{}) (gorequest.Response, error) {
|
||||||
|
|
||||||
|
// 签名
|
||||||
|
params["sign"] = c.sign(params)
|
||||||
|
|
||||||
|
// 创建请求
|
||||||
|
client := c.requestClient
|
||||||
|
|
||||||
|
// 设置请求地址
|
||||||
|
client.SetUri(url)
|
||||||
|
|
||||||
|
// 设置FORM格式
|
||||||
|
client.SetContentTypeForm()
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
client.SetParams(params)
|
||||||
|
|
||||||
|
// 传入SDk版本
|
||||||
|
client.AfferentSdkUserVersion(go_library.Version())
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
request, err := client.Post(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return gorequest.Response{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 日志
|
||||||
|
if c.log.status {
|
||||||
|
go c.log.client.Middleware(ctx, request, go_library.Version())
|
||||||
|
}
|
||||||
|
|
||||||
|
return request, err
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package wnfuwu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/dtapps/go-library/utils/gomd5"
|
||||||
|
"github.com/dtapps/go-library/utils/gostring"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 签名
|
||||||
|
func (c *Client) sign(params map[string]interface{}) string {
|
||||||
|
var keys []string
|
||||||
|
for k := range params {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
signStr := ""
|
||||||
|
for _, key := range keys {
|
||||||
|
signStr += fmt.Sprintf("%s=%s&", key, gostring.ToString(params[key]))
|
||||||
|
}
|
||||||
|
signStr += fmt.Sprintf("apikey=%s", c.GetApiKey())
|
||||||
|
return gomd5.ToUpper(signStr)
|
||||||
|
}
|
Loading…
Reference in new issue