Files

716 lines
24 KiB
C#
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<TokenData>(result);
return tokenData.token_type + " " + tokenData.access_token;
}
else
{
return "";
}
}
}
#endregion
#region 获取CRM系统Token(无Bearer)
public string GetToken1()
{
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<TokenData>(result);
return 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 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<SelSQLData>(SelData);
int count1 = selSQLData1.result.count;
if (count1 > 0)
{
List<Dictionary<string, object>> 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
{
/// <summary>
///
/// </summary>
public string id { get; set; }
/// <summary>
///
/// </summary>
public string objectApiKey { get; set; }
/// <summary>
///
/// </summary>
public string busiType { get; set; }
/// <summary>
///
/// </summary>
public string name { get; set; }
}
public class CreateReturnData
{
/// <summary>
///
/// </summary>
public string code { get; set; }
/// <summary>
///
/// </summary>
public string msg { get; set; }
/// <summary>
///
/// </summary>
public Data data { get; set; }
}
#endregion
#region 自定义变量
public class UpData
{
/// <summary>
///
/// </summary>
public string customItem168__c { get; set; }
/// <summary>
///
/// </summary>
public string productName { get; set; }
/// <summary>
/// 测试物料21
/// </summary>
public string customItem161__c { get; set; }
/// <summary>
///
/// </summary>
public int bomType__c { get; set; }
/// <summary>
///
/// </summary>
public int parentId { get; set; }
}
public class UpdataRoot
{
/// <summary>
///
/// </summary>
public UpData data { get; set; }
}
#endregion
#region 查询SQL返回类
public class Result
{
/// <summary>
///
/// </summary>
public int totalSize { get; set; }
/// <summary>
///
/// </summary>
public int count { get; set; }
/// <summary>
///
/// </summary>
public List<Dictionary<string, object>> records { get; set; }
}
public class SelSQLData
{
/// <summary>
///
/// </summary>
public string code { get; set; }
/// <summary>
///
/// </summary>
public string msg { get; set; }
/// <summary>
///
/// </summary>
public List<string> ext { get; set; }
/// <summary>
///
/// </summary>
public Result result { get; set; }
}
#endregion
#region 创建产品目录类
public class ProductCategoryData
{
/// <summary>
///
/// </summary>
public string entityType { get; set; }
/// <summary>
///
/// </summary>
public string code__c { get; set; }
/// <summary>
///
/// </summary>
public string name { get; set; }
/// <summary>
///
/// </summary>
public string productModel__c { get; set; }
/// <summary>
///
/// </summary>
public string enableStatus { get; set; }
/// <summary>
///
/// </summary>
public string parentName { get; set; }
}
public class CProductCategory
{
/// <summary>
///
/// </summary>
public ProductCategoryData data { get; set; }
}
#endregion
#region 创建产品类
public class ProductData
{
/// <summary>
///
/// </summary>
public string entityType { get; set; }
/// <summary>
///
/// </summary>
public string customItem168__c { get; set; }
/// <summary>
///
/// </summary>
public string productName { get; set; }
/// <summary>
///
/// </summary>
public string customItem161__c { get; set; }
/// <summary>
///
/// </summary>
public string fscProductModel { get; set; }
/// <summary>
///
/// </summary>
public string enableStatus { get; set; }
/// <summary>
///
/// </summary>
public string defaultTaxRate__c { get; set; }
/// <summary>
///
/// </summary>
public string baseUnitId { get; set; }
public bool isActiveBom__c { get; set; }
/// <summary>
///
/// </summary>
public string parentId { get; set; }
}
public class ProductRoot
{
/// <summary>
///
/// </summary>
public ProductData data { get; set; }
}
#endregion
#region 更新交付与发货状态
public class DeliveryRecordRoot
{
/// <summary>
///
/// </summary>
public DeliveryRecordData data { get; set; }
}
public class DeliveryRecordData
{
/// <summary>
///
/// </summary>
public string syncStatus__c { get; set; }
/// <summary>
///
/// </summary>
public string syncMessage__c { get; set; }
}
#endregion
#region 创建BOM类
public class BOMRoot
{
/// <summary>
///
/// </summary>
public BOMData data { get; set; }
}
public class BOMData
{
/// <summary>
///
/// </summary>
public string entityType { get; set; }
/// <summary>
///
/// </summary>
public string parentProductCode__c { get; set; }
/// <summary>
///
/// </summary>
public string bomType__c { get; set; }
public string enableStatus__c { get; set; }
/// <summary>
///
/// </summary>
public string name { get; set; }
/// <summary>
///
/// </summary>
public string bomShortName__c { get; set; }
}
#endregion
}