Files
inside_out_web_data_interac…/Test/Test/App_Start/Service.cs
2022-03-17 10:16:39 +08:00

142 lines
6.9 KiB
C#

using Getf.Service.Transfer.Request.SDK;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace Test.App_Start
{
public class Service
{
private readonly static string TransferServiceIpInfo = System.Configuration.ConfigurationManager.AppSettings["TransferService_IpInfo"];
private readonly static string TransferServiceAppID = System.Configuration.ConfigurationManager.AppSettings["TransferService_AppID"];
private readonly static string TransferServiceAppSecret = System.Configuration.ConfigurationManager.AppSettings["TransferService_AppSecret"];
private readonly static string TransferServiceTargetAppID = System.Configuration.ConfigurationManager.AppSettings["TransferService_TargetAppID"];
//private readonly static string TransferServiceUrlSaveData = System.Configuration.ConfigurationManager.AppSettings["TransferService_Url_SaveData"];
private readonly static string TransferService_api1_data_Url = System.Configuration.ConfigurationManager.AppSettings["TransferService_api1_data_Url"];
private readonly static string TransferService_api2_data_Url = System.Configuration.ConfigurationManager.AppSettings["TransferService_api2_data_Url"];
private readonly static string TransferService_api1_appkey = System.Configuration.ConfigurationManager.AppSettings["TransferService_api1_appkey"];
private readonly static string TransferService_api1_appsecret = System.Configuration.ConfigurationManager.AppSettings["TransferService_api1_appsecret"];
private readonly string TransferServiceIp = TransferServiceIpInfo.Split(':')[0];
private readonly int TransferServicePort = int.Parse(TransferServiceIpInfo.Split(':')[1]);
public string GetDataGetPdf(string url, string queryParam, out string msg)
{
using (RequestSevice requestSevice = new RequestSevice(TransferServiceIp, TransferServicePort))
{
var r = requestSevice.GetResult(TransferServiceTargetAppID, url,
JsonConvert.SerializeObject(new
{
Param = "",
//ContentType = "application/json",
Method = "get"
}));
msg = "";
var resultJson = (byte[])r.Data;
var filename = "a.pdf";
var filepath = HttpContext.Current.Server.MapPath("~/" + filename);
using (var fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))
{
fs.Write(resultJson, 0, resultJson.Length);
}
return "http://localhost:28436/" + filename;
}
}
public string GetDataTest(string name, string cardno, out string msg)
{
using (RequestSevice requestSevice = new RequestSevice(TransferServiceIp, TransferServicePort))
{
var param_obj = new JObject();
param_obj.Add("type", "nb");
param_obj.Add("urls", "http://10.68.138.194/gateway/api/001008002016003/dataSharing/vdjb19VHdIh27Rc5.htm");
param_obj.Add("params", "czrkgmsfhm=" + cardno + "&czrkxm=" + name + "&additional={\"powerMatters\":\"许可0000-00\",\"subPowerMatters\":\"许可0000-0101\",\"accesscardId\":\"" + cardno + "\"}");
var param = new JObject();
param.Add("param", Newtonsoft.Json.JsonConvert.SerializeObject(param_obj));
var r = requestSevice.GetResult(TransferServiceTargetAppID, "http://10.19.93.192:8096/api/data/get",
JsonConvert.SerializeObject(new
{
Param = Newtonsoft.Json.JsonConvert.SerializeObject(param),
ContentType = "application/json"
}));
msg = "";
var resultJson = r.TransResultInfo.Message;
return resultJson;
}
}
public string GetDataJson(string url, string appkey, string appsecret, Dictionary<string, string> other_param, out string msg)
{
using (RequestSevice requestSevice = new RequestSevice(TransferServiceIp, TransferServicePort))
{
var ts = GetTimeStamp();
var param = new JObject
{
{ "appKey", appkey },
{ "requestTime", ts.ToString()},
{ "sign", GetSign(ts, appkey, appsecret) }
};
foreach (var item in other_param)
{
param.Add(item.Key, item.Value);
}
var r = requestSevice.GetResult(TransferServiceTargetAppID, url,
JsonConvert.SerializeObject(new
{
Param = Newtonsoft.Json.JsonConvert.SerializeObject(param),
ContentType = "application/json"
}));
msg = "";
var resultJson = r.TransResultInfo.Message;
return resultJson;
}
}
public string GetData(string url, string appkey, string appsecret, string other_param, out string msg)
{
using (RequestSevice requestSevice = new RequestSevice(TransferServiceIp, TransferServicePort))
{
var r = requestSevice.GetResult(TransferServiceTargetAppID, url,
JsonConvert.SerializeObject(new
{
Param = GetDefaultParam(appkey, appsecret) + other_param// $"&czrkxm={name}&czrkgmsfhm={cardno}"
}));
msg = "";
var resultJson = r.TransResultInfo.Message;
return resultJson;
}
}
private string GetDefaultParam(string appKey, string appSecret)
{
var ts = GetTimeStamp();
return "appKey=" + appKey + "&requestTime=" + ts + "&sign=" + GetSign(ts, appKey, appSecret);
}
private long GetTimeStamp()
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (long)(DateTime.Now - startTime).TotalMilliseconds;
return t;
}
protected string GetSign(long timeStamp, string appKey, string appSecret)
{
var str = appKey + appSecret + timeStamp;
var r = Md5(str).ToLower();
return r;
}
protected string Md5(string str)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] data = Encoding.UTF8.GetBytes(str);
data = md5.ComputeHash(data);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.Append(data[i].ToString("x2"));
}
return sb.ToString();
}
}
}