using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text; namespace GXWebApi.Common { public class CrmApi { #region 获取CRM系统Token public string GetToken() { string path = "http://esb.gs-robot.com:8800/REST_REST/original/02099000000004/02099"; string result = string.Empty; HttpContent httpContent = new StringContent(""); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; TokenData tokenData = JsonConvert.DeserializeObject(result); return tokenData.token_type + " " + tokenData.access_token; } else { return ""; } } } #endregion #region 创建接口 public string CreateApi(string apiKey, string Token, string JsonData) { string path = "http://esb.gs-robot.com:8800/REST_REST/original/02099000000001/02099?apiKey=" + apiKey; string result = string.Empty; HttpContent httpContent = new StringContent(JsonData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 更新接口 public string UpdateApi(string apiKey, string Id, string Token, string JsonData) { string path = "https://api-p05.xiaoshouyi.com//rest/data/v2.0/xobjects/" + apiKey + "/" + Id; string result = string.Empty; HttpContent httpContent = new StringContent(JsonData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), path) { Content = httpContent }; HttpResponseMessage response = httpClient.SendAsync(request).Result; if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 删除接口 public string DeleteApi(string apiKey, string Id, string Token, string JsonData) { string path = "http://esb.gs-robot.com:8800/REST_REST/original/02099000000003/02099?apiKey=" + apiKey + "&id=" + Id; string result = string.Empty; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Token); //异步Post HttpResponseMessage response = httpClient.DeleteAsync(path).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 用SQL查询接口 public string SelectSQLApi(string q, string Token) { string path = "https://api-p05.xiaoshouyi.com//rest/data/v2/query?q=" + q; string result = string.Empty; HttpContent httpContent = new StringContent(""); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.GetAsync(path).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 获取订单头信息 public string GetOrderInfoApi(string OrderID, string Token) { string path = "https://api-p05.xiaoshouyi.com//rest/data/v2.0/xobjects/order/" + OrderID; string result = string.Empty; HttpContent httpContent = new StringContent(""); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.GetAsync(path).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 创建订单头 public string CreateOrderInfoApi(string JsonData, string Token) { string path = "https://api-p05.xiaoshouyi.com/rest/data/v2.0/xobjects/order"; string result = string.Empty; HttpContent httpContent = new StringContent(JsonData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 获取订单明细信息 public string GetOrderEntryInfoApi(string OrderID, string Token) { string path = "https://api-p05.xiaoshouyi.com//rest/data/v2.0/xobjects/orderProduct/" + OrderID; string result = string.Empty; HttpContent httpContent = new StringContent(""); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.GetAsync(path).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 创建订单明细 public string CreateOrderEntryInfoApi(string JsonData, string Token) { string path = "https://api-p05.xiaoshouyi.com/rest/data/v2.0/xobjects/orderProduct"; string result = string.Empty; HttpContent httpContent = new StringContent(JsonData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 订单生效 public string OrderActivation(string Id, string Token) { string path = "https://api-p05.xiaoshouyi.com//rest/data/v2.0/xobjects/order/actions/activation?recordId=" + Id; string result = string.Empty; HttpContent httpContent = new StringContent(""); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), path) { Content = httpContent }; HttpResponseMessage response = httpClient.SendAsync(request).Result; if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 获取CRM组织内码 public string GetCRMOrgID(string OrgNumber, string Token) { string FOrgId = string.Empty; string SelSQL = "select id from salesOrganization__c where code__c = '" + OrgNumber + "'"; string SelData = SelectSQLApi(SelSQL, Token); SelSQLData selSQLData1 = JsonConvert.DeserializeObject(SelData); int count1 = selSQLData1.result.count; if (count1 > 0) { List> keys = selSQLData1.result.records; FOrgId = keys[0]["id"].ToString(); } return FOrgId; } #endregion #region 获取钉钉员工内码 public string GetDingDingUserID(string userids, string Token) { string path = "https://api-p05.xiaoshouyi.com/rest/auc/v2.0/third-party/actions/id-mapping?source=dingtalk&userIds=" + userids; string result = string.Empty; HttpContent httpContent = new StringContent(""); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.GetAsync(path).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 获取CRM交付与发货表头信息 public string GetDeliveryApplicationApi(string BillNO, string Token) { string path = "https://api-p05.xiaoshouyi.com/rest/data/v2.0/xobjects/deliveryRecord/" + BillNO; string result = string.Empty; HttpContent httpContent = new StringContent(""); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", Token); //异步Post HttpResponseMessage response = httpClient.GetAsync(path).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 日期转时间戳 public long ToUnixTimeMilliseconds(DateTime dateTime) { DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime utcTime = dateTime.ToUniversalTime(); TimeSpan timeSpan = utcTime - UnixEpoch; return (long)timeSpan.TotalMilliseconds; } #endregion #region 时间戳转日期 public string ToMillisecondsUnixTime(long unixTime) { DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); dtDateTime = dtDateTime.AddMilliseconds(unixTime).ToLocalTime(); // 转换为本地时间 return dtDateTime.ToString("yyyy-MM-dd"); } #endregion } #region 自定义变量 public class TokenData { public string access_token; public string token_type; public string expires_in; public string scope; public string tenant_id; public string login_type; public string encryption_key; public string client_id; public string passport_id; public string mobile_params; public string instance_uri; } public class Data { /// /// /// public string id { get; set; } /// /// /// public string objectApiKey { get; set; } /// /// /// public string busiType { get; set; } /// /// /// public string name { get; set; } } public class CreateReturnData { /// /// /// public string code { get; set; } /// /// /// public string msg { get; set; } /// /// /// public Data data { get; set; } } #endregion #region 自定义变量 public class UpData { /// /// /// public string customItem168__c { get; set; } /// /// /// public string productName { get; set; } /// /// 测试物料21 /// public string customItem161__c { get; set; } /// /// /// public int bomType__c { get; set; } /// /// /// public int parentId { get; set; } } public class UpdataRoot { /// /// /// public UpData data { get; set; } } #endregion #region 查询SQL返回类 public class Result { /// /// /// public int totalSize { get; set; } /// /// /// public int count { get; set; } /// /// /// public List> records { get; set; } } public class SelSQLData { /// /// /// public string code { get; set; } /// /// /// public string msg { get; set; } /// /// /// public List ext { get; set; } /// /// /// public Result result { get; set; } } #endregion #region 创建产品目录类 public class ProductCategoryData { /// /// /// public string entityType { get; set; } /// /// /// public string code__c { get; set; } /// /// /// public string name { get; set; } /// /// /// public string productModel__c { get; set; } /// /// /// public string enableStatus { get; set; } /// /// /// public string parentName { get; set; } } public class CProductCategory { /// /// /// public ProductCategoryData data { get; set; } } #endregion #region 创建产品类 public class ProductData { /// /// /// public string entityType { get; set; } /// /// /// public string customItem168__c { get; set; } /// /// /// public string productName { get; set; } /// /// /// public string customItem161__c { get; set; } /// /// /// public string fscProductModel { get; set; } /// /// /// public string enableStatus { get; set; } /// /// /// public string defaultTaxRate__c { get; set; } /// /// /// public string baseUnitId { get; set; } public bool isActiveBom__c { get; set; } /// /// /// public string parentId { get; set; } } public class ProductRoot { /// /// /// public ProductData data { get; set; } } #endregion #region 更新交付与发货状态 public class DeliveryRecordRoot { /// /// /// public DeliveryRecordData data { get; set; } } public class DeliveryRecordData { /// /// /// public string syncStatus__c { get; set; } /// /// /// public string syncMessage__c { get; set; } } #endregion #region 创建BOM类 public class BOMRoot { /// /// /// public BOMData data { get; set; } } public class BOMData { /// /// /// public string entityType { get; set; } /// /// /// public string parentProductCode__c { get; set; } /// /// /// public string bomType__c { get; set; } public string enableStatus__c { get; set; } /// /// /// public string name { get; set; } /// /// /// public string bomShortName__c { get; set; } } #endregion }