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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package schl
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/grand"
"net/url"
"sort"
"strings"
"time"
)
const Host = "https://schl-api.szbaoly.com"
type Config struct {
ApiUrl string
AppKey string
AppSecret string
ImgUrl string
}
type CommonRes struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
type PubilcParams struct {
AppID string `json:"appid"`
Nonce string `json:"nonce"`
Timestamp string `json:"timestamp"`
Signature string `json:"signature"`
}
const pkgName = "schl"
func New(config *Config) *Config {
return config
}
func (s *Config) Post(ctx context.Context, URL string, bodyMap g.Map) (result string, err error) {
Start := gtime.TimestampMilli()
pubilcParams := new(PubilcParams)
pubilcParams.AppID = s.AppKey
pubilcParams.Timestamp = gtime.TimestampStr()
pubilcParams.Nonce = grand.S(16, false)
s.Sign(pubilcParams)
defer func() {
ctx = context.WithValue(ctx, "URI", URL)
if err != nil {
g.Log().Ctx(ctx).Cat(pkgName).Cat("error").
Infof("参数【%v】错误【%v】响应时间:【%v ms】", gjson.New(bodyMap).MustToJsonString(), err.Error(), gtime.TimestampMilli()-Start)
} else {
g.Log().Ctx(ctx).Cat(pkgName).
Infof("参数【%v】响应【%v】响应时间:【%v ms】", gjson.New(bodyMap).MustToJsonString(), result, gtime.TimestampMilli()-Start)
}
}()
var value = url.Values{}
value.Add("appid", pubilcParams.AppID)
value.Add("nonce", pubilcParams.Nonce)
value.Add("timestamp", pubilcParams.Timestamp)
value.Add("signature", pubilcParams.Signature)
response, err := g.Client().
Timeout(time.Second*3).
ContentJson().
Post(Host+URL+"?"+value.Encode(), bodyMap)
defer func() {
_ = response.Close()
}()
if nil != err {
return
}
result, _ = gregex.ReplaceString(`\s`, "", response.ReadAllString())
return
}
func (s *Config) Sign(pubilcParams *PubilcParams) {
allMaps := make(map[string]string)
var arr = garray.New().Append("appid", "timestamp", "nonce")
for k, v := range gconv.MapStrStr(pubilcParams) {
if arr.Contains(k) {
allMaps[k] = v
}
}
keys := make([]string, 0)
for k := range allMaps {
keys = append(keys, k)
}
sort.Strings(keys)
paramsString := ""
for k, v := range keys {
if k > 0 {
paramsString += "&"
}
paramsString += v + "=" + allMaps[v]
}
paramsString += "&key=" + s.AppSecret
hmacSha256String := hmacSha256(paramsString, s.AppSecret)
pubilcParams.Signature = strings.ToUpper(hmacSha256String)
return
}
func hmacSha256(data string, secret string) string {
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}