This commit is contained in:
2024-10-12 20:41:55 +08:00
parent 8c09120db6
commit 597e4f1b89
75 changed files with 5009 additions and 823 deletions

View File

@@ -14,12 +14,15 @@ import (
"tianyuan-api/apps/sentinel/client/userproduct"
"tianyuan-api/apps/sentinel/client/whitelist"
"tianyuan-api/apps/sentinel/sentinel"
"tianyuan-api/apps/user/user"
"tianyuan-api/pkg/crypto"
)
type ApiAuthInterceptorMiddleware struct {
WhitelistRpc sentinel.WhitelistClient
SecretRpc sentinel.SecretClient
UserProductRpc sentinel.UserProductClient
UserRpc user.UserClient
Rds *redis.Redis
}
@@ -27,11 +30,13 @@ func NewApiAuthInterceptorMiddleware(
whitelistRpc sentinel.WhitelistClient,
secretRpc sentinel.SecretClient,
userProductRpc sentinel.UserProductClient,
userRpc user.UserClient,
rds *redis.Redis) *ApiAuthInterceptorMiddleware {
return &ApiAuthInterceptorMiddleware{
WhitelistRpc: whitelistRpc,
SecretRpc: secretRpc,
UserProductRpc: userProductRpc,
UserRpc: userRpc,
Rds: rds,
}
}
@@ -83,8 +88,21 @@ func (m *ApiAuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.Handle
return
}
// 3、是否有开通该产品
userId := secrets.UserId
// 3、额度是否冻结
info, err := m.UserRpc.GetUserInfo(r.Context(), &user.UserInfoReq{UserId: userId})
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New("系统错误,请联系管理员"))
return
}
if info.QuotaExceeded == 1 {
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New("账户余额不足,无法请求"))
return
}
// 4、是否有开通该产品
pathParts := strings.Split(r.URL.Path, "/")
productCode := pathParts[len(pathParts)-1]
userProductRedisKey := fmt.Sprintf("user_products:%d", userId)
@@ -106,7 +124,13 @@ func (m *ApiAuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.Handle
// 将 userId 存入 context供后续逻辑使用
ctx := context.WithValue(r.Context(), "userId", userId)
ctx = context.WithValue(r.Context(), "secretKey", secrets.AesKey)
ctx = context.WithValue(ctx, "secretKey", secrets.AesKey)
ctx = context.WithValue(ctx, "productCode", productCode)
// 生成流水号
transactionID := crypto.GenerateTransactionID()
ctx = context.WithValue(ctx, "transactionID", transactionID)
next(w, r.WithContext(ctx))
}
}

View File

@@ -0,0 +1,53 @@
package middleware
import (
"github.com/zeromicro/go-zero/core/logx"
"net/http"
"tianyuan-api/apps/api/internal/service"
)
type ApiMqsInterceptorMiddleware struct {
ApiRequestMqsService *service.ApiRequestMqsService
}
func NewApiMqsInterceptorMiddleware(apiRequestMqsService *service.ApiRequestMqsService) *ApiMqsInterceptorMiddleware {
return &ApiMqsInterceptorMiddleware{
ApiRequestMqsService: apiRequestMqsService,
}
}
func (m *ApiMqsInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
next(w, r)
//响应拦截
ctx := r.Context()
transactionID, ok := ctx.Value("transactionID").(string)
if ok && transactionID != "" {
userId, userIdOk := ctx.Value("userId").(int64)
if !userIdOk {
logx.Error("userId 不存在或类型错误")
return
}
productCode, productCodeOk := ctx.Value("productCode").(string)
if !productCodeOk || productCode == "" {
logx.Errorf("productCode 不存在或为空")
}
status, statusOk := ctx.Value("status").(string)
if !statusOk || status == "" {
status = "failed"
}
remark, remarkOk := ctx.Value("remark").(string)
if !remarkOk || remark == "" {
}
charges, chargesOk := ctx.Value("charges").(bool)
if !chargesOk || !charges {
charges = false
}
err := m.ApiRequestMqsService.SendApiRequestMessage(ctx, transactionID, userId, productCode, status, charges, remark)
if err != nil {
logx.Errorf("发送 API 请求消息失败: %v", err)
}
}
}
}