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
{
///
/// 氚云API
///
public class cyAPI
{
WebApi webApi = new WebApi();
printInfo printInfo = new printInfo();
static HttpClient client = new HttpClient();
///
/// 公用方法
///
///
///
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(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";
}
///
/// Post 方式调用表单创建
///
///
public string instances(string token,string opUserId,string bizJson)
{
string url = "https://api.dingtalk.com/v1.0/h3yun/forms/instances";
Dictionary dic = new Dictionary();
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;
}
///
/// Get 方法调用文件上传接口地址
///
///
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() ;
}
///
/// 附件上传接口 Post
///
/// 表单编码
/// 表单控件编码
/// 表单ObjectId值
/// 名称
///
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;
}
///
/// 上传方法
///
/// 上传接口Url
/// 文件Byte
/// 文件名
/// 请求Headers
/// 文件ContentType
///
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;
}
}
}
}
///
/// DateTime转换为13位时间戳(单位:毫秒)
///
/// DateTime
/// 13位时间戳(单位:毫秒)
public static long DateTimeToLongTimeStamp(DateTime dateTime)
{
return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
}
}