提交 b2eb2041 authored 作者: husiqi's avatar husiqi

修改了一下存储位置

上级 522c9791
......@@ -9,6 +9,7 @@ require (
github.com/alibabacloud-go/tea v1.2.2
github.com/aliyun/aliyun-log-go-sdk v0.1.27
github.com/gogf/gf v1.16.6
github.com/json-iterator/go v1.1.12
github.com/kjk/betterguid v0.0.0-20170621091430-c442874ba63a
github.com/nsqio/go-nsq v1.1.0
github.com/olivere/elastic/v7 v7.0.30
......
package stbz
// Config Config
type Config struct {
AccessKey string
SecretKey string
Host string
}
// NewConfig NewConfig
func NewConfig(host, accessKey, secretKey string) *Config {
config := new(Config)
config.AccessKey = accessKey
config.SecretKey = secretKey
config.Host = host
return config
}
package stbz
// var (
//
// Method.
//
// )
type method uint
// UtilMethod UtilMethod
type UtilMethod struct {
GET method
POST method
}
// Method Method
var Method = new(UtilMethod)
func init() {
Method.GET = 1
Method.POST = 2
}
package stbz
import (
"encoding/json"
"net/url"
"strconv"
"time"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
// APIResult APIResult
type APIResult struct {
ID string `json:"id"`
Code int `json:"code"`
Msg string `json:"message"`
Data interface{} `json:"data"`
}
var currentConfig *Config
// SetConfig SetConfig
func SetConfig(config *Config) {
currentConfig = config
}
// API API
func API(method method, api string, queryMap map[string]string, bodyMap map[string]interface{}) (result *APIResult, err error) {
header := new(signV2)
header.AppKey = currentConfig.AccessKey
header.TimeStamp = uint64(time.Now().UnixNano() / 1e6)
header.Nonce = strconv.Itoa(int(time.Now().UnixNano()))
sign := Sign(*header, queryMap, bodyMap, currentConfig.SecretKey) // 宜供优品
header.Sign = sign
var response *ghttp.ClientResponse
switch method {
case Method.POST:
response, err = g.Client().SetHeaderMap(header.toMap()).ContentJson().Post(currentConfig.Host+api, bodyMap)
if nil != err {
return
}
case Method.GET:
queryString := "?"
i := 0
for k, v := range queryMap {
if 0 != i {
queryString += "&"
}
queryString += k + "=" + url.QueryEscape(v)
i++
}
response, err = g.Client().ContentJson().SetHeaderMap(header.toMap()).Get(currentConfig.Host+api+queryString, bodyMap)
if nil != err {
return
}
}
result = new(APIResult)
err = json.Unmarshal(response.ReadAll(), result)
response.Close()
return
}
package stbz
package syh
import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"errors"
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"net/http"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
)
// APIResult APIResult
type APIResult struct {
ID string `json:"id"`
Code int `json:"code"`
Msg string `json:"message"`
Data interface{} `json:"data"`
}
// Config Config
type Client struct {
AccessKey string
SecretKey string
Host string
}
// NewConfig NewConfig
func NewConfig(host, accessKey, secretKey string) *Client {
config := new(Client)
config.AccessKey = accessKey
config.SecretKey = secretKey
config.Host = host
return config
}
// API API
func (s *Client) API(method int, api string, queryMap map[string]string, bodyMap map[string]interface{}) (result *APIResult, err error) {
header := signV2{}
header.AppKey = s.AccessKey
header.TimeStamp = uint64(time.Now().UnixNano() / 1e6)
header.Nonce = strconv.Itoa(int(time.Now().UnixNano()))
sign := s.Sign(header, queryMap, bodyMap, s.SecretKey) // 宜供优品
header.Sign = sign
var response *ghttp.ClientResponse
switch method {
case 2:
response, err = g.Client().SetHeaderMap(s.toMap(header)).ContentJson().Post(s.Host+api, bodyMap)
if nil != err {
return
}
case 1:
queryString := "?"
i := 0
for k, v := range queryMap {
if 0 != i {
queryString += "&"
}
queryString += k + "=" + url.QueryEscape(v)
i++
}
response, err = g.Client().ContentJson().SetHeaderMap(s.toMap(header)).Get(s.Host+api+queryString, bodyMap)
if nil != err {
return
}
}
result = new(APIResult)
err = json.Unmarshal(response.ReadAll(), result)
response.Close()
return
}
// Sign 签名
func Sign(params signV2, queryMaps map[string]string, bodyMaps map[string]interface{}, secretKey string) string {
func (s *Client) Sign(params signV2, queryMaps map[string]string, bodyMaps map[string]interface{}, secretKey string) string {
bodyBytes, err := json.Marshal(bodyMaps)
if nil != err {
return ""
......@@ -26,7 +99,7 @@ func Sign(params signV2, queryMaps map[string]string, bodyMaps map[string]interf
// body = ""
// }
return createSignV2(params, queryMaps, body, secretKey)
return s.createSignV2(params, queryMaps, body, secretKey)
}
type signV2 struct {
......@@ -36,10 +109,10 @@ type signV2 struct {
Sign string `header:"Api-Sign"`
}
func (s signV2) toMap() (maps map[string]string) {
func (s *Client) toMap(req signV2) (maps map[string]string) {
maps = make(map[string]string)
rtype := reflect.TypeOf(s)
rvalue := reflect.ValueOf(&s)
rtype := reflect.TypeOf(req)
rvalue := reflect.ValueOf(&req)
for i := 0; i < rtype.NumField(); i++ {
field := rtype.Field(i)
// field.Type.Kind()
......@@ -55,10 +128,10 @@ func (s signV2) toMap() (maps map[string]string) {
return
}
func (s signV2) toParamsMap() (maps map[string]string) {
func (s *Client) toParamsMap(req signV2) (maps map[string]string) {
maps = make(map[string]string)
rtype := reflect.TypeOf(s)
rvalue := reflect.ValueOf(&s)
rtype := reflect.TypeOf(req)
rvalue := reflect.ValueOf(&req)
for i := 0; i < rtype.NumField(); i++ {
field := rtype.Field(i)
// field.Type.Kind()
......@@ -76,7 +149,7 @@ func (s signV2) toParamsMap() (maps map[string]string) {
return
}
func getSignv2Params(header http.Header) (params signV2, err error) {
func (s *Client) getSignv2Params(header http.Header) (params signV2, err error) {
rtype := reflect.TypeOf(params)
rvalue := reflect.ValueOf(&params)
......@@ -137,14 +210,14 @@ func getSignv2Params(header http.Header) (params signV2, err error) {
}
// 生成签名
func createSignV2(params signV2, queryMaps map[string]string, body string, secretKey string) (sign string) {
func (s *Client) createSignV2(params signV2, queryMaps map[string]string, body string, secretKey string) (sign string) {
allMaps := make(map[string]string)
for k, v := range queryMaps {
allMaps[k] = v
}
for k, v := range params.toParamsMap() {
for k, v := range s.toParamsMap(params) {
allMaps[k] = v
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论