This commit is contained in:
2024-10-15 17:19:23 +08:00
parent 8f903b457f
commit c76451788c
42 changed files with 1600 additions and 237 deletions

View File

@@ -22,6 +22,12 @@ func EncryptStructFields(inputStruct interface{}, key string) (map[string]interf
field := v.Type().Field(i)
fieldValue := v.Field(i)
// 检查字段的 encrypt 标签是否为 "false"
encryptTag := field.Tag.Get("encrypt")
if encryptTag == "false" {
continue
}
// 如果字段为空值,跳过
if fieldValue.IsZero() {
continue

View File

@@ -0,0 +1,31 @@
package YYSY
import (
"net/http"
"tianyuan-api/pkg/response"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/api/internal/logic/YYSY"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func YYSY4B21Handler(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 {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := YYSY.NewYYSY4B21Logic(r.Context(), svcCtx)
resp, err := l.YYSY4B21(&req)
if err != nil {
response.Fail(r.Context(), w, err)
} else {
response.Success(r.Context(), w, resp)
}
}
}

View File

@@ -187,6 +187,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/YYSY09CD",
Handler: YYSY.YYSY09CDHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/YYSY4B21",
Handler: YYSY.YYSY4B21Handler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/YYSY4B37",

View File

@@ -88,7 +88,7 @@ func (l *FLXG970FLogic) FLXG970F(req *types.Request) (resp string, err *errs.App
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG970FFieldMapping, "")
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG970FFieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("WEST00028", apiRequest)
if callAPIErr != nil {

View File

@@ -88,7 +88,7 @@ func (l *QYGL2ACDLogic) QYGL2ACD(req *types.Request) (resp string, err *errs.App
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGL2ACDFieldMapping, "")
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGL2ACDFieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("WEST00022", apiRequest)
if callAPIErr != nil {

View File

@@ -88,7 +88,7 @@ func (l *QYGL45BDLogic) QYGL45BD(req *types.Request) (resp string, err *errs.App
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGL45BDFieldMapping, "")
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGL45BDFieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("WEST00021", apiRequest)
if callAPIErr != nil {

View File

@@ -88,7 +88,7 @@ func (l *QYGLB4C0Logic) QYGLB4C0(req *types.Request) (resp string, err *errs.App
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGLB4C0FieldMapping, "")
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGLB4C0FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G05HZ01", apiRequest)
if callAPIErr != nil {

View File

@@ -0,0 +1,103 @@
package YYSY
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"
"github.com/zeromicro/go-zero/core/logx"
)
type YYSY4B21Logic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewYYSY4B21Logic(ctx context.Context, svcCtx *svc.ServiceContext) *YYSY4B21Logic {
return &YYSY4B21Logic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *YYSY4B21Logic) YYSY4B21(req *types.Request) (resp string, err *errs.AppError) {
var status string
var charges bool
var remark = ""
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return "", errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return "", errs.ErrSystem
}
userId, userIdOk := l.ctx.Value("userId").(int64)
if !userIdOk {
return "", errs.ErrSystem
}
productCode, productCodeOk := l.ctx.Value("productCode").(string)
if !productCodeOk || productCode == "" {
return "", errs.ErrSystem
}
defer func() {
if err != nil {
status = "failed"
charges = false
} else {
status = "success"
charges = true
}
sendApiRequestMessageErr := l.svcCtx.ApiRequestMqsService.SendApiRequestMessage(l.ctx, transactionID, userId, productCode, status, charges, remark)
if sendApiRequestMessageErr != nil {
logx.Errorf("发送 API 请求消息失败: %v", err)
}
}()
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return "", errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return "", errs.ErrParamDecryption
}
// 2、校验
var data validator.YYSY4B21Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return "", errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, encryptStructFieldsErr := common.EncryptStructFields(data, westConfig.Key)
if encryptStructFieldsErr != nil {
logx.Errorf("西部加密错误:%v", encryptStructFieldsErr)
return "", errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.YYSY4B21FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G25BJ02", apiRequest)
if callAPIErr != nil {
return "", errs.ErrSystem
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return "", errs.ErrSystem
}
return encryptData, nil
}

View File

@@ -117,6 +117,9 @@ type QYGLB4C0Request struct {
type YYSY4B37Request struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
}
type YYSY4B21Request struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
}
type YYSY6F2ERequest struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
MobileType string `json:"mobile_type" validate:"omitempty,validMobileType"`
@@ -139,5 +142,5 @@ type YYSYD50FRequest struct {
}
type YYSYF7DBRequest struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
StartDate string `json:"start_date" validate:"required,validDate"`
StartDate string `json:"start_date" validate:"required,validDate" encrypt:"false"`
}

View File

@@ -105,7 +105,7 @@ func validDate(fl validator.FieldLevel) bool {
// 自定义身份证校验
func validIDCard(fl validator.FieldLevel) bool {
id := fl.Field().String()
validIDPattern := `^\d{15}$|^\d{18}$|^\d{17}(\d|X|x)$` // 匹配15位或18位身份证
validIDPattern := `^\d{17}(\d|X|x)$` // 匹配18位身份证号码
matched, _ := regexp.MatchString(validIDPattern, id)
return matched
}

View File

@@ -101,6 +101,9 @@ var QYGLB4C0FieldMapping = map[string]string{
var YYSY4B37FieldMapping = map[string]string{
"MobileNo": "phone",
}
var YYSY4B21FieldMapping = map[string]string{
"MobileNo": "phone",
}
var YYSY6F2EFieldMapping = map[string]string{
"IDCard": "idNo",
"Name": "name",