From 270e63f57e09f7769870c8330c9582082bdc5048 Mon Sep 17 00:00:00 2001
From: zhanglibo <zhanglibo@stbz.net>
Date: Wed, 19 Jun 2024 15:41:05 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BA=91=E4=BC=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 upstream/yunzmall/yunzmall.go       | 35 ++++++++++++++++++-----------
 upstream/yunzmall/yunzmall_token.go | 22 ++++++++----------
 2 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/upstream/yunzmall/yunzmall.go b/upstream/yunzmall/yunzmall.go
index d134af5..9292024 100644
--- a/upstream/yunzmall/yunzmall.go
+++ b/upstream/yunzmall/yunzmall.go
@@ -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()
diff --git a/upstream/yunzmall/yunzmall_token.go b/upstream/yunzmall/yunzmall_token.go
index 1486374..831de17 100644
--- a/upstream/yunzmall/yunzmall_token.go
+++ b/upstream/yunzmall/yunzmall_token.go
@@ -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
-- 
2.18.1