增加api optional Json
This commit is contained in:
parent
190bd0cb0f
commit
75ea50fff9
@ -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"`
|
||||
}
|
||||
|
6
go.mod
6
go.mod
@ -32,9 +32,12 @@ require (
|
||||
github.com/alibabacloud-go/tea-xml v1.1.3 // indirect
|
||||
github.com/aliyun/credentials-go v1.3.1 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bytedance/sonic v1.13.2 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.5.5 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
github.com/coreos/go-semver v0.3.1 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
@ -64,6 +67,7 @@ require (
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.17.9 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
@ -88,6 +92,7 @@ require (
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/tjfoc/gmsm v1.3.2 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
|
||||
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
|
||||
@ -106,6 +111,7 @@ require (
|
||||
go.uber.org/automaxprocs v1.5.3 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
go.uber.org/zap v1.24.0 // indirect
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
|
||||
golang.org/x/crypto v0.26.0 // indirect
|
||||
golang.org/x/net v0.28.0 // indirect
|
||||
golang.org/x/oauth2 v0.21.0 // indirect
|
||||
|
17
go.sum
17
go.sum
@ -54,12 +54,20 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ=
|
||||
github.com/bytedance/sonic v1.13.2/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
|
||||
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/clbanning/mxj/v2 v2.5.5 h1:oT81vUeEiQQ/DcHbzSytRngP6Ky9O+L+0Bw0zSJag9E=
|
||||
github.com/clbanning/mxj/v2 v2.5.5/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
|
||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
|
||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
|
||||
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
|
||||
@ -162,6 +170,9 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
|
||||
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
@ -263,6 +274,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
@ -277,6 +289,8 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tjfoc/gmsm v1.3.2 h1:7JVkAn5bvUJ7HtU08iW6UiD+UTmJTIToHCfeFzkcCxM=
|
||||
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
@ -334,6 +348,8 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
|
||||
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
@ -488,6 +504,7 @@ k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt
|
||||
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
|
||||
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
|
||||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
|
||||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
|
||||
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
|
||||
|
Loading…
Reference in New Issue
Block a user