加日志
This commit is contained in:
parent
773e3bdc43
commit
49fc422218
@ -127,6 +127,35 @@ type reportPreResponse struct {
|
|||||||
ResultDesc string `json:"resultDesc"` // 描述
|
ResultDesc string `json:"resultDesc"` // 描述
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON 自定义反序列化,处理 resultCode 可能是数字或字符串的情况
|
||||||
|
func (r *reportPreResponse) UnmarshalJSON(data []byte) error {
|
||||||
|
// 使用临时结构体来处理可能的不同类型
|
||||||
|
var temp struct {
|
||||||
|
ResultCode interface{} `json:"resultCode"`
|
||||||
|
ResultDesc string `json:"resultDesc"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &temp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 ResultCode,可能是数字或字符串
|
||||||
|
switch v := temp.ResultCode.(type) {
|
||||||
|
case float64:
|
||||||
|
// 如果是数字,转换为字符串
|
||||||
|
r.ResultCode = fmt.Sprintf("%.0f", v)
|
||||||
|
case int:
|
||||||
|
r.ResultCode = fmt.Sprintf("%d", v)
|
||||||
|
case string:
|
||||||
|
r.ResultCode = v
|
||||||
|
default:
|
||||||
|
r.ResultCode = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ResultDesc = temp.ResultDesc
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 获取商品库存返回数据
|
// 获取商品库存返回数据
|
||||||
type getProdStockResponse struct {
|
type getProdStockResponse struct {
|
||||||
ResultCode string `json:"resultCode"` // 操作编码
|
ResultCode string `json:"resultCode"` // 操作编码
|
||||||
@ -289,20 +318,25 @@ func NoticePreOrderCancel(orderNo string) (bool, error) {
|
|||||||
func (r ReportPreRequest) ReportPre() (bool, error) {
|
func (r ReportPreRequest) ReportPre() (bool, error) {
|
||||||
jsonData, err := json.Marshal(r)
|
jsonData, err := json.Marshal(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
utils.LogJsonErr("上报处方平台-序列化请求参数失败", err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.LogJsonInfo("上报处方平台:", jsonData)
|
|
||||||
|
|
||||||
// 准备请求体
|
// 准备请求体
|
||||||
requestBody := bytes.NewBuffer(jsonData)
|
requestBody := bytes.NewBuffer(jsonData)
|
||||||
|
|
||||||
// 设置请求 URL
|
// 设置请求 URL
|
||||||
url := config.C.Pre.PrePlatAppUrl + "v1/preOrder/receivePreOrder"
|
url := config.C.Pre.PrePlatAppUrl + "v1/preOrder/receivePreOrder"
|
||||||
|
|
||||||
|
// 打印请求信息
|
||||||
|
utils.LogJsonInfo("上报处方平台-请求URL", url)
|
||||||
|
utils.LogJsonInfo("上报处方平台-请求参数", r)
|
||||||
|
utils.LogJsonInfo("上报处方平台-请求体", requestBody.String())
|
||||||
|
|
||||||
// 创建 POST 请求
|
// 创建 POST 请求
|
||||||
req, err := http.NewRequest("POST", url, requestBody)
|
req, err := http.NewRequest("POST", url, requestBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
utils.LogJsonErr("上报处方平台-创建请求失败", err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,8 +345,10 @@ func (r ReportPreRequest) ReportPre() (bool, error) {
|
|||||||
|
|
||||||
token, _ := global.Redis.Get(context.Background(), "prescription_token").Result()
|
token, _ := global.Redis.Get(context.Background(), "prescription_token").Result()
|
||||||
if token == "" {
|
if token == "" {
|
||||||
|
utils.LogJsonInfo("上报处方平台-缓存中无token,重新获取", nil)
|
||||||
token, err = GetToken()
|
token, err = GetToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
utils.LogJsonErr("上报处方平台-获取token失败", err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -323,6 +359,7 @@ func (r ReportPreRequest) ReportPre() (bool, error) {
|
|||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
utils.LogJsonErr("上报处方平台-发送请求失败", err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,24 +367,43 @@ func (r ReportPreRequest) ReportPre() (bool, error) {
|
|||||||
_ = Body.Close()
|
_ = Body.Close()
|
||||||
}(resp.Body)
|
}(resp.Body)
|
||||||
|
|
||||||
// 检查响应状态码
|
// 读取响应体原始内容(用于日志)
|
||||||
if resp.StatusCode != 200 {
|
responseBodyBytes, err := io.ReadAll(resp.Body)
|
||||||
return false, errors.New("返回数据错误")
|
|
||||||
}
|
|
||||||
|
|
||||||
var response reportPreResponse
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
utils.LogJsonErr("上报处方平台-读取响应体失败", err)
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 打印响应状态码和原始响应内容
|
||||||
|
utils.LogJsonInfo("上报处方平台-响应状态码", fmt.Sprintf("statusCode: %d", resp.StatusCode))
|
||||||
|
utils.LogJsonInfo("上报处方平台-响应体原始内容", string(responseBodyBytes))
|
||||||
|
|
||||||
|
// 检查响应状态码
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
utils.LogJsonErr("上报处方平台-响应状态码错误", fmt.Sprintf("statusCode: %d, responseBody: %s", resp.StatusCode, string(responseBodyBytes)))
|
||||||
|
return false, errors.New("返回数据错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析响应数据
|
||||||
|
var response reportPreResponse
|
||||||
|
err = json.Unmarshal(responseBodyBytes, &response)
|
||||||
|
if err != nil {
|
||||||
|
utils.LogJsonErr("上报处方平台-解析响应数据失败", fmt.Sprintf("error: %v, responseBody: %s", err, string(responseBodyBytes)))
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印解析后的响应数据
|
||||||
|
utils.LogJsonInfo("上报处方平台-解析后的响应数据", response)
|
||||||
|
|
||||||
if response.ResultCode != "1000" {
|
if response.ResultCode != "1000" {
|
||||||
|
utils.LogJsonErr("上报处方平台-业务处理失败", response)
|
||||||
if response.ResultDesc != "" {
|
if response.ResultDesc != "" {
|
||||||
return false, errors.New(response.ResultDesc)
|
return false, errors.New(response.ResultDesc)
|
||||||
}
|
}
|
||||||
return false, errors.New("上报处方平台失败")
|
return false, errors.New("上报处方平台失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils.LogJsonInfo("上报处方平台-成功", response)
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user