huawei
9 天以前 cac3a5a9ea6a99a45cf3763779f9b699b953afb5
WebApi/Gs.Toolbox/InterfaceUtil.cs
@@ -1,8 +1,9 @@
using System.Net.Cache;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Dynamic;
using System.Net;
using System.Net.Cache;
using System.Text;
namespace Gs.Toolbox;
@@ -27,7 +28,29 @@
        string strLogGuid = Guid.NewGuid().ToString();
        string url = AppSettingsHelper.getValueByKey("TestErpUrl");
        if (urlType == 2)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl2");
        }
        else if (urlType == 3)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl3");
        }
        else if (urlType == 4)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl4");
        }
        else if (urlType == 5)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl5");
        }
        else if (urlType == 6)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl6");
        }
        else if (urlType == 101)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl101");
        }
        HttpWebRequest request = null;
        StreamWriter requestStream = null;
        WebResponse response = null;
@@ -38,7 +61,7 @@
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Accept = "*/*";
            request.Timeout = 150000;
            //request.Timeout = 150000;
            request.AllowAutoRedirect = false;
            request.ServicePoint.Expect100Continue = false;
            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
@@ -49,11 +72,11 @@
            response = request.GetResponse();
            if (response != null)
            {
                var reader = new StreamReader(response.GetResponseStream(),
                    Encoding.UTF8);
                var reader = new StreamReader(response.GetResponseStream(),Encoding.UTF8);
                responseStr = reader.ReadToEnd();
                reader.Close();
            }
           // LogHelper.Debug(url, "HttpPostErp response:" + param + ",response:" + responseStr.ToString());
        }
        catch (Exception ex)
        {
@@ -67,11 +90,39 @@
            requestStream = null;
            response = null;
        }
        //{"msg":"操作成功","code":200,"data":{"number":"CGSL168397","code":"200","id":"268781"}}
        if (_rtn != -1)
        {
            Result _result = JsonConvert.DeserializeObject<Result>(responseStr);
            if ("200".Equals(_result.state))
                _rtn = 1;
            try
            {
                Result _result = JsonConvert.DeserializeObject<Result>(responseStr);
                // 【优先判断】data.code(ERP真正的处理结果)
                if (_result?.data != null && "200".Equals(_result.data.code))
                {
                    _rtn = 1;
                    LogHelper.Debug(url, $"ERP处理成功,data.code=200, number={_result.data.number}, id={_result.data.id}");
                }
                // 【降级判断】如果data为空,则判断外层code(兼容旧接口)
               /* else if (_result != null && "200".Equals(_result.code))
                {
                    _rtn = 1;
                    LogHelper.Debug(url, $"ERP外层返回成功,code=200(无data层,可能是旧接口)");
                }*/
                else
                {
                    _rtn = -1;
                    string errorMsg = _result?.data?.code != null
                        ? $"ERP处理失败,data.code={_result.data.code}"
                        : $"ERP处理失败,外层code={_result?.code}";
                    LogHelper.Debug(url, errorMsg + $", msg={_result?.msg}, responseStr={responseStr}");
                }
            }
            catch (Exception ex)
            {
                _rtn = -1;
                LogHelper.Debug(url, $"ERP返回数据解析失败:{ex.Message}, responseStr={responseStr}");
            }
        }
        sbLog.Append("," + DateTime.Now.ToString() + "结束发送");
        if (_rtn > 0)
@@ -102,16 +153,38 @@
        return (_rtn, (_rtn > 0 ? strLogGuid : responseStr));
    }
}
//{"msg":"操作成功","code":200,"data":{"number":"CGSL168397","code":"200","id":"268781"}}
public class Result
{
    /// <summary>
    /// 200成功,否则失败
    /// 200成功,否则失败(外层code)
    /// </summary>
    public string? state { get; set; }
    public string? msg { get; set; }
    public string? code { get; set; }
    /// <summary>
    /// data层,包含真正的ERP处理结果
    /// </summary>
    public ErpData? data { get; set; }
}
    public string? status { get; set; }
    public string? message { get; set; }
/// <summary>
/// ERP返回的data层数据结构
/// </summary>
public class ErpData
{
    /// <summary>
    /// ERP单据编号
    /// </summary>
    public string? number { get; set; }
    /// <summary>
    /// ERP处理结果代码("200"成功)
    /// </summary>
    public string? code { get; set; }
    /// <summary>
    /// ERP单据ID
    /// </summary>
    public string? id { get; set; }
}