123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- using WebApplication.controller;
- namespace WebApplication.Controllers
- {
- /// <summary>
- /// 氚云API
- /// </summary>
- public class cyAPI
- {
- WebApi webApi = new WebApi();
- printInfo printInfo = new printInfo();
- static HttpClient client = new HttpClient();
- /// <summary>
- /// 公用方法
- /// </summary>
- /// <param name="peopledata"></param>
- /// <returns></returns>
- public string commonMethod(string token, string peopledata,string ufname,string FilePropertyName)
- {
- string result = instances(token,"e02b79c9-ec99-4982-bcb7-2811457b98c8", peopledata); //e02b79c9-ec99-4982-bcb7-2811457b98c8
- JObject toArr = JsonConvert.DeserializeObject<JObject>(result);
- if (toArr["code"].ToString() == "success")
- {
- string dtnow = DateTimeToLongTimeStamp(DateTime.Now).ToString();
- printInfo.WriteDataToExcelTemplate("E:/jsjPrintTemplate/XBPrintTemplateOA.xlsx", "E:/jsjPrintTemplate/XBPrintTemplateOA" + dtnow + ".xlsx", JsonConvert.SerializeObject(peopledata[0]).ToString(), "OA审批");
- printInfo.excelToPdf("E:/jsjPrintTemplate/XBPrintTemplateOA" + dtnow + ".xlsx", "E:/jsjPrintTemplate/XBPrintTemplateOA" + dtnow + ".PDF");
- string bizObjectId = toArr["data"]["bizObjectId"].ToString();
- string schemaCode = toArr["data"]["schemaCode"].ToString();
- UploadAttachment(schemaCode, bizObjectId, "E:\\jsjPrintTemplate\\XBPrintTemplateOA" + dtnow + ".PDF", ufname, FilePropertyName);
- }
- return "true";
- }
- /// <summary>
- /// Post 方式调用表单创建
- /// </summary>
- /// <returns></returns>
- public string instances(string token,string opUserId,string bizJson)
- {
- string url = "https://api.dingtalk.com/v1.0/h3yun/forms/instances";
- Dictionary<string, string> dic = new Dictionary<string, string>();
- dic.Add("schemaCode", "D1463788fabdbfcf89b4080b91d58b1dbc7f856");
- dic.Add("opUserId", opUserId);
- dic.Add("bizObjectJson", bizJson);
- dic.Add("isDraft", "false");
- Hashtable headht = new Hashtable();
- headht.Add("x-acs-dingtalk-access-token", token);
- string toredate = webApi.PostJson(url, JsonConvert.SerializeObject(dic).ToString(), headht);
- //string toredate = webApi.HttpPosts(url, JsonConvert.SerializeObject(dic).ToString(), token);
- return toredate;
- }
- /// <summary>
- /// Get 方法调用文件上传接口地址
- /// </summary>
- /// <returns></returns>
- public string uploadUrls(string token,string schemaCode, string bizObjectId,string fieldName)
- {
- //GET / v1.0 / h3yun / attachments / uploadUrls ? schemaCode = String & bizObjectId = String & fieldName = String & isOverwrite = Boolean HTTP / 1.1
- //Host: api.dingtalk.com
- //x - acs - dingtalk - access - token:String
- //Content - Type:application / json
- //schemaCode 表单编码。 bizObjectId 业务数据实例ID。 fieldName 文件上传至目标控件的字段名。 isOverwrite 是否覆盖,取值:false:添加
- string url = "https://api.dingtalk.com/v1.0/h3yun/attachments/uploadUrls?schemaCode="+ schemaCode + "&bizObjectId="+ bizObjectId + "&fieldName="+ fieldName + "&isOverwrite=false";
- var errUf = webApi.HttpGet(url, token);
- return errUf.ToString() ;
- }
- /// <summary>
- /// 附件上传接口 Post
- /// </summary>
- /// <param name="SchemaCode">表单编码</param>
- /// <param name="BizObjectId">表单控件编码</param>
- /// <param name="fileurl">表单ObjectId值</param>
- /// <param name="name">名称</param>
- /// <returns></returns>
- public string UploadAttachment(string SchemaCode,string BizObjectId,string fileurl,string name,string FilePropertyName)
- {
- WebHeaderCollection head = new WebHeaderCollection();
- head.Add("EngineCode", "mdhf9vbowovdb2xastdoua0f0");
- head.Add("EngineSecret", "jMr8Hdq+RmPcGHfP8OPBQ/23a+dsNkvE3NH+6KFu5OfNvhUJ07cSmg==");
- //请求的URL
- string url = "https://www.h3yun.com/OpenApi/UploadAttachment?SchemaCode="+ SchemaCode + "&FilePropertyName="+ FilePropertyName + "&BizObjectId="+ BizObjectId;
- string directory = Path.GetDirectoryName(fileurl);
- if (!Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- //读取本地文件
- FileStream file = new FileStream(fileurl, FileMode.Open);
- var fd = file.Length;
- byte[] bytes = new byte[fd];
- file.Read(bytes, 0, Convert.ToInt32(fd));
- file.Close();
- //调用上传方法
- string filename = name+".PDF"; //氚云显示的文件名,必须带后缀(例:file.jpg)
- string contentType = "application/pdf";//文件ContentType,不清楚可以打开网址(https://www.w3school.com.cn/media/media_mimeref.asp)参考(例:image/jpg)
- string result = _UploadFile(url, bytes, filename, head, contentType);
- return result;
- }
- /// <summary>
- /// 上传方法
- /// </summary>
- /// <param name="url">上传接口Url</param>
- /// <param name="bytes">文件Byte</param>
- /// <param name="fileName">文件名</param>
- /// <param name="headers">请求Headers</param>
- /// <param name="contentType">文件ContentType</param>
- /// <returns></returns>
- private static string _UploadFile(string url, byte[] bytes, string fileName, WebHeaderCollection headers, string contentType)
- {
- Uri oUri = new Uri(url);
- string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
- // The trailing boundary string
- byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n");
- // The post message header
- StringBuilder sb = new StringBuilder();
- sb.Append("--");
- sb.Append(strBoundary);
- sb.Append("\r\n");
- sb.Append("Content-Disposition: form-data; name=\"media\";");
- sb.Append(" filename=\"");
- sb.Append(fileName);
- sb.Append("\"");
- sb.Append("\r\n");
- sb.Append("Content-Type: ");
- sb.Append(contentType + "\r\n\r\n");
- string strPostHeader = sb.ToString();
- byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
- // The WebRequest
- HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);
- oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
- oWebrequest.Method = "POST";
- if (headers != null)
- {
- foreach (string key in headers.Keys)
- {
- oWebrequest.Headers.Add(key, headers[key]);
- }
- }
- // This is important, otherwise the whole file will be read to memory anyway...
- oWebrequest.AllowWriteStreamBuffering = false;
- long length = postHeaderBytes.Length + bytes.Length + boundaryBytes.Length;
- oWebrequest.ContentLength = length;
- using (Stream oRequestStream = oWebrequest.GetRequestStream())
- {
- // Write the post header
- oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
- oRequestStream.Write(bytes, 0, bytes.Length);
- // Add the trailing boundary
- oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
- WebResponse oWResponse = oWebrequest.GetResponse();
- using (Stream s = oWResponse.GetResponseStream())
- {
- using (StreamReader sr = new StreamReader(s))
- {
- string sReturnString = sr.ReadToEnd();
- return sReturnString;
- }
- }
- }
- }
- /// <summary>
- /// DateTime转换为13位时间戳(单位:毫秒)
- /// </summary>
- /// <param name="dateTime"> DateTime</param>
- /// <returns>13位时间戳(单位:毫秒)</returns>
- public static long DateTimeToLongTimeStamp(DateTime dateTime)
- {
- return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
- }
- }
- }
|