using HandleUtils;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Log;
using Kingdee.BOS.TCP;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EastChanger
{
public class BaseService
{
protected readonly Context _context;
protected readonly string _apiName;
protected readonly string _apiVersion;
protected readonly string _apiUrl;
protected readonly string _appKey;
protected readonly string _appKeySecret;
protected readonly string _moduleCnName;
public BaseService(Context context, string apiName, string moduleCnName = "海关信息同步")
{
_context = context;
var sql = "SELECT * FROM V_CUSTOMS_API_REQUEST_PARAMETE ";
var data = DBUtils.ExecuteDynamicObject(context, $"/*dialect*/{sql}");
var info = data[0];
_apiUrl = info["appUrl"].ToString();
_appKey = info["appKey"].ToString();
_appKeySecret = info["appKeySecret"].ToString();
_apiName = apiName;
_apiVersion = "";
_moduleCnName = moduleCnName;
}
///
/// 执行数据加密后发送请求
///
///
///
public string Execute(string dataJson)
{
//对json字符串进行url编码
var requestData = EncryptHelper.UrlEncode(dataJson);
var request = new Dictionary();
request.Add("app_key", _appKey);
request.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
request.Add("name", _apiName); //接口名称
request.Add("data", requestData);
request.Add("version", _apiVersion);
string sign = SignUtil.BuildSign(request, _appKeySecret);
request.Add("sign", sign);
string bodyData = JsonUtil.Serialize(request);
string result = WebHelper.DoPost(_apiUrl, bodyData);
// 转换为字符串
var timestampStr = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
sfj4Class.SaveTxt(dataJson, $"D:/sjf4Class/{timestampStr}.txt", result);
return result;
}
///
/// 处理接口回调信息
///
///
///
///
///
public void ExecuteOperateResult(IOperationResult opResult, string itemNo, string msg, bool IsSuccessStatus)
{
if (opResult != null)
{
opResult.OperateResult.Add(new OperateResult
{
Name = "同步编号:" + itemNo,
Message = msg,
SuccessStatus = IsSuccessStatus
});
}
else
{
if (IsSuccessStatus)
Logger.Info(_moduleCnName, $"同步编号:{itemNo}--{msg}");
else
Logger.Error(_moduleCnName, "请检查单据状态", new Exception(msg));
}
}
}
}