feat(gateway): 添加订单号缓存处理
- 在 ScanController 中集成 bigcache 用于缓存订单号 - 在处理扫码请求时,先检查订单号是否已存在缓存中 - 如果订单号已缓存,返回错误提示"订单已经提交~" - 新增测试文件 scan_controller_test.go 用于验证缓存功能
This commit is contained in:
1
go.mod
1
go.mod
@@ -23,6 +23,7 @@ require (
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/allegro/bigcache/v3 v3.1.0 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -1,5 +1,7 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
|
||||
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
|
||||
github.com/beego/beego/v2 v2.3.4 h1:HurQEOGIEhLlPFCTR6ZDuQkybrUl2Ag2i6CdVD2rGiI=
|
||||
github.com/beego/beego/v2 v2.3.4/go.mod h1:5cqHsOHJIxkq44tBpRvtDe59GuVRVv/9/tyVDxd5ce4=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
|
||||
@@ -1,39 +1,9 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"gateway/internal/schema/response"
|
||||
"gateway/internal/service"
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
"strings"
|
||||
|
||||
"github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
type BaseGateway struct {
|
||||
web.Controller
|
||||
}
|
||||
|
||||
// PayPrepare 获取商户请求过来的基本参数参数
|
||||
func (c *BaseGateway) PayPrepare() *response.PayBaseResp {
|
||||
// 获取客户端的ip
|
||||
p := service.GetMerchantInfo(map[string]any{
|
||||
"exValue": strings.TrimSpace(c.GetString("exValue")),
|
||||
"orderNo": strings.TrimSpace(c.GetString("orderNo")),
|
||||
"orderPeriod": strings.TrimSpace(c.GetString("orderPeriod")),
|
||||
"productCode": strings.TrimSpace(c.GetString("productCode")),
|
||||
"orderPrice": strings.TrimSpace(c.GetString("orderPrice")),
|
||||
"notifyUrl": strings.TrimSpace(c.GetString("notifyUrl")),
|
||||
"payKey": strings.TrimSpace(c.GetString("payKey")),
|
||||
"timestamp": strings.TrimSpace(c.GetString("timestamp")),
|
||||
"sign": strings.TrimSpace(c.GetString("sign")),
|
||||
"ip": strings.TrimSpace(c.GetString("ip")),
|
||||
"deviceId": strings.TrimSpace(c.GetString("deviceId")),
|
||||
})
|
||||
p.ClientIP = strings.TrimSpace(c.GetString("ip"))
|
||||
logs.Info("【BaseGateway】获取商户请求过来的ip地址:", p.ClientIP)
|
||||
p = service.JudgeParams(p)
|
||||
if p.Code != -1 {
|
||||
p.Code = 200
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"gateway/internal/service/supplier/t_mall_game"
|
||||
"gateway/internal/service/supplier/third_party"
|
||||
"gateway/internal/utils"
|
||||
"github.com/allegro/bigcache/v3"
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
"github.com/beego/beego/v2/core/validation"
|
||||
"github.com/bytedance/gopkg/util/gopool"
|
||||
@@ -31,7 +32,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
delayPool = gopool.NewPool("delayHandler", 20, gopool.NewConfig())
|
||||
delayPool = gopool.NewPool("delayHandler", 20, gopool.NewConfig())
|
||||
idCache, _ = bigcache.New(context.Background(), bigcache.DefaultConfig(1*time.Second))
|
||||
)
|
||||
|
||||
type ScanController struct {
|
||||
@@ -41,11 +43,40 @@ type ScanController struct {
|
||||
// Scan 处理扫码的请求
|
||||
func (c *ScanController) Scan() {
|
||||
// 获取所有请求参数
|
||||
p := c.PayPrepare()
|
||||
// 获取客户端的ip
|
||||
p := service.GetMerchantInfo(map[string]any{
|
||||
"exValue": strings.TrimSpace(c.GetString("exValue")),
|
||||
"orderNo": strings.TrimSpace(c.GetString("orderNo")),
|
||||
"orderPeriod": strings.TrimSpace(c.GetString("orderPeriod")),
|
||||
"productCode": strings.TrimSpace(c.GetString("productCode")),
|
||||
"orderPrice": strings.TrimSpace(c.GetString("orderPrice")),
|
||||
"notifyUrl": strings.TrimSpace(c.GetString("notifyUrl")),
|
||||
"payKey": strings.TrimSpace(c.GetString("payKey")),
|
||||
"timestamp": strings.TrimSpace(c.GetString("timestamp")),
|
||||
"sign": strings.TrimSpace(c.GetString("sign")),
|
||||
"ip": strings.TrimSpace(c.GetString("ip")),
|
||||
"deviceId": strings.TrimSpace(c.GetString("deviceId")),
|
||||
})
|
||||
p.ClientIP = strings.TrimSpace(c.GetString("ip"))
|
||||
cacheId, _ := idCache.Get(strings.TrimSpace(c.GetString("orderNo")))
|
||||
if len(cacheId) != 0 {
|
||||
p.Msg = "订单已经提交~"
|
||||
p.Code = -1
|
||||
c.SolveFailJSON(p)
|
||||
return
|
||||
}
|
||||
logs.Info("【BaseGateway】获取商户请求过来的ip地址:", p.ClientIP)
|
||||
p = service.JudgeParams(p)
|
||||
p = service.OrderIsValid(p)
|
||||
p = service.NotifyUrlIsValid(p)
|
||||
p = service.OrderPeriodIsValid(p)
|
||||
p = service.OrderPriceIsValid(p)
|
||||
p = service.ExValueIsValid(p)
|
||||
if p.Code == -1 {
|
||||
c.SolveFailJSON(p)
|
||||
return
|
||||
}
|
||||
|
||||
logs.Info("请求参数:%+v", p.Params)
|
||||
// 签名验证
|
||||
if !utils.Md5MFVerify(p.Params, p.MerchantInfo.MerchantSecret) &&
|
||||
|
||||
15
internal/controllers/gateway/scan_controller_test.go
Normal file
15
internal/controllers/gateway/scan_controller_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestScanController_Scan(t *testing.T) {
|
||||
cacheValue, err := idCache.Get("123456")
|
||||
logs.Info(cacheValue, err)
|
||||
err2 := idCache.Set("123456", []byte("123456"))
|
||||
logs.Info(err2)
|
||||
cacheValue, err = idCache.Get("123456")
|
||||
logs.Info(cacheValue, err)
|
||||
}
|
||||
Reference in New Issue
Block a user