commit 9bca917f3ec47f7bb38433f79154e13720622759 Author: zhuxianglong <56494565@qq.com> Date: Wed Jan 15 18:03:33 2025 +0800 初始化 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ef0e14 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/ +.idea/ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..17de46e --- /dev/null +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2b52407 --- /dev/null +++ b/go.sum @@ -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= diff --git a/sdk/sdk.go b/sdk/sdk.go new file mode 100644 index 0000000..dcd4667 --- /dev/null +++ b/sdk/sdk.go @@ -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 +} diff --git a/sdk/sign.go b/sdk/sign.go new file mode 100644 index 0000000..bc1a38b --- /dev/null +++ b/sdk/sign.go @@ -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)) +} diff --git a/test/sdk_test.go b/test/sdk_test.go new file mode 100644 index 0000000..1f2cc71 --- /dev/null +++ b/test/sdk_test.go @@ -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)) +}