|
@@ -4,7 +4,8 @@ import (
|
|
. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
|
|
. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
|
|
"bp.jydev.jianyu360.cn/BaseService/gateway/core/proxy/rpc"
|
|
"bp.jydev.jianyu360.cn/BaseService/gateway/core/proxy/rpc"
|
|
"bp.jydev.jianyu360.cn/BaseService/gateway/core/router"
|
|
"bp.jydev.jianyu360.cn/BaseService/gateway/core/router"
|
|
- "bp.jydev.jianyu360.cn/BaseService/gateway/core/util"
|
|
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"strings"
|
|
"strings"
|
|
@@ -63,52 +64,68 @@ func filterBefore(r *ghttp.Request) error {
|
|
// 同步处理,若扣减异常,接口返回错误信息。
|
|
// 同步处理,若扣减异常,接口返回错误信息。
|
|
func filterAfter(r *ghttp.Request) (err error) {
|
|
func filterAfter(r *ghttp.Request) (err error) {
|
|
ctx := router.GetGContext(r.GetCtx())
|
|
ctx := router.GetGContext(r.GetCtx())
|
|
|
|
+ if r.GetError() != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
rule := ctx.RouterRule
|
|
rule := ctx.RouterRule
|
|
// 响应完成,判断是否扣除
|
|
// 响应完成,判断是否扣除
|
|
if rule.Deduct != 0 {
|
|
if rule.Deduct != 0 {
|
|
- respJson, err := GetResponseJsonAndClear(r, "resourceNum", "resourceIds")
|
|
|
|
- if err != nil {
|
|
|
|
- //获取资源扣减异常
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if code, ok := respJson["error_code"]; !ok || gconv.Int64(code) != 0 {
|
|
|
|
- //扣减数据结构异常
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
var ids []string
|
|
var ids []string
|
|
var deductNum int64 = 1
|
|
var deductNum int64 = 1
|
|
if rule.Deduct == 2 || rule.Deduct == -2 { //后端应用
|
|
if rule.Deduct == 2 || rule.Deduct == -2 { //后端应用
|
|
- deductNum = gconv.Int64(respJson["resourceNum"])
|
|
|
|
- ids = strings.Split(gconv.String(respJson["resourceIds"]), ",")
|
|
|
|
|
|
+ var respErr error
|
|
|
|
+ deductNum, ids, respErr = GetResponseJsonAndClear(r)
|
|
|
|
+ if respErr != nil { //扣减接口异常 不修改接口内容
|
|
|
|
+ g.Log().Error(r.Context(), respErr)
|
|
|
|
+ }
|
|
} else if rule.Deduct != 1 {
|
|
} else if rule.Deduct != 1 {
|
|
deductNum = gconv.Int64(rule.Deduct)
|
|
deductNum = gconv.Int64(rule.Deduct)
|
|
}
|
|
}
|
|
|
|
|
|
- if rule.Deduct > 0 { //扣减
|
|
|
|
- if err = rpc.ResourcePowerDeduct(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- } else { //充值
|
|
|
|
- if err = rpc.ResourcePowerRecharge(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
|
|
- return err
|
|
|
|
|
|
+ if deductNum > 0 { //资源接口异常
|
|
|
|
+ if rule.Deduct > 0 { //扣减
|
|
|
|
+ fmt.Println("扣减", deductNum, ids)
|
|
|
|
+ if err = rpc.ResourcePowerDeduct(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ } else { //充值
|
|
|
|
+ fmt.Println("充值", deductNum, ids)
|
|
|
|
+ if err = rpc.ResourcePowerRecharge(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
// GetResponseJsonAndClear 获取接口返回参数,并删除扣减预留字段
|
|
// GetResponseJsonAndClear 获取接口返回参数,并删除扣减预留字段
|
|
-func GetResponseJsonAndClear(r *ghttp.Request, deleteFiled ...string) (respJson map[string]interface{}, err error) {
|
|
|
|
- err = util.ChangeResponse(r, func(b []byte) error {
|
|
|
|
- respJson = gconv.Map(b)
|
|
|
|
- for _, field := range deleteFiled {
|
|
|
|
|
|
+func GetResponseJsonAndClear(r *ghttp.Request) (deductNum int64, ids []string, err error) {
|
|
|
|
+ respJson := gconv.Map(r.Response.Buffer())
|
|
|
|
+ defer func() {
|
|
|
|
+ r.Response.ClearBuffer()
|
|
|
|
+ r.Response.WriteJson(respJson)
|
|
|
|
+ r.Response.Header().Set("Content-Length", gconv.String(len(r.Response.Buffer())))
|
|
|
|
+ g.Log().Debugf(r.Context(), "填充 %s 相应头长度%s", gconv.String(respJson), r.Response.Header().Get("Content-Length"))
|
|
|
|
+ }()
|
|
|
|
+ g.Log().Debugf(r.Context(), "获取 %s 相应头长度%s", gconv.String(respJson), r.Response.Header().Get("Content-Length"))
|
|
|
|
+
|
|
|
|
+ //仅接口返回成功后 进行资源操作
|
|
|
|
+ if code, exists := respJson["error_code"]; !(exists && code == 0) {
|
|
|
|
+ err = fmt.Errorf("扣减接口返回异常 %v", gconv.String(respJson))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ deductNum = gconv.Int64(respJson["resourceNum"])
|
|
|
|
+ if idStr := gconv.String(respJson["resourceIds"]); idStr != "" {
|
|
|
|
+ ids = strings.Split(idStr, ",")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, field := range []string{"resourceNum", "resourceIds"} {
|
|
|
|
+ if _, ok := respJson[field]; ok {
|
|
delete(respJson, field)
|
|
delete(respJson, field)
|
|
}
|
|
}
|
|
- b = gconv.Bytes(respJson)
|
|
|
|
- return nil
|
|
|
|
- })
|
|
|
|
|
|
+ }
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|