增加api optional Json
This commit is contained in:
@@ -8,7 +8,12 @@ info (
|
||||
)
|
||||
|
||||
type request {
|
||||
Data string `json:"data"`
|
||||
Data string `json:"data"`
|
||||
Options Options `json:"options,optional"`
|
||||
}
|
||||
|
||||
type Options {
|
||||
Json bool `json:"json,optional"`
|
||||
}
|
||||
|
||||
@server (
|
||||
@@ -152,5 +157,4 @@ service api-api {
|
||||
|
||||
@handler JRZQ4AA8
|
||||
post /JRZQ4AA8 (request) returns (string)
|
||||
}
|
||||
|
||||
}
|
||||
71
apps/api/internal/common/response.go
Normal file
71
apps/api/internal/common/response.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"tianyuan-api/pkg/errs"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// ParseWestResponse 解析西部返回的响应数据(获取data字段后解析)
|
||||
// westResp: 西部返回的原始响应
|
||||
// Returns: 解析后的数据字节数组
|
||||
func ParseWestResponse(westResp []byte) ([]byte, *errs.AppError) {
|
||||
dataResult := gjson.GetBytes(westResp, "data")
|
||||
if !dataResult.Exists() {
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
return ParseJsonResponse([]byte(dataResult.Raw))
|
||||
}
|
||||
|
||||
// ParseJsonResponse 直接解析JSON响应数据
|
||||
// jsonResp: JSON响应数据
|
||||
// Returns: 解析后的数据字节数组
|
||||
func ParseJsonResponse(jsonResp []byte) ([]byte, *errs.AppError) {
|
||||
parseResult, err := RecursiveParse(string(jsonResp))
|
||||
if err != nil {
|
||||
logx.Errorf("递归反序列化失败:%v", err)
|
||||
return nil, errs.ErrSystem
|
||||
}
|
||||
|
||||
resultResp, marshalErr := sonic.Marshal(parseResult)
|
||||
if marshalErr != nil {
|
||||
logx.Errorf("序列化失败:%v", marshalErr)
|
||||
return nil, errs.NewAppError(errs.ErrSystem.Code, marshalErr.Error())
|
||||
}
|
||||
|
||||
return resultResp, nil
|
||||
}
|
||||
|
||||
// RecursiveParse 递归解析JSON数据
|
||||
func RecursiveParse(data interface{}) (interface{}, *errs.AppError) {
|
||||
switch v := data.(type) {
|
||||
case string:
|
||||
var parsed interface{}
|
||||
if err := sonic.Unmarshal([]byte(v), &parsed); err == nil {
|
||||
return RecursiveParse(parsed)
|
||||
}
|
||||
return v, nil
|
||||
case map[string]interface{}:
|
||||
for key, val := range v {
|
||||
parsed, err := RecursiveParse(val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v[key] = parsed
|
||||
}
|
||||
return v, nil
|
||||
case []interface{}:
|
||||
for i, item := range v {
|
||||
parsed, err := RecursiveParse(item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v[i] = parsed
|
||||
}
|
||||
return v, nil
|
||||
default:
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
@@ -94,7 +94,17 @@ func (l *FLXG0V4BLogic) FLXG0V4B(req *types.Request) (resp string, err *errs.App
|
||||
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G22SC01", apiRequest, l.svcCtx.Config.WestConfig.SecretId)
|
||||
if callAPIErr != nil {
|
||||
if callAPIErr.Code == errs.ErrDataSource.Code {
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
||||
var resultResp []byte
|
||||
if req.Options.Json {
|
||||
var parseErr *errs.AppError
|
||||
resultResp, parseErr = common.ParseWestResponse(westResp)
|
||||
if parseErr != nil {
|
||||
return "", parseErr
|
||||
}
|
||||
} else {
|
||||
resultResp = westResp
|
||||
}
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(resultResp, key)
|
||||
if aesEncrypt != nil {
|
||||
return "", errs.ErrSystem
|
||||
}
|
||||
@@ -103,7 +113,18 @@ func (l *FLXG0V4BLogic) FLXG0V4B(req *types.Request) (resp string, err *errs.App
|
||||
return "", callAPIErr
|
||||
}
|
||||
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
||||
var resultResp []byte
|
||||
if req.Options.Json {
|
||||
var parseErr *errs.AppError
|
||||
resultResp, parseErr = common.ParseJsonResponse(westResp)
|
||||
if parseErr != nil {
|
||||
return "", parseErr
|
||||
}
|
||||
} else {
|
||||
resultResp = westResp
|
||||
}
|
||||
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(resultResp, key)
|
||||
if aesEncrypt != nil {
|
||||
return "", errs.ErrSystem
|
||||
}
|
||||
|
||||
@@ -94,7 +94,17 @@ func (l *QYGL8271Logic) QYGL8271(req *types.Request) (resp string, err *errs.App
|
||||
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("Q03SC01", apiRequest, l.svcCtx.Config.WestConfig.SecretId)
|
||||
if callAPIErr != nil {
|
||||
if callAPIErr.Code == errs.ErrDataSource.Code {
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
||||
var resultResp []byte
|
||||
if req.Options.Json {
|
||||
var parseErr *errs.AppError
|
||||
resultResp, parseErr = common.ParseJsonResponse(westResp)
|
||||
if parseErr != nil {
|
||||
return "", parseErr
|
||||
}
|
||||
} else {
|
||||
resultResp = westResp
|
||||
}
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(resultResp, key)
|
||||
if aesEncrypt != nil {
|
||||
return "", errs.ErrSystem
|
||||
}
|
||||
@@ -103,7 +113,18 @@ func (l *QYGL8271Logic) QYGL8271(req *types.Request) (resp string, err *errs.App
|
||||
return "", callAPIErr
|
||||
}
|
||||
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
||||
var resultResp []byte
|
||||
if req.Options.Json {
|
||||
var parseErr *errs.AppError
|
||||
resultResp, parseErr = common.ParseJsonResponse(westResp)
|
||||
if parseErr != nil {
|
||||
return "", parseErr
|
||||
}
|
||||
} else {
|
||||
resultResp = westResp
|
||||
}
|
||||
|
||||
encryptData, aesEncrypt := crypto.AesEncrypt(resultResp, key)
|
||||
if aesEncrypt != nil {
|
||||
return "", errs.ErrSystem
|
||||
}
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
|
||||
package types
|
||||
|
||||
type Request struct {
|
||||
Data string `json:"data"`
|
||||
type Options struct {
|
||||
Json bool `json:"json,optional"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Data string `json:"data"`
|
||||
Options Options `json:"options,optional"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user