You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

889 lines
45 KiB
C#

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HuizhongLibrary.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace HuizhongLibrary.Dingtalk
{
public class DingtalkService
{
#region 获取签名
public static String GetSignName(string AccessToken, out string ErrorMessage)
{
ErrorMessage="";
return "";
}
#endregion
#region 获取AccessToken
public static string DingtalkAccessToken(string Appid, string Secret, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/gettoken?corpid=" + Appid + "&corpsecret=" + Secret;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
UTF8Encoding encoding = new UTF8Encoding();
string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return "";
DingtalkAccessToken model2 = JsonConvert.Deserialize<DingtalkAccessToken>(XmlContent);
if (model2.errcode == 0)
{
return model2.access_token;
}
else
{
ErrorMessage = model2.errmsg;
}
return "";
}
#endregion
#region 获取AccessToken
public static string DingtalkAccessTokenNew(string Appid, string Secret, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/gettoken?appkey=" + Appid + "&appsecret=" + Secret;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
UTF8Encoding encoding = new UTF8Encoding();
string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return "";
DingtalkAccessToken model2 = JsonConvert.Deserialize<DingtalkAccessToken>(XmlContent);
if (model2.errcode == 0)
{
return model2.access_token;
}
else
{
ErrorMessage = model2.errmsg;
}
return "";
}
#endregion
#region 获取AccessToken
public static string DingtalkAccessTokenNewZZD(string Appid, string Secret, out string ErrorMessage,string ip, string mac)
{
//string url = "https://openplatform.dg-work.cn/gettoken.json?appkey=" + Appid + "&appsecret=" + Secret;
//JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//UTF8Encoding encoding = new UTF8Encoding();
//string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
//if (string.IsNullOrEmpty(ErrorMessage) == false) return "";
ErrorMessage="";
string url = "https://openplatform-pro.ding.zj.gov.cn/gettoken.json?appkey=" + Appid + "&appsecret=" + Secret;
string XmlContent = GetAccessTokenZZD(url, ip, mac, Appid, Secret);
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
DingtalkAccessTokenMessZZD model2 = JsonConvert.Deserialize<DingtalkAccessTokenMessZZD>(XmlContent);
if (model2.success == true)
{
if (model2.content.success == true)
{
return model2.content.data.accessToken;
}
else
{
ErrorMessage = XmlContent;
}
}
else
{
ErrorMessage = XmlContent;
}
return "";
}
//根据authCode换取用户信息
//参数 paramData"access_token=" + access_token + "&auth_code=" + auth_code + ""
public static string ZzdPostWebRequest(string postUrl, string paramData, string access_token, string auth_code, string ip, string mac, string Appid, string Secret)
{
try
{
string ret = string.Empty;
byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
var AccessKey = Appid;
var SecretKey = Secret;
var Timestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");
var Nonce = ConvertDateTimeToInt(DateTime.Now) + "1234";
var CanonicalURI = "/rpc/oauth2/dingtalk_app_user.json";
var HttpRequestParams = "access_token=" + access_token + "&auth_code=" + auth_code + "";
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/json";
webReq.ContentLength = byteArray.Length;
webReq.Headers.Add("X-Hmac-Auth-IP", ip);
webReq.Headers.Add("X-Hmac-Auth-MAC", mac);
webReq.Headers.Add("X-Hmac-Auth-Timestamp", Timestamp);
webReq.Headers.Add("X-Hmac-Auth-Version", "1.0");
webReq.Headers.Add("X-Hmac-Auth-Nonce", Nonce);
webReq.Headers.Add("apiKey", AccessKey);
var bytesToSign = "POST" + '\n' + Timestamp + '\n' + Nonce + '\n' + CanonicalURI + '\n' + HttpRequestParams;
webReq.Headers.Add("X-Hmac-Auth-Signature", GetSignature(bytesToSign, SecretKey));
System.IO.Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
return ret;
}
catch (WebException ex)
{
var res = (HttpWebResponse)ex.Response;
System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), Encoding.UTF8);
var ret = sr.ReadToEnd();
return ret;
}
}
///<summary>
///获取AccessToken
///</summary>
///<paramname="url"></param>
///<returns></returns>
public static string GetAccessTokenZZD(string url, string ip, string mac, string Appid, string Secret)
{
try
{
var AccessKey = Appid;
var SecretKey = Secret;
var xmlResponse = "";
var Timestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");
var Nonce = ConvertDateTimeToInt(DateTime.Now) + "1234";
var CanonicalURI = "/gettoken.json";
var HttpRequestParams = "appkey=" + AccessKey + "&appsecret=" + SecretKey + "";
//发起请求
var webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
//webRequest.Headers.Add("X-Hmac-Auth-IP",ip);
//webRequest.Headers.Add("X-Hmac-Auth-MAC",mac);
webRequest.Headers.Add("X-Hmac-Auth-Timestamp", Timestamp);
webRequest.Headers.Add("X-Hmac-Auth-Version", "1.0");
webRequest.Headers.Add("X-Hmac-Auth-Nonce", Nonce);
webRequest.Headers.Add("apiKey", AccessKey);
var bytesToSign = "GET" + '\n' + Timestamp + '\n' + Nonce + '\n' + CanonicalURI +'\n' + HttpRequestParams;
webRequest.Headers.Add("X-Hmac-Auth-Signature", GetSignature(bytesToSign,SecretKey));
webRequest.ContentType = "application/json";
webRequest.Method = "GET";
//响应
WebResponse webResponse = webRequest.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream(),
Encoding.UTF8);
xmlResponse = sr.ReadToEnd();
return xmlResponse;
}
catch (WebException ex)
{
var res = (HttpWebResponse)ex.Response;
System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), Encoding.UTF8);
var ret = sr.ReadToEnd();
return ret;
}
}
/// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        private static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
}
/// <summary>
/// 获取签名
/// </summary>
/// <param name="message">将bytesToSign作为消息</param>
/// <param name="secret">将SecretKey作为秘钥</param>
/// <returns></returns>
private static string GetSignature(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new System.Security.Cryptography.HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
}
// private T DingGovRequest<T>(HttpMethod method, string canonicalURI, Dictionary<string, string> requestParas)
// {
// var domain = ModuleConfig.DingGovAPIDomain;
// var accessKey = ModuleConfig.DingGovAppKey;
// var secretKey = ModuleConfig.DingGovAppSecret;
// var timestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");
// var nonce = ConvertDateTimeToInt(DateTime.Now) + "1234";
// var requestUrl = domain + canonicalURI;
// try
// {
// var bytesToSign = $"{method}\n{timestamp}\n{nonce}\n{canonicalURI}";//参数参与签名
// bytesToSign += '\n' + DicionartyToUrlParameters(requestParas);
// var message = new HttpRequestMessage
// {
// Method = method,
// };
// message.Headers.Add("X-Hmac-Auth-IP", "1.1.1.1");
// message.Headers.Add("X-Hmac-Auth-MAC", "MAC");
// message.Headers.Add("X-Hmac-Auth-Timestamp", timestamp);
// message.Headers.Add("X-Hmac-Auth-Version", "1.0");
// message.Headers.Add("X-Hmac-Auth-Nonce", nonce.ToString());
// message.Headers.Add("apiKey", accessKey);
// message.Headers.Add("X-Hmac-Auth-Signature", GetSignature(bytesToSign, secretKey));
////Post时放入body
// if (method == HttpMethod.Post)
// {
// var paras = new Dictionary<string, string>();
// foreach (var dic in requestParas)
// {
// paras.Add(dic.Key, Uri.UnescapeDataString(dic.Value));
// }
// message.Content = new FormUrlEncodedContent(paras);
// message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
// }
////Get时放入urlParameters
// else if (method == HttpMethod.Get)
// {
// requestUrl += $"?{DicionartyToUrlParameters(requestParas)}";
// }
// message.RequestUri = new Uri(requestUrl);
// using (var client = new HttpClient())
// {
// var request = client.SendAsync(message);
// request.Wait();
// var reponseContent = request.Result.Content.ReadAsStringAsync();
// reponseContent.Wait();
// //记录日志
// if (!request.Result.IsSuccessStatusCode)
// {
// throw new Exception(reponseContent.Result);
// }
// return JsonConvert.DeserializeObject<T>(reponseContent.Result);
// }
// }
// catch (Exception ex)
// {
////记录日志
// return (T)default;
// }
// }
#endregion
#region 根据code获取用户信息
public static DingtalkUserInfo GetDingtalkUserInfo(string AccessToken, string code, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/user/getuserinfo?access_token=" + AccessToken + "&code=" + code;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
UTF8Encoding encoding = new UTF8Encoding();
string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
DingtalkUser model2 = JsonConvert.Deserialize<DingtalkUser>(XmlContent);
if (model2.errcode != 0)
{
ErrorMessage = XmlContent;
return null;
}
ErrorFollow.TraceWrite("获取钉钉用户ID", "", model2.ToJsonString());
url = "https://oapi.dingtalk.com/user/get?access_token=" + AccessToken + "&userid=" + model2.userid;
XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
DingtalkUserInfo model3 = JsonConvert.Deserialize<DingtalkUserInfo>(XmlContent);
if (model3.errcode != 0)
{
ErrorMessage = XmlContent;
}
return model3;
}
#endregion
#region 根据code获取用户信息
public static DingtalkUserInfoZZD GetDingtalkUserInfoZZD(string AccessToken, string code, out string ErrorMessage, string ip, string mac,string Appid, string Secret)
{
//string url = "https://openplatform.dg-work.cn/rpc/oauth2/dingtalk_app_user.json?access_token=" + AccessToken + "&auth_code=" + code;
//JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//UTF8Encoding encoding = new UTF8Encoding();
//string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
//if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
ErrorMessage = "";
string url = "https://openplatform-pro.ding.zj.gov.cn/rpc/oauth2/dingtalk_app_user.json";
string paramData = "access_token=" + AccessToken + "&auth_code=" + code + "";
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string XmlContent = ZzdPostWebRequest(url, paramData, AccessToken, code, ip, mac, Appid, Secret);
DingtalkUserZZD model2 = JsonConvert.Deserialize<DingtalkUserZZD>(XmlContent);
if (model2.success != true)
{
ErrorMessage = XmlContent;
return null;
}
ErrorFollow.TraceWrite("获取钉钉用户ID", "", model2.ToJsonString());
//url = "https://oapi.dingtalk.com/user/get?access_token=" + AccessToken + "&userid=" + model2.content.data.accountId;
//XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
//if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
//DingtalkUserInfo model3 = JsonConvert.Deserialize<DingtalkUserInfo>(XmlContent);
//if (model3.errcode != 0)
//{
// ErrorMessage = XmlContent;
//}
return model2.content.data;
}
#endregion
#region 发送工作消息
public static void SendMessage(string AccessToken,int AgentID, DingtalkMessage model,out string ErrorMessage)
{
ErrorMessage="";
string url = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=" + AccessToken;
string postmessage = model.ToJson();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string XmlContent = CustomIO.HttpRequest(url, "POST", "Content-Type:application/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return;
ErrorMessage = model2.errmsg;
}
#endregion
#region 发送工作消息
public static void SendMessage(string AccessToken, int AgentID, string msg, out string ErrorMessage)
{
ErrorMessage = "";
string url = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=" + AccessToken;
string postmessage = msg;
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string XmlContent = CustomIO.HttpRequest(url, "POST", "Content-Type:application/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return;
ErrorMessage = model2.errmsg;
}
#endregion
#region 创建部门接口
public static int DingDepartMentCreate(string AccessToken, DingDepartMent model, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/department/create?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = model.ToJson();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return 0;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.id;
ErrorMessage = model2.errmsg;
return 0;
}
#endregion
#region 创建部门接口
public static int DingDepartMentCreate(string AccessToken, string msg, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/department/create?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = msg;
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return 0;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.id;
ErrorMessage = model2.errmsg;
return 0;
}
#endregion
#region 修改部门接口
public static int DingDepartMentUpdate(string AccessToken, DingDepartMent model, out string ErrorMessage)
{
string
url = "https://oapi.dingtalk.com/department/update?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return 0;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.id;
ErrorMessage = model2.errmsg;
return 0;
}
#endregion
#region 修改部门接口
public static int DingDepartMentUpdate(string AccessToken, string msg, out string ErrorMessage)
{
string
url = "https://oapi.dingtalk.com/department/update?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = msg;
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return 0;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.id;
ErrorMessage = model2.errmsg;
return 0;
}
#endregion
#region 删除部门接口
public static void DingDepartMentDelete(string AccessToken, string DeptID, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/department/delete?access_token=" + AccessToken + "&id=" + DeptID;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return;
ErrorMessage = model2.errmsg;
}
#endregion
#region 获取部门列表
public static List<DingDepartMent> DingDepartMent(string Appid, string Secret, out string ErrorMessage)
{
string AccessToken = DingtalkService.DingtalkAccessToken(Appid, Secret, out ErrorMessage);
if (string.IsNullOrEmpty(AccessToken) == true) return null;
ErrorFollow.TraceWrite("DingDepartMent", AccessToken, "");
// AccessToken = "49f5edfc4cc6317a888091616f7000eb";
string url = "https://oapi.dingtalk.com/department/list?access_token=" + AccessToken+ "";
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = "{\"id\":\"1\",\"fetch_child\":true}";
//UTF8Encoding encoding = new UTF8Encoding();
//byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "GET", "text/json", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.department;
ErrorMessage = model2.errmsg;
return null;
}
#endregion
#region 获取部门列表
public static List<DingDepartMent> DingDepartMent2(string Appid, int did,string Secret, out string ErrorMessage)
{
string AccessToken = DingtalkService.DingtalkAccessToken(Appid, Secret, out ErrorMessage);
if (string.IsNullOrEmpty(AccessToken) == true) return null;
ErrorFollow.TraceWrite("DingDepartMent", AccessToken, "");
// AccessToken = "49f5edfc4cc6317a888091616f7000eb";
string url = "https://oapi.dingtalk.com/department/list?access_token=" + AccessToken + "&id="+ did;//98995365
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = "{\"id\":\"1\",\"fetch_child\":true}";
//UTF8Encoding encoding = new UTF8Encoding();
//byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "GET", "text/json", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.department;
ErrorMessage = model2.errmsg;
return null;
}
#endregion
#region 获取部门id列表
public static List<DingDepartMent> DingDepartMentId(string Appid, int did, string Secret, out string ErrorMessage)
{
string AccessToken = DingtalkService.DingtalkAccessToken(Appid, Secret, out ErrorMessage);
if (string.IsNullOrEmpty(AccessToken) == true) return null;
ErrorFollow.TraceWrite("DingDepartMentId", AccessToken, "");
// AccessToken = "49f5edfc4cc6317a888091616f7000eb";
string url = "https://oapi.dingtalk.com/department/list_ids?access_token=" + AccessToken + "&id=1";//98995365
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = "{\"id\":\"1\",\"fetch_child\":true}";
//UTF8Encoding encoding = new UTF8Encoding();
//byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "GET", "text/json", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
ErrorFollow.TraceWrite("DingDepartMentId", XmlContent, "");
// DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
// if (model2.errcode == 0) return model2.department;
// ErrorMessage = model2.errmsg;
return null;
}
#endregion
#region 创建员工接口
public static string DingUserCreate(string AccessToken, DingUser model, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/user/create?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return "";
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.userid;
ErrorMessage = model2.errmsg;
return "";
}
#endregion
#region 创建员工接口
public static string DingUserCreate(string AccessToken, string msg, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/user/create?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = msg;
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return "";
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.userid;
ErrorMessage = model2.errmsg;
return "";
}
#endregion
#region 修改员工接口
public static void DingUserUpdate(string AccessToken, DingUser model, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/user/update?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return;
ErrorMessage = model2.errmsg;
}
#endregion
#region 修改员工接口
public static void DingUserUpdate(string AccessToken, string msg, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/user/update?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
string postmessage = msg;
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return;
ErrorMessage = model2.errmsg;
}
#endregion
#region 删除员工接口
public static void DingUserDelete(string AccessToken, string UserID, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/user/delete?access_token=" + AccessToken + "&userid=" + UserID;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return;
ErrorMessage = model2.errmsg;
}
#endregion
#region 按手机号查询钉钉用户ID接口
public static string DingUserGet(string AccessToken, string Mobile, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/user/get_by_mobile?access_token="+ AccessToken + "&mobile="+ Mobile;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//string postmessage = JsonConvert.Serialize(model);
//string postmessage = model.ToJsonString();
//UTF8Encoding encoding = new UTF8Encoding();
//byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "GET", "text/json", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return "";
DingUserGet model2 = JsonConvert.Deserialize<DingUserGet>(XmlContent);
if (model2.errcode == 0) return model2.userid;
ErrorMessage = model2.errmsg;
return "";
}
#endregion
#region 获取员工列表
public static List<DingUser> DingUser(string Appid,string Secret, string deptid, out string ErrorMessage)
{
string AccessToken = DingtalkService.DingtalkAccessToken(Appid, Secret, out ErrorMessage);
if (string.IsNullOrEmpty(AccessToken) == true) return null;
string url = "https://oapi.dingtalk.com/user/simplelist?access_token=" + AccessToken + "&department_id=" + deptid;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string XmlContent = CustomIO.HttpRequest(url, "GET", "", null, HttpVersion.Version10, null, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0) return model2.userlist;
ErrorMessage = model2.errmsg;
return null;
}
#endregion
#region 获取考勤结果
public static List<DingWorkResult2> GetWork(string AccessToken, DingWorkRequest model, out string ErrorMessage)
{
var ListModel = new List<DingWorkResult2>();
GetWorkDetail(AccessToken, model, ListModel, out ErrorMessage);
return ListModel;
}
public static void GetWorkDetail(string AccessToken, DingWorkRequest model, List<DingWorkResult2> ListResult, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/attendance/list?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
var model2 = JsonConvert.Deserialize<DingWorkResult>(XmlContent);
if (model2.errcode == 0)
{
foreach (var item in model2.recordresult)
{
ListResult.Add(item.Copy());
}
if (model2.hasMore == false)
{
return;
}
model.offset += model.limit;
GetWorkDetail(AccessToken, model, ListResult, out ErrorMessage);
}
else
{
ErrorMessage = model2.errmsg;
}
}
public static List<DingWorkResult2> GetWordRecord(string AccessToken, DingWorkRequest model, out string ErrorMessage)
{
var ListModel = new List<DingWorkResult2>();
GetWorkDetailRecord(AccessToken, model, ListModel, out ErrorMessage);
return ListModel;
}
public static void GetWorkDetailRecord(string AccessToken, DingWorkRequest model, List<DingWorkResult2> ListResult, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/attendance/listRecord?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
var model2 = JsonConvert.Deserialize<DingWorkResult>(XmlContent);
if (model2.errcode == 0)
{
foreach (var item in model2.recordresult)
{
ListResult.Add(item.Copy());
}
if (model2.hasMore == false)
{
return;
}
model.offset += model.limit;
GetWorkDetail(AccessToken, model, ListResult, out ErrorMessage);
}
else
{
ErrorMessage = model2.errmsg;
}
}
#endregion
#region 获取请假时长
public static int GetLeaveTime(string AccessToken,string userid, DateTime StartDate, DateTime StopDate, out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/topapi/attendance/getleaveapproveduration?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
var model = new LeaveTimeRequest();
model.from_date = StartDate.ToString("yyyy-MM-dd HH:mm:ss");
model.to_date = StopDate.ToString("yyyy-MM-dd HH:mm:ss");
model.userid = userid;
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return -1;
DingResult model2 = JsonConvert.Deserialize<DingResult>(XmlContent);
if (model2.errcode == 0)return model2.result.duration_in_minutes;
ErrorMessage = model2.errmsg;
return -1;
}
#endregion
#region 获取审批
public static List<ExamResult> GetListExam(string AccessToken,string examcode, DateTime StartTime,DateTime StopTime, out string ErrorMessage)
{
var requst1 = new ExamIdRequest();
requst1.process_code = examcode;
requst1.start_time = CustomIO.GetTimeStamp(StartTime);
requst1.end_time = CustomIO.GetTimeStamp(StopTime);
var list1 = DingtalkService.GetExamIds(AccessToken, examcode, requst1, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
var ListExam = new List<ExamResult>();
foreach (string item in list1)
{
var model = DingtalkService.GetExam(AccessToken, item, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
ListExam.Add(model);
}
return ListExam;
}
#endregion
#region 获取审批ID列表
public static List<string> GetExamIds(string AccessToken, string code, ExamIdRequest model,out string ErrorMessage)
{
var ListModel = new List<string>();
GetExamIdsDetail(AccessToken, code, model, ListModel, out ErrorMessage);
return ListModel;
}
public static void GetExamIdsDetail(string AccessToken, string code, ExamIdRequest model, List<string> ListResult,out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/topapi/processinstance/listids?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
var model2 = JsonConvert.Deserialize<ExamIdResult>(XmlContent);
if (model2.errcode == 0)
{
foreach (var item in model2.result.list)
{
ListResult.Add(item);
}
if (model2.result.next_cursor == 0)
{
return;
}
model.cursor += model2.result.next_cursor;
GetExamIdsDetail(AccessToken, code, model, ListResult, out ErrorMessage);
}
else
{
ErrorMessage = model2.errmsg;
}
}
#endregion
#region 获取审批详情
public static ExamResult GetExam(string AccessToken, string process_instance_id,out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/topapi/processinstance/get?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string postmessage = "{\"process_instance_id\":\""+ process_instance_id + "\"}";
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
var model2 = JsonConvert.Deserialize<ExamResult>(XmlContent);
model2.process_instance_id = process_instance_id;
if (model2.errcode == 0) return model2;
ErrorMessage = model2.errmsg;
return null;
}
#endregion
#region 获取请假详情
public static LeaveResult GetLeave(string AccessToken, string process_instance_id, out string ErrorMessage)
{
//string XmlContent = GetExam(AccessToken, process_instance_id, out ErrorMessage);
//if (string.IsNullOrEmpty(ErrorMessage) == false) return null;
//JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
//DingResult2 model2 = JsonConvert.Deserialize<DingResult2>(XmlContent);
//if (model2.errcode != 0)
//{
// ErrorMessage = model2.errmsg;
// return null;
//}
//var model = new LeaveResult();
//model.userid = model2.process_instance.originator_userid;
//model.State = -1;
//if (model2.process_instance.result == "refuse") model.State = 0;
//if (model2.process_instance.result== "agree") model.State = 1;
//foreach (var item in model2.process_instance.form_component_values)
//{
// if (item.name == "请假类型") model.LeaveType = item.value;
// if (item.name == "请假事由") model.Cause = item.value;
// if (item.name == "请假天数") model.LeaveDay = Convert.ToInt32(item.value);
// if (item.name == "[\"开始时间\",\"结束时间\"]")
// {
// string sss = item.value.Replace("[", "").Replace("]","").Replace("\"", "");
// string[] ss = sss.Split(',');
// model.StartTime = Convert.ToDateTime(ss[0]);
// model.EndTime = Convert.ToDateTime(ss[1]);
// }
//}
//return model;
ErrorMessage = "";
return null;
}
#endregion
#region 获取日志详情
public static List<DingLogResult3> GetLog(string AccessToken, DingLogRequest model, out string ErrorMessage)
{
var ListModel = new List<DingLogResult3>();
GetLogDetail(AccessToken, model, ListModel, out ErrorMessage);
return ListModel;
}
public static void GetLogDetail(string AccessToken, DingLogRequest model, List<DingLogResult3> ListResult,out string ErrorMessage)
{
string url = "https://oapi.dingtalk.com/topapi/report/list?access_token=" + AccessToken;
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string postmessage = model.ToJsonString();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postmessage);
ErrorFollow.TraceWrite("获取日志详情请求", "", postmessage);
string XmlContent = CustomIO.HttpRequest(url, "POST", "text/json", null, HttpVersion.Version10, data, out ErrorMessage);
if (string.IsNullOrEmpty(ErrorMessage) == false) return;
ErrorFollow.TraceWrite("获取日志详情返回", "", XmlContent);
var model2 = JsonConvert.Deserialize<DingLogResult>(XmlContent);
if (model2.errcode == 0)
{
foreach (var item in model2.result.data_list)
{
ListResult.Add(item.Copy());
}
if (model2.result.has_more == false)
{
return;
}
model.cursor = model2.result.next_cursor;
GetLogDetail(AccessToken, model, ListResult, out ErrorMessage);
}
else
{
ErrorMessage = model2.errmsg;
}
}
#endregion
}
}