1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package aliNew
import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"sort"
"strings"
"time"
)
type Config struct {
Wsurl string
ApiUrl string
AppKey string
AppSecret string
AccessToken string
}
type CommonRes struct {
Success bool `json:"success"`
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
}
var server *Config
const PkgName = "aliNew"
const WebSite = "1688"
func New(config *Config) {
server = config
return
}
func (s *Config) CreateSign(signStr string) (sign string) {
//拼接参数
appSecret := []byte(s.AppSecret)
mac := hmac.New(sha1.New, appSecret)
mac.Write([]byte(signStr))
mdStr := hex.EncodeToString(mac.Sum(nil))
sign = strings.ToUpper(mdStr)
return
}
func (s *Config) sign(method string, param g.Map) g.Map {
var keys []string
mewparam := param
for k := range mewparam {
keys = append(keys, k)
}
sort.Strings(keys)
var signStr string
for _, v := range keys {
if v != "_aop_signature" {
signStr += v
signStr += gconv.String(mewparam[v])
}
}
//拼接参数
signStr = "param2/1/" + method + "/" + s.AppKey + signStr
param["_aop_signature"] = s.CreateSign(signStr)
return param
}
func (s *Config) Post(ctx context.Context, method string, params g.Map) (str string, err error) {
Start := gtime.TimestampMilli()
allparams := s.sign(method, params)
Url := s.ApiUrl + method + "/" + s.AppKey
Request := g.Client()
Request.SetHeader("Content-Type", "application/x-www-form-urlencoded")
resp, err := Request.Timeout(time.Second*5).Post(Url, allparams)
defer func() {
_ = resp.Close()
paramStr := gjson.New(params).MustToJsonString()
ctx = context.WithValue(ctx, "Method", "POST")
ctx = context.WithValue(ctx, "URI", Url)
if err != nil {
g.Log().Cat(PkgName).Ctx(ctx).Infof("参数【%v】错误【%v】响应时间【%v】", paramStr, err.Error(), gtime.TimestampMilli()-Start)
} else {
g.Log().Cat(PkgName).Ctx(ctx).Infof("参数【%v】响应【%v】响应时间【%v】", paramStr,str, gtime.TimestampMilli()-Start)
}
}()
str = resp.ReadAllString()
return
}