From e50dac6b1cdb9b77a8701ad03f56e331808aba8e Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 20 Jun 2023 19:36:28 +0800 Subject: [PATCH] =?UTF-8?q?Order=E8=AE=A2=E5=8D=95=E5=BE=85=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 5 +++ goctl_template/api/context.tpl | 4 +- model/gmodel/fs_address_logic.go | 3 +- model/gmodel/fs_pay_logic.go | 6 +-- model/gmodel/fs_refund_reason_logic.go | 19 +++++++++- server/canteen/internal/svc/servicecontext.go | 7 ++-- .../internal/svc/servicecontext.go | 7 ++-- server/home-user-auth/etc/home-user-auth.yaml | 3 ++ .../home-user-auth/internal/config/config.go | 4 ++ .../internal/logic/useroderdeletelogic.go | 38 +++++++++++++------ .../internal/svc/servicecontext.go | 4 +- .../internal/svc/servicecontext.go | 7 ++-- server/orders/internal/svc/servicecontext.go | 7 ++-- .../internal/svc/servicecontext.go | 8 ++-- server/product/internal/svc/servicecontext.go | 4 +- .../internal/svc/servicecontext.go | 4 +- utils/basic/basic.go | 28 +++++++++++++- 18 files changed, 116 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index 2181fceb..5f4837dc 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( require ( github.com/google/uuid v1.3.0 // indirect github.com/schollz/progressbar v1.0.0 // indirect + github.com/stripe/stripe-go/v74 v74.22.0 // indirect ) require ( diff --git a/go.sum b/go.sum index 6eb0d0b8..c2890a2b 100644 --- a/go.sum +++ b/go.sum @@ -229,6 +229,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stripe/stripe-go/v74 v74.22.0 h1:A6yqbyS61JYWhq6H4b0PFZukM8Ltx1VOXxsLYBNL+wE= +github.com/stripe/stripe-go/v74 v74.22.0/go.mod h1:f9L6LvaXa35ja7eyvP6GQswoaIPaBRvGAimAO+udbBw= github.com/tidwall/gjson v1.12.0 h1:61wEp/qfvFnqKH/WCI3M8HuRut+mHT6Mr82QrFmM2SY= github.com/tidwall/gjson v1.12.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= @@ -333,6 +335,7 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -376,6 +379,7 @@ golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -388,6 +392,7 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/goctl_template/api/context.tpl b/goctl_template/api/context.tpl index 1bb0c6c5..d78190d8 100644 --- a/goctl_template/api/context.tpl +++ b/goctl_template/api/context.tpl @@ -25,7 +25,7 @@ func NewServiceContext(c {{.config}}) *ServiceContext { } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -37,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 81ff56cd..4f87ec4e 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -2,6 +2,7 @@ package gmodel import ( "context" + "gorm.io/gorm" ) @@ -51,7 +52,7 @@ func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (res func (a *FsAddressModel) UpdateAddAddress(ctx context.Context, address *FsAddress) (err error) { err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { if *address.IsDefault > 0 { - err = tx.Where("user_id = ? and is_default = 1", *address.UserId).Update("is_default", 0).Error + err = tx.Where("user_id = ? and is_default = 1", address.UserId).Update("is_default", 0).Error if err != nil { return err } diff --git a/model/gmodel/fs_pay_logic.go b/model/gmodel/fs_pay_logic.go index 30cdfa2b..b8de697d 100644 --- a/model/gmodel/fs_pay_logic.go +++ b/model/gmodel/fs_pay_logic.go @@ -9,10 +9,8 @@ func (p *FsPayModel) GetListByOrderNumber(ctx context.Context, sn string) (resp } return resp, nil } + func (p *FsPayModel) GetOrderPayList(ctx context.Context, sn string, payStatus int64, isRefund int64) (resp []FsPay, err error) { err = p.db.WithContext(ctx).Model(&FsPay{}).Where("`order_number` = ? and `pay_status` = ? and `is_refund` = ?", sn, payStatus, isRefund).Find(&resp).Error - if err != nil { - return nil, err - } - return resp, nil + return resp, err } diff --git a/model/gmodel/fs_refund_reason_logic.go b/model/gmodel/fs_refund_reason_logic.go index 7844bfaa..be92652b 100644 --- a/model/gmodel/fs_refund_reason_logic.go +++ b/model/gmodel/fs_refund_reason_logic.go @@ -1,9 +1,24 @@ package gmodel -import "context" +import ( + "context" + + "gorm.io/gorm" +) // TODO: 使用model的属性做你想做的 -func (fsRefundReason *FsRefundReasonModel) Update(cxt context.Context) { +func (m *FsRefundReasonModel) Create(ctx context.Context, obj *FsRefundReason) error { + return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + return nil + }) +} + +func (m *FsRefundReasonModel) Update(ctx context.Context, obj *FsRefundReason) error { + return m.db.WithContext(ctx).Model(obj).Where("`id` = ?", obj.Id).Updates(obj).Error +} + +func (m *FsRefundReasonModel) UpdateByRefundReasonId(ctx context.Context, obj *FsRefundReason) error { + return m.db.WithContext(ctx).Model(obj).Where("`refund_reason_id` = ?", obj.RefundReasonId).Updates(obj).Error } diff --git a/server/canteen/internal/svc/servicecontext.go b/server/canteen/internal/svc/servicecontext.go index b01e6e99..482d13c6 100644 --- a/server/canteen/internal/svc/servicecontext.go +++ b/server/canteen/internal/svc/servicecontext.go @@ -5,9 +5,10 @@ import ( "fmt" "fusenapi/initalize" "fusenapi/server/canteen/internal/config" + "net/http" + "github.com/golang-jwt/jwt" "gorm.io/gorm" - "net/http" ) type ServiceContext struct { @@ -24,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -36,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/server/data-transfer/internal/svc/servicecontext.go b/server/data-transfer/internal/svc/servicecontext.go index f60586a5..795c0c3c 100644 --- a/server/data-transfer/internal/svc/servicecontext.go +++ b/server/data-transfer/internal/svc/servicecontext.go @@ -5,9 +5,10 @@ import ( "fmt" "fusenapi/initalize" "fusenapi/server/data-transfer/internal/config" + "net/http" + "github.com/golang-jwt/jwt" "gorm.io/gorm" - "net/http" ) type ServiceContext struct { @@ -24,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -36,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/server/home-user-auth/etc/home-user-auth.yaml b/server/home-user-auth/etc/home-user-auth.yaml index 0628bf73..65d333ee 100644 --- a/server/home-user-auth/etc/home-user-auth.yaml +++ b/server/home-user-auth/etc/home-user-auth.yaml @@ -7,3 +7,6 @@ Auth: AccessSecret: fusen2023 AccessExpire: 60 RefreshAfter: 60 + +Stripe: + SK: "123" diff --git a/server/home-user-auth/internal/config/config.go b/server/home-user-auth/internal/config/config.go index 825f66bc..44f26567 100644 --- a/server/home-user-auth/internal/config/config.go +++ b/server/home-user-auth/internal/config/config.go @@ -10,4 +10,8 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + + Stripe struct { + SK string + } } diff --git a/server/home-user-auth/internal/logic/useroderdeletelogic.go b/server/home-user-auth/internal/logic/useroderdeletelogic.go index e587cd22..4f646627 100644 --- a/server/home-user-auth/internal/logic/useroderdeletelogic.go +++ b/server/home-user-auth/internal/logic/useroderdeletelogic.go @@ -13,6 +13,9 @@ import ( "fusenapi/server/home-user-auth/internal/svc" "fusenapi/server/home-user-auth/internal/types" + "github.com/stripe/stripe-go/v74" + "github.com/stripe/stripe-go/v74/client" + "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) @@ -104,27 +107,40 @@ func (l *UserOderDeleteLogic) UserOderDelete(req *types.RequestOrderId, userinfo CreatedAt: &CreatedAt, } - mFsRefund := gmodel.NewFsRefundReasonModel(l.svcCtx.MysqlConn) - - err = refund.Save() + mFsRefundReason := gmodel.NewFsRefundReasonModel(l.svcCtx.MysqlConn) + err = mFsRefundReason.Create(l.ctx, refund) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr) + } // 退款 // 调用第三方接口发起退款 - config := GetStripeConfig() + + config := &stripe.BackendConfig{ + MaxNetworkRetries: stripe.Int64(0), // Zero retries + } + + sc := &client.API{} + sc.Init(l.svcCtx.Config.Stripe.SK, &stripe.Backends{ + API: stripe.GetBackendWithConfig(stripe.APIBackend, config), + Uploads: stripe.GetBackendWithConfig(stripe.UploadsBackend, config), + }) + // ['order_number' => $order->sn, 'is_refund' => 0, 'pay_status' => 1] + pay := gmodel.NewFsPayModel(l.svcCtx.MysqlConn) // 查询支付信息 - pays, err := Pay.Find(db.Where("order_number = ? AND is_refund = ? AND pay_status = ?", order.SN, 0, 1)) + pays, err := pay.GetOrderPayList(l.ctx, *order.Sn, 1, 0) for _, pay := range pays { - stripe := stripe.New(config.SK) - _, err := stripe.Refunds.Create(stripe.RefundParams{ + sc.Refunds.New(&stripe.RefundParams{ PaymentIntent: pay.TradeNo, }) } - if err == nil && err == nil { - return ResponseSuccess(200, "Cancel successfully", order) - } - return ResponseError(500, "Cancellation failure") + // if err == nil && err == nil { + // return ResponseSuccess(200, "Cancel successfully", order) + // } + // return ResponseError(500, "Cancellation failure") return resp.SetStatus(basic.CodeOK) } diff --git a/server/home-user-auth/internal/svc/servicecontext.go b/server/home-user-auth/internal/svc/servicecontext.go index 5ab0161f..0a127e7c 100644 --- a/server/home-user-auth/internal/svc/servicecontext.go +++ b/server/home-user-auth/internal/svc/servicecontext.go @@ -23,7 +23,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if AuthKey == "" { return nil, nil @@ -35,7 +35,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return []byte(svcCxt.Config.Auth.AccessSecret), nil + return []byte(svcCtx.Config.Auth.AccessSecret), nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/server/map-library/internal/svc/servicecontext.go b/server/map-library/internal/svc/servicecontext.go index 3f4cf673..29087239 100644 --- a/server/map-library/internal/svc/servicecontext.go +++ b/server/map-library/internal/svc/servicecontext.go @@ -5,9 +5,10 @@ import ( "fmt" "fusenapi/initalize" "fusenapi/server/map-library/internal/config" + "net/http" + "github.com/golang-jwt/jwt" "gorm.io/gorm" - "net/http" ) type ServiceContext struct { @@ -24,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -36,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/server/orders/internal/svc/servicecontext.go b/server/orders/internal/svc/servicecontext.go index 16031242..544a6191 100644 --- a/server/orders/internal/svc/servicecontext.go +++ b/server/orders/internal/svc/servicecontext.go @@ -5,9 +5,10 @@ import ( "fmt" "fusenapi/initalize" "fusenapi/server/orders/internal/config" + "net/http" + "github.com/golang-jwt/jwt" "gorm.io/gorm" - "net/http" ) type ServiceContext struct { @@ -24,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -36,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/server/product-templatev2/internal/svc/servicecontext.go b/server/product-templatev2/internal/svc/servicecontext.go index 2f214a7d..76ee9a8e 100644 --- a/server/product-templatev2/internal/svc/servicecontext.go +++ b/server/product-templatev2/internal/svc/servicecontext.go @@ -2,11 +2,13 @@ package svc import ( "errors" + "fmt" "fusenapi/initalize" "fusenapi/server/product-templatev2/internal/config" + "net/http" + "github.com/golang-jwt/jwt" "gorm.io/gorm" - "net/http" ) type ServiceContext struct { @@ -23,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -35,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/server/product/internal/svc/servicecontext.go b/server/product/internal/svc/servicecontext.go index 6705de1e..300cfad0 100644 --- a/server/product/internal/svc/servicecontext.go +++ b/server/product/internal/svc/servicecontext.go @@ -25,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -37,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/server/shopping-cart-confirmation/internal/svc/servicecontext.go b/server/shopping-cart-confirmation/internal/svc/servicecontext.go index 4ea1a88a..35fd6c08 100644 --- a/server/shopping-cart-confirmation/internal/svc/servicecontext.go +++ b/server/shopping-cart-confirmation/internal/svc/servicecontext.go @@ -25,7 +25,7 @@ func NewServiceContext(c config.Config) *ServiceContext { } } -func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if len(AuthKey) <= 50 { return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) @@ -37,7 +37,7 @@ func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, err return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // 返回用于验证签名的密钥 - return svcCxt.Config.Auth.AccessSecret, nil + return svcCtx.Config.Auth.AccessSecret, nil }) if err != nil { return nil, errors.New(fmt.Sprint("Error parsing token:", err)) diff --git a/utils/basic/basic.go b/utils/basic/basic.go index 5f338c0a..84d2f87c 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -68,8 +68,17 @@ func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Respon } else { newResp.Data = data[0] } - } else if len(data) > 1 { - newResp.Data = data + } else if len(data) == 2 { + var dataIndex = 0 + if err, ok := data[dataIndex].(error); ok { + newResp.Message = err.Error() + dataIndex++ + } else { + panic("data只接收1或2个 data 或 error.Error() 和 data ") + } + newResp.Data = data[1] + } else { + panic("data只接收1或2个 data, error.Error() 和 data ") } return newResp @@ -83,6 +92,21 @@ func (resp *Response) SetStatusWithMessage(sr *StatusResponse, msg string, data } if len(data) == 1 { newResp.Data = data[0] + } else { + panic("data只接收一个 data") + } + return newResp +} + +func (resp *Response) SetStatusAddMessage(sr *StatusResponse, msg string, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + Message: sr.Message + ":" + msg, + } + if len(data) == 1 { + newResp.Data = data[0] + } else { + panic("data只接收一个") } return newResp }