54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
|
package dw_sdk
|
||
|
|
||
|
import (
|
||
|
"crypto/md5"
|
||
|
"fmt"
|
||
|
"github.com/bytedance/sonic"
|
||
|
"github.com/google/go-querystring/query"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type PublicParams struct {
|
||
|
AppKey string `json:"app_key" url:"app_key"`
|
||
|
Sign string `json:"sign" url:"-"`
|
||
|
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"`
|
||
|
}
|