初始化
commit
9bca917f3e
@ -0,0 +1,2 @@
|
|||||||
|
vendor/
|
||||||
|
.idea/
|
@ -0,0 +1,7 @@
|
|||||||
|
module gitea.weitiangame.com/sdk/wt-game
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
|
|
||||||
|
require github.com/go-resty/resty/v2 v2.16.3
|
||||||
|
|
||||||
|
require golang.org/x/net v0.33.0 // indirect
|
@ -0,0 +1,6 @@
|
|||||||
|
github.com/go-resty/resty/v2 v2.16.3 h1:zacNT7lt4b8M/io2Ahj6yPypL7bqx9n1iprfQuodV+E=
|
||||||
|
github.com/go-resty/resty/v2 v2.16.3/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
|
||||||
|
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||||
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
|
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
|
||||||
|
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
@ -0,0 +1,70 @@
|
|||||||
|
package sdk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Configuration for SDK
|
||||||
|
type Config struct {
|
||||||
|
AppID string
|
||||||
|
PlatformID string // Renamed for consistency
|
||||||
|
ChannelID string
|
||||||
|
AppSecret string
|
||||||
|
BaseURL string // Either Test or Production
|
||||||
|
}
|
||||||
|
|
||||||
|
// RequestParams for common parameters
|
||||||
|
type RequestParams map[string]interface{} // Renamed to be more descriptive
|
||||||
|
|
||||||
|
// Response for API responses
|
||||||
|
type Response struct {
|
||||||
|
ErrorCode int `json:"code"` // Renamed to ErrorCode for clarity
|
||||||
|
ErrorMessage string `json:"msg"` // Renamed to ErrorMessage for clarity
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
clientInstance *Client
|
||||||
|
once sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewClient initializes the SDK client with reusable resty client (Singleton)
|
||||||
|
func NewClient(config Config) *Client {
|
||||||
|
once.Do(func() {
|
||||||
|
clientInstance = &Client{
|
||||||
|
config: config,
|
||||||
|
client: resty.New().SetRetryCount(3).SetRetryWaitTime(1 * time.Second),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return clientInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client struct to manage requests
|
||||||
|
type Client struct {
|
||||||
|
config Config
|
||||||
|
client *resty.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTP POST helper function with retry logic
|
||||||
|
func (c *Client) post(url string, params interface{}) (*Response, error) {
|
||||||
|
// Retry up to 3 times with a 1-second delay between retries
|
||||||
|
var resp *Response
|
||||||
|
response, err := c.client.R().
|
||||||
|
SetBody(params).
|
||||||
|
SetResult(&resp). // Set result to unmarshal JSON into Response struct
|
||||||
|
Post(url)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("request failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the response status code
|
||||||
|
if response.StatusCode() != 200 {
|
||||||
|
return nil, fmt.Errorf("non-200 response received: %v", response.StatusCode())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the response body directly after it has been unmarshalled
|
||||||
|
return resp, nil
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package sdk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func sortMapString(params map[string]interface{}) string {
|
||||||
|
str := ""
|
||||||
|
keys := make([]string, 0)
|
||||||
|
for k, _ := range params {
|
||||||
|
if k == "sign" || k == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys) // 对键进行排序
|
||||||
|
for _, k := range keys {
|
||||||
|
str += params[k].(string)
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func md5String(str string) string {
|
||||||
|
ctx := md5.New()
|
||||||
|
ctx.Write([]byte(str))
|
||||||
|
return hex.EncodeToString(ctx.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Sign(str map[string]interface{}) string {
|
||||||
|
return md5String(sortMapString(str))
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gitea.weitiangame.com/sdk/wt-game/wt-game/sdk"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSign(t *testing.T) {
|
||||||
|
params := map[string]interface{}{
|
||||||
|
"appid": "123456",
|
||||||
|
}
|
||||||
|
fmt.Println(sdk.Sign(params))
|
||||||
|
}
|
Loading…
Reference in New Issue