钉钉开放平台API对接第三讲

钉钉开放平台-服务端API-发起审批实例
官方API: https://ding-doc.dingtalk.com/doc#/serverapi2/cmct1a

需要做的事项:
1、进入钉钉后台管理-工作台-审批-创建新审批-自定义模板
2、按界面拖控件定义好您需要的模板

API调用方式:指定审批人模式。
实现代码:

/// 
/// 智能工作流-发起审批实例
/// 
/// 
public async Task CreateProcessInstanceAsync(CreateProcessInstanceRequestInfo requestInfo)
{
    await StaffsCheckedAsync();

    requestInfo.TaskActionType = requestInfo.TaskActionType ?? "AND";
    requestInfo.CCPosition = requestInfo.CCPosition ?? "START";

    var originatorStaffInfo = _staffs.FirstOrDefault(m => m.StaffNumber.Equals(requestInfo.OriginatorStaffNumber));
    if (originatorStaffInfo == null) return null;

    var approverUserids = new List();
    foreach(var staffNumber in requestInfo.Approvers)
    {
        var staffInfo = _staffs.FirstOrDefault(m => m.StaffNumber.Equals(staffNumber, StringComparison.OrdinalIgnoreCase));
        if (staffInfo != null) approverUserids.Add(staffInfo.Id);
    }

    var copyUserids = _staffs.Where(m => requestInfo.CopyPersons.Contains(m.StaffNumber)).Select(m => m.Id);
    var cclist = (copyUserids == null || !copyUserids.Any()) ? null : string.Join(",", copyUserids);
    var ccposition = !string.IsNullOrEmpty(cclist) ? requestInfo.CCPosition : null;

    List approvers = null;

    if (requestInfo.TaskActionType.Equals("OR", StringComparison.OrdinalIgnoreCase) && (approverUserids != null && approverUserids.Count() > 1))
    {
        approvers = new List
        {
            new OapiProcessInstanceApproverVo { ApproverUserIds = approverUserids,TaskActionType = requestInfo.TaskActionType }
        };
    }
    
    var request = new OapiProcessinstanceCreateRequest
    {
        AgentId = _config.AgentId,
        ProcessCode = requestInfo.ProcessCode,
        OriginatorUserId = originatorStaffInfo.Id,
        DeptId = long.Parse(originatorStaffInfo.DepmtId),
        FormComponentValues = requestInfo.FormComponentValues,
        ApproverUserIds = string.Join(",", approverUserids),
        CopyUserIds = cclist,
        CCPosition = ccposition,
        Approvers = approvers
    };

    var httpContent = new StringContent(JsonConvert.SerializeObject(request));

    await AccessTokenCheckedAsync();

    var response = await _httpClient.PostAsync(string.Format(CreateProcessInstanceUrl, _accessToken), httpContent);
    if (!response.IsSuccessStatusCode)
    {
        _logger.LogInformation($"DingtalkClient.CreateProcessInstanceAsync,http response code:{(int)response.StatusCode}");

        return null;
    }

    return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
}

/// 
/// 智能工作流-获取审批实例详情
/// 
/// 
/// 
public async Task GetProcessInstanceAsync(string processInstanceId)
{
    await AccessTokenCheckedAsync();

    var response = await _httpClient.GetAsync(string.Format(ProcessInstanceUrl, _accessToken, processInstanceId));
    if (!response.IsSuccessStatusCode)
    {
        _logger.LogInformation($"DingtalkClient.GetProcessInstanceAsync,http response code:{(int)response.StatusCode}");

        return null;
    }

    return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
}

//发起审批实例
private const string CreateProcessInstanceUrl = "https://oapi.dingtalk.com/topapi/processinstance/create?access_token={0}";

public class OapiCreateProcessInstanceResult : OapiResponseBase
{
    [JsonProperty("process_instance_id")]
    public string ProcessInstanceId { get; set; }
}

public class OapiProcessInstanceResult : OapiResponseBase
{
    [JsonProperty("process_instance")]
    public OapiProcessInstanceVo ProcessInstance { get; set; }
}

public class OapiProcessInstanceVo
{
    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("create_time")]
    public DateTime CreateTime { get; set; }

    [JsonProperty("finish_time")]
    public DateTime FinishTime { get; set; }

    /// 
    /// 发起人
    /// 
    [JsonProperty("originator_userid")]
    public string OriginatorUserId { get; set; }

    /// 
    /// 发起部门
    /// 
    [JsonProperty("originator_dept_id")]
    public string OriginatorDeptId { get; set; }

    /// 
    /// 审批状态
    /// 
    [JsonProperty("status")]
    public string Status { get; set; }

    /// 
    /// 抄送人
    /// 
    [JsonProperty("cc_userids")]
    public string CcUserids { get; set; }

    /// 
    /// 表单详情列表
    /// 
    [JsonProperty("form_component_values")]
    public IList FormComponentValues { get; set; }

    /// 
    /// 审批结果,分为 agree 和 refuse
    /// 
    [JsonProperty("result")]
    public string ApprovalResult { get; set; }

    /// 
    /// 审批实例业务编号
    /// 
    [JsonProperty("business_id")]
    public string BusinessId { get; set; }

    /// 
    /// 发起部门
    /// 
    [JsonProperty("originator_dept_name")]
    public string OriginatorDeptName { get; set; }

    /// 
    /// 审批实例业务动作
    /// 
    [JsonProperty("biz_action")]
    public string BizAction { get; set; }

    /// 
    /// 审批附属实例列表
    /// 
    [JsonProperty("attached_process_instance_ids")]
    public string[] AttachedProcessInstanceIds { get; set; }

    /// 
    /// 操作记录列表
    /// 
    [JsonProperty("operation_records")]
    public IEnumerable OperationRecords { get; set; }

    /// 
    /// 已审批任务列表
    /// 
    [JsonProperty("tasks")]
    public IEnumerable Tasks { get; set; }
}

public class FormComponentValueVo
{
    /// 
    /// (必须)请假类型
    /// 
    [JsonProperty("name")]
    public string Name { get; set; }

    /// 
    /// (必须)事假
    /// 
    [JsonProperty("value")]
    public string Value { get; set; }

    /// 
    /// 可选
    /// 
    [JsonProperty("ext_value")]
    public string ExtValue { get; set; }
}

/// 
/// 审批实例操作记录
/// 
public class OapiProcessInstanceOperationalRecordsVo
{
    [JsonProperty("userid")]
    public string UserId { get; set; }

    [JsonProperty("date")]
    public DateTime Date { get; set; }

    [JsonProperty("operation_type")]
    public string OperationType { get; set; }

    [JsonProperty("operation_result")]
    public string OperationResult { get; set; }

    [JsonProperty("remark")]
    public string Remark { get; set; }
}

/// 
/// 已审批任务列表,可以通过此列表获取已审批人
/// 
public class OapiProcessInstanceTasksVo
{
    [JsonProperty("userid")]
    public string UserId { get; set; }

    [JsonProperty("task_status")]
    public string TaskStatus { get; set; }

    [JsonProperty("task_result")]
    public string TaskResult { get; set; }

    [JsonProperty("create_time")]
    public DateTime CreateTime { get; set; }

    [JsonProperty("finish_time")]
    public DateTime FinishTime { get; set; }

    [JsonProperty("taskid")]
    public string TaskId { get; set; }
}

public class OapiResponseBase
{
    [JsonProperty("errcode")]
    public int ErrorCode { get; set; }

    [JsonProperty("errmsg")]
    public string ErrorMsg { get; set; }
}

你可能感兴趣的:(钉钉开放平台服务端API,.NET,CORE对接钉钉开放平台)