/// <summary>
/// 数据发送工具类(Post)
/// </summary>
public class PostHelper
{
/// <summary>
/// 键值对生成json请求参数
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ToPostJson(Dictionary<string, string> source)
{
string res = "";
var buff = new StringBuilder(string.Empty);
if (source == null)
throw new ArgumentNullException("source", "Unable to convert object to a dictionary. The source object is null.");
buff.Append("{");
foreach (var key in source.Keys)
{
string value = source[key];
if (value != null)
{
buff.Append("\"" + key + "\"" + ":" + "\"" + value + "\"" + ",");
}
}
res = buff.ToString();
//buff.ToString().Trim(',');
//buff.Append("}");
res = res.Trim(',');
res += "}";
return res;
}
/// <summary>
/// 请求方法
/// </summary>
/// <param name="url"></param>
/// <param name="postdata"></param>
/// <returns></returns>
public static string PostUrl(string url, string postdata)
{
string result = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";//请求方式
req.ContentType = "application/json";//请求参数的格式
req.Timeout = 1000 * 10 * 60;//请求超时时间
byte[] data = Encoding.UTF8.GetBytes(postdata);
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))//获取响应内容
{
result = reader.ReadToEnd();
}
}
catch (Exception e)
{
}
return result;
}
/// <summary>
/// 请求方法
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="postdata">POST请求的数据</param>
/// <param name="customHeaderName">自定义请求头的名称</param>
/// <param name="customHeaderValue">自定义请求头的值</param>
/// <returns>响应的字符串</returns>
public static string PostUrl(string url, string postdata, string customHeaderName, string customHeaderValue)
{
string result = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST"; // 请求方式
req.ContentType = "application/json"; // 请求参数的格式
req.Timeout = 1000 * 10 * 60; // 请求超时时间
// 添加自定义请求头
req.Headers[customHeaderName] = customHeaderValue;
byte[] data = Encoding.UTF8.GetBytes(postdata);
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) // 获取响应内容
{
result = reader.ReadToEnd();
}
}
catch (Exception e)
{
// 这里可以添加异常处理逻辑,比如记录日志
// Console.WriteLine(e.ToString());
}
return result;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
[HotUpdate]
[Description("【执行计划】 钉钉付款申请单同步")]
public class KingdeeTBDD : AbstractBillPlugIn
{
public class TokenResponse
{
public int? ExpireIn { get; set; }
public string AccessToken { get; set; }
}
public class InstanceIdsResponse
{
public bool Success { get; set; }
public Result Resul { get; set; }
// 内部类来存储result对象的属性
public class Result
{
public int? NextToken { get; set; }
public List<string> List { get; set; }
}
}
private string accessToken;
private List<string> instanceIds;
public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
{
base.AfterBarItemClick(e);
if (e.BarItemKey.EqualsIgnoreCase("F_YDL_DDTB"))
{
Dictionary<string, string> kv = new Dictionary<string, string>();
kv.Add("appKey", "");
kv.Add("appSecret", "");
var TOKEN = PostHelper.PostUrl("https://api.dingtalk.com/v1.0/oauth2/accessToken", PostHelper.ToPostJson(kv));
TokenResponse root = JsonConvert.DeserializeObject<TokenResponse>(TOKEN);
// 检查expireIn是否存在且为7200
if (root?.ExpireIn.HasValue == true && root.ExpireIn.Value == 7200)
{
// 如果条件满足,获取并使用accessToken
accessToken = root.AccessToken;
}
else
{
Logger.Info("API接口提示:TOKEN返回错误请检查APPKEY和APPSERCRET的数据!", TOKEN);
return;
}
if (!accessToken.IsNullOrEmptyOrWhiteSpace())
{
// 获取当前UTC时间
DateTime utcNow = DateTime.UtcNow;
var endTime = (utcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
DateTime utcTwoHoursAgo = utcNow.AddHours(-60);
var startTime = (utcTwoHoursAgo - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
Dictionary<string, object> kk = new Dictionary<string, object>();
kk.Add("startTime", startTime);
kk.Add("endTime", endTime);
kk.Add("processCode", "PROC-63643A59-18F0-4FBA-B9E2-C7E2FBDE3C7A");
kk.Add("nextToken", 0);
kk.Add("maxResults", 20);
kk.Add("statuses", new List<string> { "COMPLETED" });
string json = JsonConvert.SerializeObject(kk);
var SLID = PostHelper.PostUrl("https://api.dingtalk.com/v1.0/workflow/processes/instanceIds/query", json, "x-acs-dingtalk-access-token", accessToken);
InstanceIdsResponse response = JsonConvert.DeserializeObject<InstanceIdsResponse>(SLID);
// 假设 response 已经被正确反序列化为 InstanceIdsResponse 类型的对象
if (response?.Success == true) // 或者简单地写作 if (response?.Success ?? false)
{
// 如果 success 为 true,则访问 result.list
if (response.Resul?.List != null)
{
foreach (var instanceId in response.Resul.List)
{
// 将 instanceId 添加到新数组中
instanceIds.Add(instanceId);
}
}
else
{
// 如果 result.List 为 null,则记录日志(可选)
Logger.Info("API接口返回的实例ID列表为空!", SLID);
}
}
else
{
// 如果 success 不为 true,则记录日志
Logger.Info("API接口提示:获取审批实例ID列表异常!", SLID);
return;
}
}
}
}
}
推荐阅读