dw-sdk/params.go

55 lines
1.1 KiB
Go
Raw Normal View History

package dw_sdk
import (
"crypto/md5"
"fmt"
"net/url"
"strings"
2024-09-02 14:34:11 +08:00
"github.com/bytedance/sonic"
"github.com/google/go-querystring/query"
)
type PublicParams struct {
AppKey string `json:"app_key" url:"app_key"`
2024-09-02 14:34:11 +08:00
Sign string `json:"sign" url:"sign,omitempty"`
AccessToken string `json:"access_token,omitempty" url:"access_token,omitempty"`
Timestamp int64 `json:"timestamp" url:"timestamp"`
}
func (p *PublicParams) SetSign(sign string) {
p.Sign = sign
}
type SetSigner interface {
SetSign(string)
}
func Sign(signer SetSigner, secret string) {
v, _ := query.Values(signer)
stringSignTemp := v.Encode() + secret
data := []byte(stringSignTemp)
has := md5.Sum(data)
signer.SetSign(fmt.Sprintf("%X", has))
}
// 对sign中的数组处理
type ListParams[T any] []T
func (s ListParams[T]) EncodeValues(key string, v *url.Values) error {
var list = make([]string, 0, len(s))
for _, item := range s {
js, _ := sonic.MarshalString(&item)
list = append(list, js)
}
v.Set(key, strings.Join(list, ","))
return nil
}
type PublicResponse[T any] struct {
Msg string `json:"msg"`
Code int `json:"code"`
Data T `json:"data"`
}