c# 钉钉获取OA审批数据,谁有示例 ,比如获取请假,签卡,加班等
以下是一个使用 C# 获取钉钉 OA 审批数据的示例:
csharp
Copy code
using System;
using System.Collections.Generic;
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
namespace DingTalkOASample
{
class Program
{
static void Main(string[] args)
{
// 钉钉开放平台应用信息
string appKey = "YourAppKey";
string appSecret = "YourAppSecret";
// 获取 access_token
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.Appkey = appKey;
request.Appsecret = appSecret;
request.SetHttpMethod("GET");
OapiGettokenResponse response = client.Execute(request);
string accessToken = response.AccessToken;
// 获取待我审批的审批实例列表
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/list");
OapiProcessinstanceListRequest listRequest = new OapiProcessinstanceListRequest();
listRequest.ProcessCode = "YourProcessCode"; // 替换为实际的流程模板Code
listRequest.StartTime = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss");
listRequest.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
OapiProcessinstanceListResponse listResponse = client.Execute(listRequest, accessToken);
if (listResponse.Errcode == 0)
{
List<OapiProcessinstanceListResponse.ProcessInstanceTopVoDomain> processInstances = listResponse.Result.List;
foreach (var instance in processInstances)
{
// 获取单个审批实例的详情
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
OapiProcessinstanceGetRequest getRequest = new OapiProcessinstanceGetRequest();
getRequest.ProcessInstanceId = instance.ProcessInstanceId;
OapiProcessinstanceGetResponse getResponse = client.Execute(getRequest, accessToken);
if (getResponse.Errcode == 0)
{
// 处理审批实例详情数据
// 请假审批
if (instance.FormComponentValues != null && instance.FormComponentValues.Length > 0)
{
foreach (var componentValue in instance.FormComponentValues)
{
if (componentValue.Name == "请假类型")
{
string leaveType = componentValue.Value;
Console.WriteLine("请假类型: " + leaveType);
}
else if (componentValue.Name == "请假时间")
{
string leaveTime = componentValue.Value;
Console.WriteLine("请假时间: " + leaveTime);
}
// 其他请假审批字段...
}
}
// 签卡审批
// TODO: 添加签卡审批字段处理
// 加班审批
// TODO: 添加加班审批字段处理
Console.WriteLine("==============");
}
else
{
Console.WriteLine("Error: " + getResponse.Errmsg);
}
}
}
else
{
Console.WriteLine("Error: " + listResponse.Errmsg);
}
Console.ReadLine();
}
}
}
在上述代码中,你需要替换以下部分:
YourAppKey:替换为你的钉钉开放平台应用的 App Key。
YourAppSecret:替换为你的钉钉开放平台应用的 App Secret。
YourProcessCode:替换为你要获取审批数据的流程模板 Code。可以通过钉钉开放平台的流程模板管理获取相应的模板 Code。
请注意,以上示例仅演示了如何获取审批实例的基本信息,具体字段的处理和展示需要根据你的实际需求进行修改。你可以根据实际的请假、签卡、加班等审批字段进行解析和处理。