提交 270e63f5 authored 作者: 张立波's avatar 张立波

云众

上级 ef6265c0
...@@ -21,25 +21,35 @@ import ( ...@@ -21,25 +21,35 @@ import (
type Client struct { type Client struct {
AppKey string AppKey string
AppSecret string AppSecret string
AccessToken string accessToken string
Url string
DB int DB int
Source int
Goods goodsLogic
Logistics logisticsLogic
Order orderLogic
Refund refundLogic
Storage storageLogic
} }
var server *Client var server *Client
const Url = "https://supply.yunzmall.com/supplyapi" const Url = "https://supply.yunzmall.com/supplyapi"
const pkgName = "yunzmall" const pkgName = "yunzmall"
const CacheKey = "yunzmall:token" const CacheKey = "yunzmall:token:"
func New(req *Client) { func New(req *Client) *Client {
server = req if req.DB == 0 {
if server.DB == 0 { req.DB = 10
server.DB = 10
} }
if req.Url == "" {
req.Url = Url
}
return req
} }
// post 请求 // post 请求
func post(ctx context.Context, method string, params g.Map, xToken ...interface{}) (str string, err error) { func (s *Client) post(ctx context.Context, method string, params g.Map, xToken ...interface{}) (str string, err error) {
Start := gtime.TimestampMilli() Start := gtime.TimestampMilli()
Request := g.Client().ContentJson() Request := g.Client().ContentJson()
var AppNonce = grand.S(16) var AppNonce = grand.S(16)
...@@ -48,7 +58,7 @@ func post(ctx context.Context, method string, params g.Map, xToken ...interface{ ...@@ -48,7 +58,7 @@ func post(ctx context.Context, method string, params g.Map, xToken ...interface{
Request.SetHeader("App-Timestamp", gtime.TimestampStr()) Request.SetHeader("App-Timestamp", gtime.TimestampStr())
if len(xToken) == 0 { if len(xToken) == 0 {
var token string var token string
token, err = Token.Access(ctx) token, err = s.AccessToken(ctx)
if err != nil { if err != nil {
return return
} }
...@@ -61,7 +71,7 @@ func post(ctx context.Context, method string, params g.Map, xToken ...interface{ ...@@ -61,7 +71,7 @@ func post(ctx context.Context, method string, params g.Map, xToken ...interface{
Request.SetHeader("App-Sign", AppSign) Request.SetHeader("App-Sign", AppSign)
resp, err := Request. resp, err := Request.
Timeout(time.Second*5). Timeout(time.Second*5).
Post(Url+method, params) Post(s.Url+method, params)
defer func() { defer func() {
_ = resp.Close() _ = resp.Close()
paramStr := gjson.New(params).MustToJsonString() paramStr := gjson.New(params).MustToJsonString()
...@@ -80,7 +90,7 @@ func post(ctx context.Context, method string, params g.Map, xToken ...interface{ ...@@ -80,7 +90,7 @@ func post(ctx context.Context, method string, params g.Map, xToken ...interface{
return return
} }
func get(ctx context.Context, method string, params g.Map, xToken ...interface{}) (str string, err error) { func (s *Client) get(ctx context.Context, method string, params g.Map, xToken ...interface{}) (str string, err error) {
Start := gtime.TimestampMilli() Start := gtime.TimestampMilli()
Request := g.Client().ContentJson() Request := g.Client().ContentJson()
var AppNonce = grand.S(16) var AppNonce = grand.S(16)
...@@ -89,7 +99,7 @@ func get(ctx context.Context, method string, params g.Map, xToken ...interface{} ...@@ -89,7 +99,7 @@ func get(ctx context.Context, method string, params g.Map, xToken ...interface{}
Request.SetHeader("App-Timestamp", gtime.TimestampStr()) Request.SetHeader("App-Timestamp", gtime.TimestampStr())
if len(xToken) == 0 { if len(xToken) == 0 {
var token string var token string
token, err = Token.Access(ctx) token, err = s.AccessToken(ctx)
if err != nil { if err != nil {
return return
} }
...@@ -103,14 +113,13 @@ func get(ctx context.Context, method string, params g.Map, xToken ...interface{} ...@@ -103,14 +113,13 @@ func get(ctx context.Context, method string, params g.Map, xToken ...interface{}
for k, v := range params { for k, v := range params {
postValues.Add(k, gconv.String(v)) postValues.Add(k, gconv.String(v))
} }
URL, _ := url.Parse(Url + method) URL, _ := url.Parse(s.Url + method)
URL.RawQuery = postValues.Encode() URL.RawQuery = postValues.Encode()
urlPath := URL.String() urlPath := URL.String()
Request.SetHeader("App-Sign", AppSign) Request.SetHeader("App-Sign", AppSign)
resp, err := Request. resp, err := Request.
Timeout(time.Second * 5). Timeout(time.Second * 5).
Get(urlPath) Get(urlPath)
resp.RawDump()
defer func() { defer func() {
_ = resp.Close() _ = resp.Close()
paramStr := gjson.New(params).MustToJsonString() paramStr := gjson.New(params).MustToJsonString()
......
...@@ -6,13 +6,9 @@ import ( ...@@ -6,13 +6,9 @@ import (
"github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime" "github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
) )
type tokenLogic struct {
}
var Token = tokenLogic{}
type TokenGetRes struct { type TokenGetRes struct {
Code int `json:"code"` Code int `json:"code"`
Data struct { Data struct {
...@@ -22,9 +18,9 @@ type TokenGetRes struct { ...@@ -22,9 +18,9 @@ type TokenGetRes struct {
Msg string `json:"msg"` Msg string `json:"msg"`
} }
func (s tokenLogic) Get(ctx context.Context) (res *TokenGetRes, err error) { func (s *Client) GetToken(ctx context.Context) (res *TokenGetRes, err error) {
var method = "/app/application/getToken" var method = "/app/application/getToken"
result, err := post(ctx, method, g.Map{ result, err := s.post(ctx, method, g.Map{
"app_key": server.AppKey, "app_key": server.AppKey,
"app_secret": server.AppSecret, "app_secret": server.AppSecret,
}, true) }, true)
...@@ -38,14 +34,14 @@ func (s tokenLogic) Get(ctx context.Context) (res *TokenGetRes, err error) { ...@@ -38,14 +34,14 @@ func (s tokenLogic) Get(ctx context.Context) (res *TokenGetRes, err error) {
return return
} }
func (s tokenLogic) Refresh(ctx context.Context) (res string, err error) { func (s *Client) RefreshToken(ctx context.Context) (res string, err error) {
var conn = g.Redis().Conn() var conn = g.Redis().Conn()
defer func() { defer func() {
_ = conn.Close() _ = conn.Close()
}() }()
_, _ = conn.DoVar("SELECT", server.DB) _, _ = conn.DoVar("SELECT", server.DB)
var result *TokenGetRes var result *TokenGetRes
result, err = s.Get(ctx) result, err = s.GetToken(ctx)
if err != nil { if err != nil {
err = gerror.New("获取token失败") err = gerror.New("获取token失败")
return return
...@@ -55,7 +51,7 @@ func (s tokenLogic) Refresh(ctx context.Context) (res string, err error) { ...@@ -55,7 +51,7 @@ func (s tokenLogic) Refresh(ctx context.Context) (res string, err error) {
return return
} }
res = result.Data.Token res = result.Data.Token
_, _ = conn.Do("HMSET", CacheKey, "Token", result.Data.Token, "ExpiresAt", result.Data.ExpiresAt) _, _ = conn.Do("HMSET", CacheKey+gconv.String(s.Source), "Token", result.Data.Token, "ExpiresAt", result.Data.ExpiresAt)
return return
} }
...@@ -64,7 +60,7 @@ type TokenCacheRes struct { ...@@ -64,7 +60,7 @@ type TokenCacheRes struct {
ExpiresAt int64 ExpiresAt int64
} }
func (s tokenLogic) Access(ctx context.Context) (res string, err error) { func (s *Client) AccessToken(ctx context.Context) (res string, err error) {
var conn = g.Redis().Conn() var conn = g.Redis().Conn()
defer func() { defer func() {
_ = conn.Close() _ = conn.Close()
...@@ -73,12 +69,12 @@ func (s tokenLogic) Access(ctx context.Context) (res string, err error) { ...@@ -73,12 +69,12 @@ func (s tokenLogic) Access(ctx context.Context) (res string, err error) {
cache, _ := conn.DoVar("HGETALL", CacheKey) cache, _ := conn.DoVar("HGETALL", CacheKey)
g.Log().Line(true).Info(cache.String()) g.Log().Line(true).Info(cache.String())
if cache.IsEmpty() { if cache.IsEmpty() {
return s.Refresh(ctx) return s.RefreshToken(ctx)
} }
var cacheRes *TokenCacheRes var cacheRes *TokenCacheRes
_ = gjson.New(cache).Scan(&cacheRes) _ = gjson.New(cache).Scan(&cacheRes)
if cacheRes.ExpiresAt < gtime.TimestampMilli() { if cacheRes.ExpiresAt < gtime.TimestampMilli() {
return s.Refresh(ctx) return s.RefreshToken(ctx)
} }
res = cacheRes.Token res = cacheRes.Token
return return
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论