cyAPI.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Net.Http.Headers;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using WebApplication.controller;
  13. namespace WebApplication.Controllers
  14. {
  15. /// <summary>
  16. /// 氚云API
  17. /// </summary>
  18. public class cyAPI
  19. {
  20. WebApi webApi = new WebApi();
  21. printInfo printInfo = new printInfo();
  22. static HttpClient client = new HttpClient();
  23. /// <summary>
  24. /// 公用方法
  25. /// </summary>
  26. /// <param name="peopledata"></param>
  27. /// <returns></returns>
  28. public string commonMethod(string token, string peopledata,string ufname,string FilePropertyName)
  29. {
  30. string result = instances(token,"e02b79c9-ec99-4982-bcb7-2811457b98c8", peopledata); //e02b79c9-ec99-4982-bcb7-2811457b98c8
  31. JObject toArr = JsonConvert.DeserializeObject<JObject>(result);
  32. if (toArr["code"].ToString() == "success")
  33. {
  34. string dtnow = DateTimeToLongTimeStamp(DateTime.Now).ToString();
  35. printInfo.WriteDataToExcelTemplate("E:/jsjPrintTemplate/XBPrintTemplateOA.xlsx", "E:/jsjPrintTemplate/XBPrintTemplateOA" + dtnow + ".xlsx", JsonConvert.SerializeObject(peopledata[0]).ToString(), "OA审批");
  36. printInfo.excelToPdf("E:/jsjPrintTemplate/XBPrintTemplateOA" + dtnow + ".xlsx", "E:/jsjPrintTemplate/XBPrintTemplateOA" + dtnow + ".PDF");
  37. string bizObjectId = toArr["data"]["bizObjectId"].ToString();
  38. string schemaCode = toArr["data"]["schemaCode"].ToString();
  39. UploadAttachment(schemaCode, bizObjectId, "E:\\jsjPrintTemplate\\XBPrintTemplateOA" + dtnow + ".PDF", ufname, FilePropertyName);
  40. }
  41. return "true";
  42. }
  43. /// <summary>
  44. /// Post 方式调用表单创建
  45. /// </summary>
  46. /// <returns></returns>
  47. public string instances(string token,string opUserId,string bizJson)
  48. {
  49. string url = "https://api.dingtalk.com/v1.0/h3yun/forms/instances";
  50. Dictionary<string, string> dic = new Dictionary<string, string>();
  51. dic.Add("schemaCode", "D1463788fabdbfcf89b4080b91d58b1dbc7f856");
  52. dic.Add("opUserId", opUserId);
  53. dic.Add("bizObjectJson", bizJson);
  54. dic.Add("isDraft", "false");
  55. Hashtable headht = new Hashtable();
  56. headht.Add("x-acs-dingtalk-access-token", token);
  57. string toredate = webApi.PostJson(url, JsonConvert.SerializeObject(dic).ToString(), headht);
  58. //string toredate = webApi.HttpPosts(url, JsonConvert.SerializeObject(dic).ToString(), token);
  59. return toredate;
  60. }
  61. /// <summary>
  62. /// Get 方法调用文件上传接口地址
  63. /// </summary>
  64. /// <returns></returns>
  65. public string uploadUrls(string token,string schemaCode, string bizObjectId,string fieldName)
  66. {
  67. //GET / v1.0 / h3yun / attachments / uploadUrls ? schemaCode = String & bizObjectId = String & fieldName = String & isOverwrite = Boolean HTTP / 1.1
  68. //Host: api.dingtalk.com
  69. //x - acs - dingtalk - access - token:String
  70. //Content - Type:application / json
  71. //schemaCode 表单编码。 bizObjectId 业务数据实例ID。 fieldName 文件上传至目标控件的字段名。 isOverwrite 是否覆盖,取值:false:添加
  72. string url = "https://api.dingtalk.com/v1.0/h3yun/attachments/uploadUrls?schemaCode="+ schemaCode + "&bizObjectId="+ bizObjectId + "&fieldName="+ fieldName + "&isOverwrite=false";
  73. var errUf = webApi.HttpGet(url, token);
  74. return errUf.ToString() ;
  75. }
  76. /// <summary>
  77. /// 附件上传接口 Post
  78. /// </summary>
  79. /// <param name="SchemaCode">表单编码</param>
  80. /// <param name="BizObjectId">表单控件编码</param>
  81. /// <param name="fileurl">表单ObjectId值</param>
  82. /// <param name="name">名称</param>
  83. /// <returns></returns>
  84. public string UploadAttachment(string SchemaCode,string BizObjectId,string fileurl,string name,string FilePropertyName)
  85. {
  86. WebHeaderCollection head = new WebHeaderCollection();
  87. head.Add("EngineCode", "mdhf9vbowovdb2xastdoua0f0");
  88. head.Add("EngineSecret", "jMr8Hdq+RmPcGHfP8OPBQ/23a+dsNkvE3NH+6KFu5OfNvhUJ07cSmg==");
  89. //请求的URL
  90. string url = "https://www.h3yun.com/OpenApi/UploadAttachment?SchemaCode="+ SchemaCode + "&FilePropertyName="+ FilePropertyName + "&BizObjectId="+ BizObjectId;
  91. string directory = Path.GetDirectoryName(fileurl);
  92. if (!Directory.Exists(directory))
  93. {
  94. Directory.CreateDirectory(directory);
  95. }
  96. //读取本地文件
  97. FileStream file = new FileStream(fileurl, FileMode.Open);
  98. var fd = file.Length;
  99. byte[] bytes = new byte[fd];
  100. file.Read(bytes, 0, Convert.ToInt32(fd));
  101. file.Close();
  102. //调用上传方法
  103. string filename = name+".PDF"; //氚云显示的文件名,必须带后缀(例:file.jpg)
  104. string contentType = "application/pdf";//文件ContentType,不清楚可以打开网址(https://www.w3school.com.cn/media/media_mimeref.asp)参考(例:image/jpg)
  105. string result = _UploadFile(url, bytes, filename, head, contentType);
  106. return result;
  107. }
  108. /// <summary>
  109. /// 上传方法
  110. /// </summary>
  111. /// <param name="url">上传接口Url</param>
  112. /// <param name="bytes">文件Byte</param>
  113. /// <param name="fileName">文件名</param>
  114. /// <param name="headers">请求Headers</param>
  115. /// <param name="contentType">文件ContentType</param>
  116. /// <returns></returns>
  117. private static string _UploadFile(string url, byte[] bytes, string fileName, WebHeaderCollection headers, string contentType)
  118. {
  119. Uri oUri = new Uri(url);
  120. string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
  121. // The trailing boundary string
  122. byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n");
  123. // The post message header
  124. StringBuilder sb = new StringBuilder();
  125. sb.Append("--");
  126. sb.Append(strBoundary);
  127. sb.Append("\r\n");
  128. sb.Append("Content-Disposition: form-data; name=\"media\";");
  129. sb.Append(" filename=\"");
  130. sb.Append(fileName);
  131. sb.Append("\"");
  132. sb.Append("\r\n");
  133. sb.Append("Content-Type: ");
  134. sb.Append(contentType + "\r\n\r\n");
  135. string strPostHeader = sb.ToString();
  136. byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
  137. // The WebRequest
  138. HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);
  139. oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
  140. oWebrequest.Method = "POST";
  141. if (headers != null)
  142. {
  143. foreach (string key in headers.Keys)
  144. {
  145. oWebrequest.Headers.Add(key, headers[key]);
  146. }
  147. }
  148. // This is important, otherwise the whole file will be read to memory anyway...
  149. oWebrequest.AllowWriteStreamBuffering = false;
  150. long length = postHeaderBytes.Length + bytes.Length + boundaryBytes.Length;
  151. oWebrequest.ContentLength = length;
  152. using (Stream oRequestStream = oWebrequest.GetRequestStream())
  153. {
  154. // Write the post header
  155. oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
  156. oRequestStream.Write(bytes, 0, bytes.Length);
  157. // Add the trailing boundary
  158. oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
  159. WebResponse oWResponse = oWebrequest.GetResponse();
  160. using (Stream s = oWResponse.GetResponseStream())
  161. {
  162. using (StreamReader sr = new StreamReader(s))
  163. {
  164. string sReturnString = sr.ReadToEnd();
  165. return sReturnString;
  166. }
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// DateTime转换为13位时间戳(单位:毫秒)
  172. /// </summary>
  173. /// <param name="dateTime"> DateTime</param>
  174. /// <returns>13位时间戳(单位:毫秒)</returns>
  175. public static long DateTimeToLongTimeStamp(DateTime dateTime)
  176. {
  177. return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
  178. }
  179. }
  180. }