diff --git a/define.go b/define.go index 49c6f4d..f881c5b 100644 --- a/define.go +++ b/define.go @@ -79,4 +79,6 @@ const ( DefaultMessageField = "message" // DefaultDataField 默认数据字段 DefaultDataField = "data" + // SpecialFiledVal 特殊的 code / message / data配置 + SpecialFiledVal = "-" ) diff --git a/go.mod b/go.mod index 80de52a..e85e4b4 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,9 @@ require ( github.com/pelletier/go-toml/v2 v2.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect + github.com/tidwall/gjson v1.14.1 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect github.com/ugorji/go/codec v1.2.7 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.8.0 // indirect diff --git a/go.sum b/go.sum index f3abc32..8d1ec7d 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,12 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= +github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= diff --git a/rpc.go b/rpc.go index 505ba76..72f8c57 100644 --- a/rpc.go +++ b/rpc.go @@ -18,6 +18,8 @@ import ( "sync" "time" + "github.com/tidwall/gjson" + "git.zhangdeman.cn/zhangdeman/util" "github.com/ddliu/go-httpclient" @@ -218,14 +220,15 @@ func (r *request) Get() error { // Date : 14:24 2022/6/30 func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, parameter map[string]interface{}, receiver interface{}) error { var ( - serviceConfig *Service - apiConfig *Api - err error - fullURL string - client *httpclient.HttpClient - body []byte - response *httpclient.Response - responseBody []byte + serviceConfig *Service + apiConfig *Api + err error + fullURL string + client *httpclient.HttpClient + body []byte + response *httpclient.Response + responseBody []byte + code, message, data string ) if serviceConfig, apiConfig, err = r.GetServiceApi(serviceFlag, apiFlag); nil != err { return err @@ -237,21 +240,12 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par return err } - // 判断状态码 - isHttpSuccess := false - for _, successCode := range apiConfig.SuccessHttpCodeList { - if successCode == response.StatusCode { - isHttpSuccess = true - break - } - } - if !isHttpSuccess { - return fmt.Errorf("http响应状态码异常 -> %v", response.StatusCode) - } - if responseBody, err = io.ReadAll(response.Body); nil != err { return err } + + // 解析响应的业务数据 + code, message, data = r.getCodeAndMessageAndData(apiConfig, responseBody) return nil } @@ -317,3 +311,48 @@ func (r *request) getFullURLAndBody(serviceConfig *Service, apiConfig *Api, para } return strings.ReplaceAll(fullURL, "//", "/") + "?" + query, body } + +// validateResponseHttpCode 验证http状态码 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:18 2022/6/30 +func (r *request) validateResponseHttpCode(apiConfig *Api, response *httpclient.Response) error { + // 判断状态码 + isHttpSuccess := false + for _, successCode := range apiConfig.SuccessHttpCodeList { + if successCode == response.StatusCode { + isHttpSuccess = true + break + } + } + if !isHttpSuccess { + return fmt.Errorf("http响应状态码异常 -> %v", response.StatusCode) + } + return nil +} + +// getCodeAndMessageAndData 读取业务状态码 + 文案 + 数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:20 2022/6/30 +func (r *request) getCodeAndMessageAndData(apiConfig *Api, responseBody []byte) (string, string, string) { + var ( + code = SpecialFiledVal + message = SpecialFiledVal + data string + ) + if apiConfig.CodeField != SpecialFiledVal { + code = gjson.Get(string(responseBody), apiConfig.CodeField).String() + } + if apiConfig.MessageField != SpecialFiledVal { + message = gjson.Get(string(responseBody), apiConfig.MessageField).String() + } + if apiConfig.DataField == SpecialFiledVal { + data = string(responseBody) + } else { + data = gjson.Get(string(responseBody), apiConfig.DataField).String() + } + return code, message, data +}