• C# 钉钉审批流使用


    1.了解钉钉审批流API接口的使用

    钉钉审批流API
    安装 RestSharp 包

    2.建立请求和接受的Dto

          /// <summary>
        /// 请求 审批流实例ID
        /// </summary>
        public class RequestProcessInstance
        {
            public string process_code { get; set; }
            public string start_time { get; set; }
            public string end_time { get; set; }
            public string size { get; set; }
            public string cursor { get; set; }
            public string userid_list { get; set; }
        }
    
        /// <summary>
        /// 返回  审批流实例ID
        /// </summary>
        public class ResponseProcessInstance
        {
            /// <summary>
            /// 返回码
            /// </summary>
            public string errcode { get; set; }
            public string request_id { get; set; }
            public ResponseProcessInstanceResult result { get; set; }
    
        }
        public class ResponseProcessInstanceResult
        {
            /// <summary>
            /// 审批实例id列表
            /// </summary>
            public List<string> list { get; set; }
            /// <summary>
            /// 表示下次查询的游标,当返回结果没有该字段时表示没有更多数据了
            /// </summary>
            public string next_cursor { get; set; }
    
        }
    
    
        /// <summary>
        /// 返回 审批流实例ID详情
        /// </summary>
        public class ResponseProcessInstanceDetails
        {
            /// <summary>
            /// 状态码
            /// </summary>
            public string errcode { get; set; }
            /// <summary>
            /// 实例详情
            /// </summary>
            public Process_instance process_instance { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string request_id { get; set; }
        }
        public class Form_component_values
        {
            /// <summary>
            /// 
            /// </summary>
            public string component_type { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string id { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string name { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string value { get; set; }
        }
        public class Process_instance
        {
            /// <summary>
            /// 开始时间
            /// </summary>
            public DateTime create_time { get; set; }
            /// <summary>
            /// 结束时间
            /// </summary>
            public DateTime finish_time { get; set; }
            /// <summary>
            /// 表单详情列表
            /// </summary>
            public List<Form_component_values> form_component_values { get; set; }
    
            /// <summary>
            /// 发起部门
            /// </summary>
            public string originator_dept_id { get; set; }
            /// <summary>
            /// 发起部门名称
            /// </summary>
            public string originator_dept_name { get; set; }
            /// <summary>
            /// 发起人
            /// </summary>
            public string originator_userid { get; set; }
            /// <summary>
            /// 审批结果,分为 agree 和 refuse
            /// </summary>
            public string result { get; set; }
            /// <summary>
            /// 审批状态,分为NEW(新创建)RUNNING(运行中)TERMINATED(被终止)COMPLETED(完成)
            /// </summary>
            public string status { get; set; }
            /// <summary>
            /// 审批实例标题
            /// </summary>
            public string title { get; set; }
        }
    
    

    3.创建调用钉钉审批流API的方法

    需要获取钉钉的ACCESSTOken,看官网即可

            /// <summary>
            /// 根据审批流获取审批流实例List
            /// </summary>
            /// <param name="dto"></param>
            /// <returns></returns>
            public static List<string>  GetApproveProcessInstance(RequestProcessInstance dto)
            {
                List<string> result = new List<string>();
                var token = GetDingDingAccesToken(); //获取钉钉的ACCESSTOken
                var tempDto = GetResponseProcessInstance(dto, token);
                tempDto.result.list.ForEach(a=>result.Add(a));
    
                //因为一次性最多返回10条,所以还需要再次请求其余的数据
                while (tempDto.result.next_cursor!=null)
                {
                    dto.cursor = tempDto.result.next_cursor;
                    tempDto = GetResponseProcessInstance(dto, token);
                    tempDto.result.list.ForEach(a => result.Add(a));
                    if (tempDto.result.next_cursor == null)
                    {
                        break;
                    }
                }
                return result;
            }
    
            /// <summary>
            /// 批量获取审批流实例ID   参考文档:https://ding-doc.dingtalk.com/doc#/serverapi2/hh8lx5
            /// </summary>
            /// <param name="dto"></param>
            /// <param name="token"></param>
            /// <returns></returns>
            private static ResponseProcessInstance GetResponseProcessInstance(RequestProcessInstance dto,string token)
            {
                //发起钉钉请求
                var client = new RestClient("https://oapi.dingtalk.com/topapi/processinstance/listids?access_token=" + token);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                string json = JsonConvert.SerializeObject(dto);
                request.AddParameter("application/json", json, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                var tempDto = JsonConvert.DeserializeObject<ResponseProcessInstance>(response.Content);
                return tempDto;
            }
    
    
            /// <summary>
            /// 获取钉钉的AccessToken
            /// </summary>
            /// <returns></returns>
            private static string GetDingDingAccesToken()
            {
                string token ="";//看官方文档
                return token;
            }
    
            /// <summary>
            /// 根据审批实例id调用此接口获取审批实例详情,包括审批表单信息、操作记录列表、操作人、抄送人、审批任务列表等。
            /// </summary>
            /// <param name="process_instance_ids"></param>
            /// <returns></returns>
            public static List<ResponseProcessInstanceDetails> GetResponseProcessInstanceDetails(List<string> process_instance_ids)
            {
                List<ResponseProcessInstanceDetails> result = new List<ResponseProcessInstanceDetails>();
                string token = GetDingDingAccesToken();
                process_instance_ids.ForEach(a => { result.Add(GetResponseProcessInstanceDetails(a,token)); });
                return result;
            }
    
    
            /// <summary>
            /// 钉钉 根据用户ID 获取用户信息详情,可用Jobject转化
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public static string GetUserByDingDingID(string id)
            {
                var client = new RestClient("https://oapi.dingtalk.com/user/get?access_token="+GetDingDingAccesToken()+"&userid="+id+"");
                client.Timeout = -1;
                var request = new RestRequest(Method.GET);
                IRestResponse response = client.Execute(request);
                return (response.Content);
            }
    
            /// <summary>
            /// 时间转换 毫秒级别的时间戳
            /// </summary>
            /// <param name="dateTime"></param>
            /// <returns></returns>
            public static string GetChinaTicks(DateTime dateTime)
            {
                //北京时间相差8小时,TimeZoneInfo.Local本地不太有用
                DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1,8, 0, 0, 0), TimeZoneInfo.Local);
                long t = (dateTime.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位   
                return t.ToString();
            }
    

    4.使用例子

     public static List<ResponseProcessInstanceDetails> GetUseCarList(DateTime Date)
            {
                var dto = new RequestProcessInstance();//请求 获取审批流ID对应的所有进程
                dto.process_code = "asdasd";//钉钉审批的process_code
                dto.start_time = GetChinaTicks(Date);//转换成时间戳
                dto.end_time = GetChinaTicks(Date.AddMonths(1).AddSeconds(-1));
                var listApproveProcessInstance = GetApproveProcessInstance(dto);//获取到所有的审批流进程
                List<ResponseProcessInstanceDetails> listDetails = GetResponseProcessInstanceDetails(listApproveProcessInstance);//根据所有的进程获取到所有进程的详情
                return listDetails;
            }
    
  • 相关阅读:
    面向对象基础
    JS操作属性和样式
    表单验证
    form标签
    Dreamweaver网页设计代码大全
    最差项目展示
    CSS样式表
    while循环 do while循环 switch
    for循环
    穷举法
  • 原文地址:https://www.cnblogs.com/Alex-Mercer/p/12892968.html
Copyright © 2020-2023  润新知