no message

tags/v0.0.1
zhuxianglong 2 months ago
parent 9bca917f3e
commit 214967d94d

@ -1,7 +1,10 @@
module gitea.weitiangame.com/sdk/wt-game
go 1.23.4
go 1.20
require github.com/go-resty/resty/v2 v2.16.3
require (
github.com/go-resty/resty/v2 v2.16.3
go.uber.org/zap v1.27.0
)
require golang.org/x/net v0.33.0 // indirect

@ -1,6 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
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=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
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=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

@ -8,7 +8,7 @@ import (
)
// Configuration for SDK
type Config struct {
type HttpConfig struct {
AppID string
PlatformID string // Renamed for consistency
ChannelID string

@ -3,31 +3,105 @@ package sdk
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"sort"
"strings"
)
func sortMapString(params map[string]interface{}) string {
str := ""
keys := make([]string, 0)
for k, _ := range params {
// Order represents an order object used for signing parameters
type Order struct {
Uid string
BsTradeNo string
Role string
RoleId string
ServerId string
GoodsName string
OutTradeNo string
Body string
CpExtraInfo string
TradeState string
TotalFee string
PayFee string
PayTime string
}
// Config holds the configuration for SDK, such as appId and secret keys
type Config struct {
SecretKey string
}
// SDK struct encapsulates SDK configuration and utility functions
type SDK struct {
SecretKey string
}
// New initializes a new SDK with the given configuration
func New(secretKey string) *SDK {
return &SDK{
SecretKey: secretKey,
}
}
// Md5String generates MD5 hash of a given string
func (s *SDK) Md5String(str string) string {
hash := md5.Sum([]byte(str)) // Use md5.Sum for fixed size output
return hex.EncodeToString(hash[:])
}
// SignParam generates a signed URL-encoded string for the provided Order object
func (s *SDK) SignParam(order *Order) (string, error) {
// Create a map of the parameters to be signed
paramMap := map[string]string{
"uid": order.Uid,
"bs_trade_no": order.BsTradeNo,
"role": order.Role,
"role_id": order.RoleId,
"server_id": order.ServerId,
"goods_name": order.GoodsName,
"out_trade_no": order.OutTradeNo,
"body": order.Body,
"extra_info": order.CpExtraInfo,
"order_status": order.TradeState,
"total_fee": order.TotalFee,
"pay_fee": order.PayFee,
"pay_time": order.PayTime,
"sign": "",
}
// Generate the signed value
signStr := s.SortMapString(paramMap) + s.SecretKey
// Return the MD5 hash
return s.Md5String(signStr), nil
}
// SortMapString sorts the keys of a map and returns a concatenated string
func (s *SDK) SortMapString(params map[string]string) string {
var builder strings.Builder
keys := make([]string, 0, len(params))
// Avoid sorting the "sign" and "-" keys
for k := range params {
if k == "sign" || k == "-" {
continue
}
keys = append(keys, k)
}
sort.Strings(keys) // 对键进行排序
sort.Strings(keys) // Sort the keys
// Efficiently concatenate the values
for _, k := range keys {
str += params[k].(string)
builder.WriteString(params[k])
}
return str
}
func md5String(str string) string {
ctx := md5.New()
ctx.Write([]byte(str))
return hex.EncodeToString(ctx.Sum(nil))
return builder.String()
}
func Sign(str map[string]interface{}) string {
return md5String(sortMapString(str))
// ToJson converts an interface{} to JSON string
func (s *SDK) ToJson(item interface{}) (string, error) {
res, err := json.Marshal(item)
if err != nil {
return "", err // Simplified error handling
}
return string(res), nil
}

@ -2,13 +2,33 @@ package test
import (
"fmt"
"gitea.weitiangame.com/sdk/wt-game/wt-game/sdk"
"gitea.weitiangame.com/sdk/wt-game/sdk"
"testing"
)
func TestSign(t *testing.T) {
params := map[string]interface{}{
"appid": "123456",
var newSdk = sdk.New("your-secret-key")
func TestOrderSign(t *testing.T) {
// Generate signed parameters
signature, err := newSdk.SignParam(&sdk.Order{
Uid: "1",
BsTradeNo: "trade123",
Role: "user",
RoleId: "1001",
ServerId: "server01",
GoodsName: "product",
OutTradeNo: "outtrade123",
Body: "purchase",
CpExtraInfo: "extra_info",
TradeState: "1",
TotalFee: "100",
PayFee: "100",
PayTime: "2025-01-01T00:00:00",
})
if err != nil {
// Handle error
}
fmt.Println(sdk.Sign(params))
// Example output
fmt.Println(signature)
}

Loading…
Cancel
Save