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

云众

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