You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
8.1 KiB
212 lines
8.1 KiB
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace ICSSoft.FromERP
|
|
{
|
|
public class YSHepler
|
|
|
|
{
|
|
public static string PostByTokenGet(string url, Dictionary<string, string> parameters, string token)
|
|
{
|
|
return GetInfo("", url, token);
|
|
}
|
|
public static string GetInfo(string postData, string Url, string token)
|
|
{
|
|
try
|
|
{
|
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
|
|
HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(Url);
|
|
objWebRequest.Method = "Get";
|
|
objWebRequest.ContentType = "application/json;charset=UTF-8";
|
|
objWebRequest.ContentLength = byteArray.Length;
|
|
//objWebRequest.Headers.Add("Authorization", token);
|
|
HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
|
|
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
string textResponse = sr.ReadToEnd(); // 返回的数据
|
|
return textResponse;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
public static long ConvertDateTimeInt(System.DateTime time)
|
|
{
|
|
//double intResult = 0;
|
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
|
|
//intResult = (time- startTime).TotalMilliseconds;
|
|
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
|
|
return t;
|
|
}
|
|
public static string GetConfigString(string name)
|
|
{
|
|
try
|
|
{
|
|
Configuration config = GetConfig();
|
|
string configString = config.ConnectionStrings.ConnectionStrings[name].ConnectionString.ToString();
|
|
return configString;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public static string GetToken()
|
|
{
|
|
string configString = "";
|
|
try
|
|
{
|
|
string SearchUrl = "https://api.diwork.com/open-auth/dataCenter/getGatewayAddress?tenantId=" + GetConfigString("ZH") + "";
|
|
Dictionary<string, string> dic = new Dictionary<string, string>();
|
|
|
|
string Sresult = PostByTokenGet(SearchUrl, dic, "");
|
|
JObject Sobj = new JObject();
|
|
Sobj = JObject.Parse(Sresult);
|
|
|
|
if (Sobj["code"].ToString() == "00000")
|
|
{
|
|
string timesp = ConvertDateTimeInt(DateTime.Now).ToString();
|
|
string ZXSDS = Sign("appKey" + GetConfigString("AppKey") + "timestamp" + timesp + "", "" + GetConfigString("AppSecret") + "");
|
|
string tokenurl = Sobj["data"]["tokenUrl"] + "/open-auth/selfAppAuth/getAccessToken?appKey=" + GetConfigString("AppKey") + "×tamp=" + timesp + "&signature=" + ZXSDS + "";
|
|
SearchUrl = tokenurl;
|
|
dic = new Dictionary<string, string>();
|
|
|
|
Sresult = YSHepler.PostByTokenGet(SearchUrl, dic, "");
|
|
Sobj = JObject.Parse(Sresult);
|
|
if (Sobj["code"].ToString() == "00000")
|
|
configString = Sobj["data"]["access_token"].ToString();
|
|
}
|
|
return configString;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public static string GetSELECTURL()
|
|
{
|
|
string configString = "";
|
|
try
|
|
{
|
|
string SearchUrl = "https://api.diwork.com/open-auth/dataCenter/getGatewayAddress?tenantId=" + GetConfigString("ZH") + "";
|
|
Dictionary<string, string> dic = new Dictionary<string, string>();
|
|
|
|
string Sresult = PostByTokenGet(SearchUrl, dic, "");
|
|
JObject Sobj = new JObject();
|
|
Sobj = JObject.Parse(Sresult);
|
|
|
|
if (Sobj["code"].ToString() == "00000")
|
|
{
|
|
configString = Sobj["data"]["gatewayUrl"].ToString();
|
|
}
|
|
return configString;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public static string HttpPost(string url, string body, string token, string MethodType)
|
|
{
|
|
try
|
|
{
|
|
Encoding encoding = Encoding.UTF8;
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.Method = MethodType;
|
|
request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
|
|
request.ContentType = "application/json; charset=utf-8";
|
|
//request.Headers.Add("Authorization", token);
|
|
byte[] buffer = encoding.GetBytes(body);
|
|
request.ContentLength = buffer.Length;
|
|
request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
|
|
{
|
|
return reader.ReadToEnd();
|
|
}
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
var res = (HttpWebResponse)ex.Response;
|
|
StringBuilder sb = new StringBuilder();
|
|
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
|
|
sb.Append(sr.ReadToEnd());
|
|
//string ssb = sb.ToString();
|
|
throw new Exception(sb.ToString());
|
|
}
|
|
}
|
|
public static Configuration GetConfig()
|
|
{
|
|
Assembly assembly = Assembly.GetCallingAssembly();
|
|
string path = string.Format("{0}.config", assembly.Location);
|
|
if (!File.Exists(path))
|
|
{
|
|
throw new FileNotFoundException(path + "路径下的文件未找到!");
|
|
}
|
|
try
|
|
{
|
|
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
|
|
configFile.ExeConfigFilename = path;
|
|
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
|
|
return config;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
public static string Sign(string str, string secret)
|
|
{
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(secret);
|
|
byte[] dataBytes = Encoding.UTF8.GetBytes(str);
|
|
|
|
using (var hmac = new HMACSHA256(keyBytes))
|
|
{
|
|
byte[] hashBytes = hmac.ComputeHash(dataBytes);
|
|
return UrlEncodeBase64(hashBytes);
|
|
}
|
|
}
|
|
public static string HmacSHA256(string set, string key)
|
|
{
|
|
string sp = string.Empty;
|
|
using (HMACSHA256 MAC = new HMACSHA256(Encoding.UTF8.GetBytes(key)))
|
|
{
|
|
byte[] HASH = MAC.ComputeHash(Encoding.UTF8.GetBytes(set));
|
|
sp = Convert.ToBase64String(HASH);
|
|
}
|
|
return sp;
|
|
|
|
}
|
|
private static string UrlEncodeBase64(byte[] data)
|
|
{
|
|
string base64 = Convert.ToBase64String(data);
|
|
string urlEncoded = Uri.EscapeDataString(base64);
|
|
return urlEncoded;
|
|
}
|
|
public static string EncodeBase64(string code_type, string code)
|
|
{
|
|
string encode = "";
|
|
byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
|
|
try
|
|
{
|
|
encode = Convert.ToBase64String(bytes);
|
|
}
|
|
catch
|
|
{
|
|
encode = code;
|
|
}
|
|
return encode;
|
|
}
|
|
}
|
|
}
|