parent
f7dd78b4dc
commit
cf81bbc6fd
10 changed files with 506 additions and 8 deletions
@ -0,0 +1,168 @@ |
||||
#region CopyRight |
||||
|
||||
/**************************************************************** |
||||
* Project:健康体检信息管理系统(PEIS) |
||||
* Author:张剑峰 |
||||
* CLR Version:4.0.30319.42000 |
||||
* CreateTime:2023-05-01 14:46:15 |
||||
* Version:v2.0 |
||||
* |
||||
* Description:CA服务调用助手类 |
||||
* |
||||
* History: |
||||
* |
||||
***************************************************************** |
||||
* Copyright @ 云南新八达科技有限公司 2023 All rights reserved |
||||
*****************************************************************/ |
||||
|
||||
#endregion CopyRight |
||||
|
||||
using System; |
||||
using System.Drawing; |
||||
using System.IO; |
||||
using System.Net; |
||||
using System.Text; |
||||
using Newtonsoft.Json; |
||||
using QRCoder; |
||||
|
||||
namespace PEIS.Utils |
||||
{ |
||||
public class CAHelper |
||||
{ |
||||
private static string BaseUrl => Global.CAUrl; |
||||
|
||||
public class LoginResponse |
||||
{ |
||||
public bool Success { get; set; } |
||||
public string Message { get; set; } |
||||
public string SignDataId { get; set; } |
||||
public string QrCode { get; set; } |
||||
} |
||||
|
||||
public class GetSignResultResponse |
||||
{ |
||||
public bool Success { get; set; } |
||||
public string Message { get; set; } |
||||
public string JobStatus { get; set; } |
||||
public string SignResult { get; set; } |
||||
public string SignCert { get; set; } |
||||
public string UserId { get; set; } |
||||
public bool IsLoginSuccess { get; set; } |
||||
} |
||||
|
||||
public class AutoSignResponse |
||||
{ |
||||
public bool Success { get; set; } |
||||
public string Message { get; set; } |
||||
public string SignResult { get; set; } |
||||
public string SignCert { get; set; } |
||||
public string SignDataId { get; set; } |
||||
} |
||||
|
||||
public static LoginResponse Login(string clientType = "PEIS") |
||||
{ |
||||
try |
||||
{ |
||||
string url = $"{BaseUrl}/Client/Login?clientType={clientType}"; |
||||
string response = PostRequest(url, ""); |
||||
return JsonConvert.DeserializeObject<LoginResponse>(response); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
return new LoginResponse |
||||
{ |
||||
Success = false, |
||||
Message = $"请求失败: {ex.Message}" |
||||
}; |
||||
} |
||||
} |
||||
|
||||
public static GetSignResultResponse GetSignResult(string signDataId, string clientType = "PEIS") |
||||
{ |
||||
try |
||||
{ |
||||
string url = $"{BaseUrl}/Client/GetSignResult?clientType={clientType}"; |
||||
string data = $"{{\"SignDataId\":\"{signDataId}\"}}"; |
||||
string response = PostRequest(url, data); |
||||
return JsonConvert.DeserializeObject<GetSignResultResponse>(response); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
return new GetSignResultResponse |
||||
{ |
||||
Success = false, |
||||
Message = $"请求失败: {ex.Message}" |
||||
}; |
||||
} |
||||
} |
||||
|
||||
public static AutoSignResponse AutoSign(string userId, string signToken, string data, string clientType = "PEIS") |
||||
{ |
||||
try |
||||
{ |
||||
string url = $"{BaseUrl}/Client/AutoSign?clientType={clientType}"; |
||||
string requestData = $"{{\"UserId\":\"{userId}\",\"SignToken\":\"{signToken}\",\"Data\":\"{data}\"}}"; |
||||
string response = PostRequest(url, requestData); |
||||
return JsonConvert.DeserializeObject<AutoSignResponse>(response); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
return new AutoSignResponse |
||||
{ |
||||
Success = false, |
||||
Message = $"请求失败: {ex.Message}" |
||||
}; |
||||
} |
||||
} |
||||
|
||||
private static string PostRequest(string url, string data) |
||||
{ |
||||
using (var client = new WebClient()) |
||||
{ |
||||
client.Headers[HttpRequestHeader.ContentType] = "application/json"; |
||||
byte[] responseBytes = client.UploadData(url, Encoding.UTF8.GetBytes(data)); |
||||
return Encoding.UTF8.GetString(responseBytes); |
||||
} |
||||
} |
||||
|
||||
public static byte[] GetQrCodeImage(string qrCodeBase64) |
||||
{ |
||||
try |
||||
{ |
||||
if (!string.IsNullOrEmpty(qrCodeBase64)) |
||||
{ |
||||
return Convert.FromBase64String(qrCodeBase64); |
||||
} |
||||
return null; |
||||
} |
||||
catch |
||||
{ |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static Bitmap GenerateQrCodeFromString(string qrContent) |
||||
{ |
||||
try |
||||
{ |
||||
var qr = new QRCodeGenerator(); |
||||
var data = qr.CreateQrCode(qrContent, QRCodeGenerator.ECCLevel.Q); |
||||
var qrCode = new BitmapByteQRCode(data); |
||||
byte[] qrBytes = qrCode.GetGraphic(10); |
||||
|
||||
using (var ms = new MemoryStream(qrBytes)) |
||||
{ |
||||
using (var tempBmp = new Bitmap(ms)) |
||||
{ |
||||
// 强制生成 200x200 正方形(这是核心) |
||||
return new Bitmap(tempBmp, 200, 200); |
||||
} |
||||
} |
||||
} |
||||
catch |
||||
{ |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue