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

@@ -18,7 +18,7 @@ type response {
@server (
group: IVYZ
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler IVYZ0B03
@@ -43,7 +43,7 @@ service api-api {
@server (
group: FLXG
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler FLXGCA3D
@@ -80,7 +80,7 @@ service api-api {
@server (
group: QYGL
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler QYGLB4C0
@@ -102,7 +102,7 @@ service api-api {
@server (
group: YYSY
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler YYSY6F2E
@@ -127,7 +127,7 @@ service api-api {
@server (
group: JRZQ
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler JRZQDCBE
@@ -141,6 +141,5 @@ service api-api {
@handler JRZQ4AA8
post /JRZQ4AA8 (request) returns (response)
}

View File

@@ -18,4 +18,9 @@ KqPusherConf:
WestConfig:
Url: "http://proxy.tianyuanapi.com/api/invoke"
Key: "121a1e41fc1690dd6b90afbcacd80cf4"
SecretId: "449159"
SecretId: "449159"
UserRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: user.rpc

View File

@@ -11,6 +11,11 @@ SentinelRpc:
Hosts:
- tyapi_etcd:2379
Key: sentinel.rpc
UserRpc:
Etcd:
Hosts:
- tyapi_etcd:2379
Key: user.rpc
KqPusherConf:
Brokers:
- tyapi_kafka:9092

View File

@@ -0,0 +1,44 @@
package common
import (
"errors"
"fmt"
"reflect"
"tianyuan-api/pkg/crypto"
)
// EncryptFields 加密字段的函数,处理不同类型,并跳过空值字段
func EncryptStructFields(inputStruct interface{}, key string) (map[string]interface{}, error) {
encryptedFields := make(map[string]interface{})
// 使用反射获取结构体的类型和值
v := reflect.ValueOf(inputStruct)
if v.Kind() != reflect.Struct {
return nil, errors.New("input is not a struct")
}
// 遍历结构体字段
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
fieldValue := v.Field(i)
// 如果字段为空值,跳过
if fieldValue.IsZero() {
continue
}
// 将字段的值转换为字符串进行加密
strValue := fmt.Sprintf("%v", fieldValue.Interface())
// 执行加密操作
encryptedValue, err := crypto.WestDexEncrypt(strValue, key)
if err != nil {
return nil, err
}
// 将加密后的值存入结果映射
encryptedFields[field.Name] = encryptedValue
}
return encryptedFields, nil
}

View File

@@ -0,0 +1,21 @@
package common
func MapStructToAPIRequest(encryptedFields map[string]interface{}, fieldMapping map[string]string, wrapField string) map[string]interface{} {
apiRequest := make(map[string]interface{})
// 遍历字段映射表
for structField, apiField := range fieldMapping {
// 如果加密后的字段存在,才添加到请求
if value, exists := encryptedFields[structField]; exists {
apiRequest[apiField] = value
}
}
// 如果 wrapField 不为空,将 apiRequest 包裹到该字段下
if wrapField != "" {
return map[string]interface{}{
wrapField: apiRequest,
}
}
return apiRequest
}

View File

@@ -11,6 +11,7 @@ type Config struct {
DataSource string // 数据库连接的 DSN 字符串
CacheRedis cache.CacheConf // 缓存配置,使用 go-zero 自带的缓存配置结构体
SentinelRpc zrpc.RpcClientConf
UserRpc zrpc.RpcClientConf
KqPusherConf KqPusherConf
WestConfig WestConfig
}

View File

@@ -1,6 +1,7 @@
package IVYZ
import (
xhttp "github.com/zeromicro/x/http"
"net/http"
"tianyuan-api/pkg/response"
@@ -14,10 +15,9 @@ func IVYZ5733Handler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
response.Fail(r.Context(), w, err)
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := IVYZ.NewIVYZ5733Logic(r.Context(), svcCtx)
resp, err := l.IVYZ5733(&req)
if err != nil {

View File

@@ -19,7 +19,7 @@ import (
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@@ -78,7 +78,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@@ -117,7 +117,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@@ -134,21 +134,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/JRZQ8203",
Handler: JRZQ.JRZQ8203Handler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/JRZQCEE8",
Handler: JRZQ.JRZQCEE8Handler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/JRZQDCBE",
Handler: JRZQ.JRZQDCBEHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/JRZQFEF8",
Handler: JRZQ.JRZQFEF8Handler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/v1"),
@@ -156,7 +146,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@@ -168,11 +158,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/QYGL45BD",
Handler: QYGL.QYGL45BDHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/QYGL51BC",
Handler: QYGL.QYGL51BCHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/QYGL6F2D",
@@ -195,7 +180,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,

View File

@@ -2,6 +2,13 @@ package FLXG
import (
"context"
"encoding/hex"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@@ -24,7 +31,64 @@ func NewFLXG162ALogic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG162A
}
func (l *FLXG162ALogic) FLXG162A(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG162ARequest
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG162AFieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G32BJ05", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
var respData westmodel.G32BJ05Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Data.Code == "00" || respData.Data.Code == "100002" {
l.ctx = context.WithValue(l.ctx, "Charges", true)
} else {
return nil, errs.ErrSystem
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: encryptData,
}, nil
}

View File

@@ -3,9 +3,12 @@ package FLXG
import (
"context"
"encoding/hex"
"errors"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@@ -27,72 +30,66 @@ func NewFLXG3D56Logic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG3D56
}
}
// FLXG3D56 特殊名单
func (l *FLXG3D56Logic) FLXG3D56(req *types.Request) (resp *types.Response, err error) {
//userId, ok := l.ctx.Value("userId").(int64)
//if !ok {
// return &types.Response{}, errors.New("系统错误,请联系管理员")
//}
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errors.New("系统错误,请联系管理员")
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, err := hex.DecodeString(secretKey)
decryptData, err := crypto.AesDecrypt(req.Data, key)
if err != nil || len(decryptData) == 0 {
return nil, errors.New("参数解密失败")
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
// 2、校验
var data validator.FLXG3D56Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, validatorErr
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
mobileNo, err := crypto.WestDexEncrypt(data.MobileNo, westConfig.Key)
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
name, err := crypto.WestDexEncrypt(data.Name, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
idCard, err := crypto.WestDexEncrypt(data.IDCard, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
timeRange, err := crypto.WestDexEncrypt(data.TimeRange, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
return nil, errs.ErrSystem
}
// 4、发送请求到西部
westdexRequest := map[string]interface{}{
"id": idCard,
"cell": mobileNo,
"name": name,
"time_range": timeRange,
}
westResp, err := l.svcCtx.WestDexService.CallAPI("G26BJ05", westdexRequest)
if err != nil {
return nil, err
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG3D56FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G26BJ05", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G09GX01Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, unmarshalErr
//}
//crypto.AesEncrypt()
var respData westmodel.G26BJ05Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Data.Code == "00" || respData.Data.Code == "100002" {
l.ctx = context.WithValue(l.ctx, "Charges", true)
} else {
return nil, errs.ErrSystem
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: string(westResp),
Data: encryptData,
}, nil
}

View File

@@ -2,6 +2,13 @@ package FLXG
import (
"context"
"encoding/hex"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@@ -24,7 +31,64 @@ func NewFLXG54F5Logic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG54F5
}
func (l *FLXG54F5Logic) FLXG54F5(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG54F5Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG54F5FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G03HZ01", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
var respData westmodel.G03HZ01Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Code == "0000" {
l.ctx = context.WithValue(l.ctx, "Charges", true)
} else {
return nil, errs.ErrSystem
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: encryptData,
}, nil
}

View File

@@ -2,6 +2,12 @@ package FLXG
import (
"context"
"encoding/hex"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@@ -24,7 +30,64 @@ func NewFLXG5876Logic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG5876
}
func (l *FLXG5876Logic) FLXG5876(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG5876Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG5876FieldMapping, "")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G03XM02", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G26BJ05Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, errs.ErrSystem
//}
//
//if respData.Data.Code == "00" || respData.Data.Code == "100002" {
// l.ctx = context.WithValue(l.ctx, "Charges", true)
//} else {
// return nil, errs.ErrSystem
//}
//encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
//if aesEncrypt != nil {
// return nil, errs.ErrSystem
//}
return &types.Response{
Data: string(westResp),
}, nil
}

View File

@@ -2,6 +2,12 @@ package FLXG
import (
"context"
"encoding/hex"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@@ -24,7 +30,64 @@ func NewFLXG970FLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG970F
}
func (l *FLXG970FLogic) FLXG970F(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG970FRequest
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG970FFieldMapping, "")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("WEST00028", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G32BJ05Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, errs.ErrSystem
//}
//
//if respData.Data.Code == "00" || respData.Data.Code == "100002" {
// l.ctx = context.WithValue(l.ctx, "Charges", true)
//} else {
// return nil, errs.ErrSystem
//}
//encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
//if aesEncrypt != nil {
// return nil, errs.ErrSystem
//}
return &types.Response{
Data: string(westResp),
}, nil
}

View File

@@ -3,11 +3,14 @@ package IVYZ
import (
"context"
"encoding/hex"
"errors"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -26,64 +29,66 @@ func NewIVYZ5733Logic(ctx context.Context, svcCtx *svc.ServiceContext) *IVYZ5733
}
}
type CustomResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"` // `omitempty` 使得当 Data 为空时字段不返回
}
func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err error) {
//userId, ok := l.ctx.Value("userId").(int64)
//if !ok {
// return &types.Response{}, errors.New("系统错误,请联系管理员")
//}
func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err *errs.AppError) {
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errors.New("系统错误,请联系管理员")
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, err := hex.DecodeString(secretKey)
decryptData, err := crypto.AesDecrypt(req.Data, key)
if err != nil || len(decryptData) == 0 {
return nil, errors.New("参数解密失败")
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
// 2、校验
var data validator.IVYZ5733Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, validatorErr
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
name, err := crypto.WestDexEncrypt(data.Name, westConfig.Key)
if err != nil {
encryptedFields, encryptStructFieldsErr := common.EncryptStructFields(data, westConfig.Key)
if encryptStructFieldsErr != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
idCard, err := crypto.WestDexEncrypt(data.IDCard, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
// 4、发送请求到西部
westdexRequest := map[string]interface{}{
"id": idCard,
"name": name,
}
westResp, err := l.svcCtx.WestDexService.CallAPI("G09GX01", westdexRequest)
if err != nil {
return nil, err
return nil, errs.ErrSystem
}
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.IVYZ5733FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G09GX01", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G09GX01Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, unmarshalErr
//}
//crypto.AesEncrypt()
var respData westmodel.G09GX01Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Code == 400 || respData.Code == 500 || respData.Code == 999 {
logx.Errorf("西部响应错误%v", respData)
return nil, errs.ErrSystem
}
if respData.Code == 201 || respData.Code == 202 || respData.Code == 203 {
l.ctx = context.WithValue(l.ctx, "Charges", true)
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: string(westResp),
Data: encryptData,
}, nil
}

View File

@@ -24,11 +24,6 @@ func NewQYGL2ACDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QYGL2ACD
}
func (l *QYGL2ACDLogic) QYGL2ACD(req *types.Request) (resp *types.Response, err error) {
data := "zhangSan"
err = l.svcCtx.KqPusherClient.Push(l.ctx, data)
if err != nil {
logx.Errorf("KqPusherClient Push Error , err :%v", err)
}
return &types.Response{
Data: "三要素合演",
}, nil

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)
}
}
}
}

View File

@@ -0,0 +1,41 @@
package service
import (
"context"
"encoding/json"
"github.com/zeromicro/go-queue/kq"
"tianyuan-api/pkg/models"
"time"
)
type ApiRequestMqsService struct {
KqPusherClient *kq.Pusher
}
// NewApiRequestMqsService
func NewApiRequestMqsService(KqPusherClient *kq.Pusher) *ApiRequestMqsService {
return &ApiRequestMqsService{
KqPusherClient: KqPusherClient,
}
}
func (s *ApiRequestMqsService) SendApiRequestMessage(ctx context.Context, transactionID string, userId int64, productCode string, status string, charges bool, remark string) error {
message := models.ApiRequestMessage{
TransactionID: transactionID,
UserId: userId,
ProductCode: productCode,
Status: status,
Charges: charges,
Remark: remark,
Timestamp: time.Now(),
}
msgBytes, marshalErr := json.Marshal(message)
if marshalErr != nil {
return marshalErr
}
if pushErr := s.KqPusherClient.Push(ctx, string(msgBytes)); pushErr != nil {
return pushErr
}
return nil
}

View File

@@ -44,11 +44,7 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
// 构造请求URL
reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretId, code, timestamp)
// 将请求参数编码为JSON格式
data := map[string]interface{}{
"data": reqData,
}
jsonData, err := json.Marshal(data)
jsonData, err := json.Marshal(reqData)
if err != nil {
logx.Errorf("【西部数据请求】JSON编码错误: %v", err)
return
@@ -94,7 +90,7 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
logx.Errorf("【西部数据请求】JSON反序列化错误: %v", UnmarshalErr)
return nil, UnmarshalErr
}
logx.Infof("西部流水号: %s", westDexResp.ID)
// 到这层是西部系统
if westDexResp.Code != "00000" {
logx.Errorf("【西部数据请求】响应数据业务异常: %s %s", westDexResp.Message, westDexResp.Reason)

View File

@@ -9,18 +9,29 @@ import (
"tianyuan-api/apps/api/internal/middleware"
"tianyuan-api/apps/api/internal/service"
"tianyuan-api/apps/sentinel/sentinel"
"tianyuan-api/apps/user/user"
"time"
)
type ServiceContext struct {
Config config.Config
ApiAuthInterceptor rest.Middleware
Redis *redis.Redis
WhitelistRpc sentinel.WhitelistClient
SecretRpc sentinel.SecretClient
ProductRpc sentinel.ProductClient
UserProductRpc sentinel.UserProductClient
KqPusherClient *kq.Pusher
WestDexService *service.WestDexService
Config config.Config
ApiAuthInterceptor rest.Middleware
ApiMqsInterceptor rest.Middleware
Redis *redis.Redis
WhitelistRpc sentinel.WhitelistClient
SecretRpc sentinel.SecretClient
ProductRpc sentinel.ProductClient
UserProductRpc sentinel.UserProductClient
WestDexService *service.WestDexService
ApiRequestMqsService *service.ApiRequestMqsService
}
type ApiRequestMessage struct {
TransactionID string `json:"transactionID"`
UserId string `json:"userId"`
ProductCode string `json:"productCode"`
Status string `json:"status"` // 1. success 2. error
Charges bool `json:"charges"` // 是否扣费
Timestamp time.Time `json:"timestamp"` // 添加时间戳
}
func NewServiceContext(c config.Config) *ServiceContext {
@@ -36,15 +47,19 @@ func NewServiceContext(c config.Config) *ServiceContext {
userProductRpc := sentinel.NewUserProductClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
whitelistRpc := sentinel.NewWhitelistClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
secretRpc := sentinel.NewSecretClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
userRpc := user.NewUserClient(zrpc.MustNewClient(c.UserRpc).Conn())
KqPusherClient := kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic)
apiRequestMqsService := service.NewApiRequestMqsService(KqPusherClient)
return &ServiceContext{
Config: c,
Redis: rds,
WhitelistRpc: whitelistRpc,
SecretRpc: secretRpc,
ProductRpc: productRpc,
UserProductRpc: userProductRpc,
ApiAuthInterceptor: middleware.NewApiAuthInterceptorMiddleware(whitelistRpc, secretRpc, userProductRpc, rds).Handle,
KqPusherClient: kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic),
WestDexService: service.NewWestDexService(c.WestConfig), // 假设你将密钥和 ID 配置在 config 中
Config: c,
Redis: rds,
WhitelistRpc: whitelistRpc,
SecretRpc: secretRpc,
ProductRpc: productRpc,
UserProductRpc: userProductRpc,
ApiAuthInterceptor: middleware.NewApiAuthInterceptorMiddleware(whitelistRpc, secretRpc, userProductRpc, userRpc, rds).Handle,
ApiMqsInterceptor: middleware.NewApiMqsInterceptorMiddleware(apiRequestMqsService).Handle,
ApiRequestMqsService: apiRequestMqsService,
WestDexService: service.NewWestDexService(c.WestConfig), // 假设你将密钥和 ID 配置在 config 中
}
}

View File

@@ -21,6 +21,22 @@ type FLXG3D56Request struct {
Name string `json:"name" validate:"required,min=1,validName"`
TimeRange string `json:"time_range" validate:"omitempty,validTimeRange"` // 非必填字段
}
type FLXG54F5Request struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
}
type FLXG162ARequest struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
IDCard string `json:"id_card" validate:"required,validIDCard"`
Name string `json:"name" validate:"required,min=1,validName"`
}
type FLXG970FRequest struct {
IDCard string `json:"id_card" validate:"required,validIDCard"`
Name string `json:"name" validate:"required,min=1,validName"`
}
type FLXG5876Request struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
}
type IVYZ5733Request struct {
Name string `json:"name" validate:"required,min=1,validName"`
IDCard string `json:"id_card" validate:"required,validIDCard"`

View File

@@ -0,0 +1,28 @@
package westmodel
var IVYZ5733FieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
"MobileNo": "cell",
"TimeRange": "time_range",
}
var FLXG3D56FieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
}
var FLXG54F5FieldMapping = map[string]string{
"MobileNo": "mobile",
}
var FLXG162AFieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
"MobileNo": "cell",
}
var FLXG970FFieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
}
var FLXG5876FieldMapping = map[string]string{
"MobileNo": "mobile",
}

View File

@@ -6,6 +6,19 @@ type G09GX01Response struct {
Data string `json:"data"`
Success bool `json:"success"`
}
type G26BJ05Response struct {
Data struct {
Code string `json:"code"`
} `json:"data"`
}
type G03HZ01Response struct {
Code string `json:"code"`
}
type G32BJ05Response struct {
Data struct {
Code string `json:"code"`
} `json:"data"`
}
type G16BJ02Response struct {
Code int `json:"code"`
Msg string `json:"msg"`