|
@@ -2,6 +2,7 @@ package middleware
|
|
|
|
|
|
import (
|
|
import (
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "github.com/gogf/gf/v2/net/gtrace"
|
|
"net/url"
|
|
"net/url"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
@@ -22,22 +23,26 @@ import (
|
|
// step 2: 账户状态校验
|
|
// step 2: 账户状态校验
|
|
// step 3: 接口权益校验
|
|
// step 3: 接口权益校验
|
|
func filterBefore(r *ghttp.Request) error {
|
|
func filterBefore(r *ghttp.Request) error {
|
|
- ctx := router.GetGContext(r.GetCtx())
|
|
|
|
- rule := ctx.RouterRule
|
|
|
|
|
|
+ ctx, span := gtrace.NewSpan(r.Context(), "filterBefore")
|
|
|
|
+ defer span.End()
|
|
|
|
+ //r.SetCtx(ctx)
|
|
|
|
+
|
|
|
|
+ context := router.GetGContext(ctx)
|
|
|
|
+ rule := context.RouterRule
|
|
|
|
|
|
if rule.SessCheck.NeedCheck() {
|
|
if rule.SessCheck.NeedCheck() {
|
|
uCheck, eCheck := rule.SessCheck.CheckUserSession(), rule.SessCheck.CheckEntSession()
|
|
uCheck, eCheck := rule.SessCheck.CheckUserSession(), rule.SessCheck.CheckEntSession()
|
|
- if uCheck && ctx.Sess.UserId == "" {
|
|
|
|
|
|
+ if uCheck && context.Sess.UserId == "" {
|
|
return NewErrorWithCode(GLOBAL_ERR_NOTLOGIN)
|
|
return NewErrorWithCode(GLOBAL_ERR_NOTLOGIN)
|
|
}
|
|
}
|
|
- if eCheck && ctx.Sess.EntId == 0 {
|
|
|
|
|
|
+ if eCheck && context.Sess.EntId == 0 {
|
|
return NewErrorWithCode(GLOBAL_ERR_NOENT_SELECT)
|
|
return NewErrorWithCode(GLOBAL_ERR_NOENT_SELECT)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//校验账户状态,校验认证状态
|
|
//校验账户状态,校验认证状态
|
|
if rule.AccountCheck.NeedCheck() || rule.AuthCheck.NeedCheck() {
|
|
if rule.AccountCheck.NeedCheck() || rule.AuthCheck.NeedCheck() {
|
|
- if err := rpc.CheckAccountStatus(ctx.Sess.EntId, ctx.Sess.UserId, rule); err != nil {
|
|
|
|
|
|
+ if err := rpc.CheckAccountStatus(ctx, context.Sess.EntId, context.Sess.UserId, rule); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -45,11 +50,11 @@ func filterBefore(r *ghttp.Request) error {
|
|
//校验权益
|
|
//校验权益
|
|
var powerErr error
|
|
var powerErr error
|
|
if rule.PowerCheck == 1 {
|
|
if rule.PowerCheck == 1 {
|
|
- useCount, surplusCount, powerErr = rpc.CheckResourcePower(ctx.Sess.AccountId, ctx.Sess.EntAccountId, ctx.Sess.EntId, ctx.Sess.EntUserId, rule.FuncCode, r.GetHeader("functionCode"), rule.AppId)
|
|
|
|
|
|
+ useCount, surplusCount, powerErr = rpc.CheckResourcePower(ctx, context.Sess.AccountId, context.Sess.EntAccountId, context.Sess.EntId, context.Sess.EntUserId, rule.FuncCode, r.GetHeader("functionCode"), rule.AppId)
|
|
}
|
|
}
|
|
//老剑鱼用户身份判断
|
|
//老剑鱼用户身份判断
|
|
if powerErr == nil && rule.PowerType > 0 {
|
|
if powerErr == nil && rule.PowerType > 0 {
|
|
- powerErr = rpc.CheckUserPower(rule.PowerType, rule.AppId, ctx.Sess.MgoUserId, ctx.Sess.NewUid, ctx.Sess.AccountId, ctx.Sess.EntId, ctx.Sess.PositionType, ctx.Sess.PositionId)
|
|
|
|
|
|
+ powerErr = rpc.CheckUserPower(ctx, rule.PowerType, rule.AppId, context.Sess.MgoUserId, context.Sess.NewUid, context.Sess.AccountId, context.Sess.EntId, context.Sess.PositionType, context.Sess.PositionId)
|
|
}
|
|
}
|
|
if rule.NoPowerUrl == "" && powerErr != nil {
|
|
if rule.NoPowerUrl == "" && powerErr != nil {
|
|
return powerErr
|
|
return powerErr
|
|
@@ -63,7 +68,7 @@ func filterBefore(r *ghttp.Request) error {
|
|
}
|
|
}
|
|
|
|
|
|
//注入用户身份
|
|
//注入用户身份
|
|
- infusionIdentity(r, ctx.Sess, useCount, surplusCount, rule.AppId)
|
|
|
|
|
|
+ infusionHeader(r, context.Sess, useCount, surplusCount, rule.AppId)
|
|
//外部程序过滤器
|
|
//外部程序过滤器
|
|
if rule.IsOutServer {
|
|
if rule.IsOutServer {
|
|
//赛思用户身份注入
|
|
//赛思用户身份注入
|
|
@@ -78,11 +83,15 @@ func filterBefore(r *ghttp.Request) error {
|
|
// filterAfter 后置扣减逻辑。
|
|
// filterAfter 后置扣减逻辑。
|
|
// 同步处理,若扣减异常,接口返回错误信息。
|
|
// 同步处理,若扣减异常,接口返回错误信息。
|
|
func filterAfter(r *ghttp.Request) (err error) {
|
|
func filterAfter(r *ghttp.Request) (err error) {
|
|
- ctx := router.GetGContext(r.GetCtx())
|
|
|
|
|
|
+ ctx, span := gtrace.NewSpan(r.Context(), "filterAfter")
|
|
|
|
+ defer span.End()
|
|
|
|
+ //r.SetCtx(ctx)
|
|
|
|
+
|
|
|
|
+ context := router.GetGContext(ctx)
|
|
if r.GetError() != nil {
|
|
if r.GetError() != nil {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- rule := ctx.RouterRule
|
|
|
|
|
|
+ rule := context.RouterRule
|
|
// 响应完成,判断是否扣除
|
|
// 响应完成,判断是否扣除
|
|
if rule.Deduct != 0 {
|
|
if rule.Deduct != 0 {
|
|
var ids []string
|
|
var ids []string
|
|
@@ -100,12 +109,12 @@ func filterAfter(r *ghttp.Request) (err error) {
|
|
if deductNum > 0 { //资源接口异常
|
|
if deductNum > 0 { //资源接口异常
|
|
if rule.Deduct > 0 { //扣减
|
|
if rule.Deduct > 0 { //扣减
|
|
fmt.Println("扣减", deductNum, ids)
|
|
fmt.Println("扣减", deductNum, ids)
|
|
- if err = rpc.ResourcePowerDeduct(ctx.Sess.AccountId, ctx.Sess.EntAccountId, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
|
|
|
|
+ if err = rpc.ResourcePowerDeduct(ctx, context.Sess.AccountId, context.Sess.EntAccountId, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
} else { //充值
|
|
} else { //充值
|
|
fmt.Println("充值", deductNum, ids)
|
|
fmt.Println("充值", deductNum, ids)
|
|
- if err = rpc.ResourcePowerRecharge(ctx.Sess.AccountId, ctx.Sess.EntAccountId, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
|
|
|
|
+ if err = rpc.ResourcePowerRecharge(ctx, context.Sess.AccountId, context.Sess.EntAccountId, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -117,6 +126,8 @@ func filterAfter(r *ghttp.Request) (err error) {
|
|
|
|
|
|
// GetResponseJsonAndClear 获取接口返回参数,并删除扣减预留字段
|
|
// GetResponseJsonAndClear 获取接口返回参数,并删除扣减预留字段
|
|
func GetResponseJsonAndClear(r *ghttp.Request) (deductNum int64, ids []string, err error) {
|
|
func GetResponseJsonAndClear(r *ghttp.Request) (deductNum int64, ids []string, err error) {
|
|
|
|
+ _, span := gtrace.NewSpan(r.Context(), "GetResponseJsonAndClear")
|
|
|
|
+ defer span.End()
|
|
respJson := gconv.Map(r.Response.Buffer())
|
|
respJson := gconv.Map(r.Response.Buffer())
|
|
defer func() {
|
|
defer func() {
|
|
r.Response.ClearBuffer()
|
|
r.Response.ClearBuffer()
|
|
@@ -144,8 +155,10 @@ func GetResponseJsonAndClear(r *ghttp.Request) (deductNum int64, ids []string, e
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-// infusionIdentity 用户身份注入Header 有则注入
|
|
|
|
-func infusionIdentity(r *ghttp.Request, sess *router.JySession, useCount, surplusCount int64, appId string) {
|
|
|
|
|
|
+// infusionHeader 用户身份注入Header 有则注入
|
|
|
|
+func infusionHeader(r *ghttp.Request, sess *router.JySession, useCount, surplusCount int64, appId string) {
|
|
|
|
+ _, span := gtrace.NewSpan(r.GetCtx(), "infusionHeader")
|
|
|
|
+ defer span.End()
|
|
r.Header.Set("appId", appId)
|
|
r.Header.Set("appId", appId)
|
|
if sess.UserId != "" {
|
|
if sess.UserId != "" {
|
|
r.Header.Set("userId", sess.UserId)
|
|
r.Header.Set("userId", sess.UserId)
|