4 changed files with 284 additions and 2 deletions
@ -0,0 +1,195 @@ |
|||
|
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Net.Http; |
|||
using System.Net.Http.Headers; |
|||
using System.Security.Cryptography; |
|||
using System.Text; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
using System.Text.RegularExpressions; |
|||
using System.Threading.Tasks; |
|||
using TaskManager.Entity; |
|||
using TaskManager.EntityFramework; |
|||
|
|||
|
|||
|
|||
|
|||
namespace TaskManager.Controllers |
|||
{ |
|||
[AllowAnonymous] |
|||
public class RecurringJobBaseController :ControllerBase, IDoExecute |
|||
{ |
|||
protected string appKey = "8EG566b9bedd2bf46d"; |
|||
protected string appSecret = "48edc4425647425d87f806a1ba492580"; |
|||
protected readonly HttpClient _httpClient; |
|||
protected readonly JobDbContext _jobDbContext; |
|||
protected string Client { set; get; } = "Wood"; |
|||
protected string Path { set; get; } = "/v2/get/supplierProPlaning"; |
|||
protected string Url { set; get; } = "/v2/get/supplierProPlaning"; |
|||
protected virtual string TaskName { set; get; } = "SupplierProPlaning"; |
|||
|
|||
protected readonly LogController _logger; |
|||
|
|||
|
|||
|
|||
|
|||
public RecurringJobBaseController( |
|||
HttpClient httpClient, |
|||
JobDbContext jobDbContext, |
|||
LogController log |
|||
) |
|||
{ |
|||
_httpClient = new HttpClient(); |
|||
_jobDbContext = jobDbContext; |
|||
_logger = log; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 提交给接口
|
|||
/// </summary>
|
|||
/// <param name="url"></param>
|
|||
/// <param name="jsonData">录入DTOJson</param>
|
|||
/// <returns>返回DTOJson</returns>
|
|||
protected async Task<string> Post(string url, string path, string jsonData) |
|||
{ |
|||
|
|||
|
|||
try |
|||
{ |
|||
// 生成签名参数
|
|||
string timeStamp = GetCurrentTimestamp(); |
|||
string nonce = GenerateNonce(); |
|||
var sign = GenerateSign(HttpMethod.Post.Method, path, appKey, appSecret, timeStamp, nonce, jsonData); |
|||
// 构建请求
|
|||
var request = new HttpRequestMessage(HttpMethod.Post, url); |
|||
request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json"); |
|||
request.Headers.Add("appKey", appKey); |
|||
request.Headers.Add("appSecret", appSecret); |
|||
request.Headers.Add("timestamp", timeStamp); |
|||
request.Headers.Add("sign", sign); |
|||
request.Headers.Add("nonce", nonce); |
|||
|
|||
// 发送请求
|
|||
var response = await _httpClient.SendAsync(request); |
|||
response.EnsureSuccessStatusCode(); // 抛出异常处理状态码
|
|||
var str = await response.Content.ReadAsStringAsync(); |
|||
await _logger.AddInfoRemark("test", TaskName,str); |
|||
|
|||
return str; |
|||
} |
|||
catch (HttpRequestException ex) |
|||
{ |
|||
await _logger.AddError(ex.Message, this.TaskName); |
|||
//error = ex.Message;
|
|||
return string.Empty; |
|||
} |
|||
} |
|||
|
|||
private string GenerateSign(string method, string path, string appKey, string appSecret, string timestamp, string nonce, string jsonBody) |
|||
{ |
|||
|
|||
string paramStr = $"method={method.ToUpper()}&path={path}&appKey={appKey}&appSecret={appSecret}×tamp={timestamp}&nonce={nonce}&jsonBody={jsonBody}"; |
|||
return ComputeSHA512(paramStr); |
|||
|
|||
} |
|||
private string ComputeSHA512(string input) |
|||
{ |
|||
using (SHA512 sha512 = SHA512.Create()) |
|||
{ |
|||
byte[] bytes = Encoding.UTF8.GetBytes(input); |
|||
byte[] hash = sha512.ComputeHash(bytes); |
|||
|
|||
StringBuilder builder = new StringBuilder(); |
|||
for (int i = 0; i < hash.Length; i++) |
|||
{ |
|||
builder.Append(hash[i].ToString("x2")); // "x2" 表示小写十六进制
|
|||
} |
|||
return builder.ToString(); |
|||
} |
|||
} |
|||
private string GetCurrentTimestamp() |
|||
{ |
|||
return ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds).ToString(); |
|||
} |
|||
private string GenerateNonce() |
|||
{ |
|||
const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
|||
Random random = new Random(); |
|||
int length = random.Next(10, 51); |
|||
return new string(Enumerable.Repeat(chars, length) |
|||
.Select(s => s[random.Next(s.Length)]).ToArray()); |
|||
} |
|||
|
|||
|
|||
private async Task BeforeExecuteAsync(string uid) |
|||
{ |
|||
await _logger.AddInfo($"{TaskName}开始执行作业{uid}", TaskName); |
|||
} |
|||
private async Task AfterExecuteAsync(string uid) |
|||
{ |
|||
await _logger.AddInfo($"{TaskName}结束执行作业{uid}", TaskName); |
|||
} |
|||
[NonAction] |
|||
public async Task ExecuteAsync(string url, string path, string taskName,string client) |
|||
{ |
|||
var str = DateTime.Now.ToLongTimeString(); |
|||
await BeforeExecuteAsync(str); |
|||
|
|||
await DoExecutingAsync(url, path, taskName,client); |
|||
|
|||
await AfterExecuteAsync(str); |
|||
|
|||
} |
|||
protected async virtual Task DoExecutingAsync(string url, string path, string takName,string client) |
|||
{ |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
protected static string RemoveWhitespace(string input) |
|||
{ |
|||
if (string.IsNullOrEmpty(input)) |
|||
return input; |
|||
// 使用正则表达式移除空格和换行
|
|||
return Regex.Replace(input, @"[\s]+", ""); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
Loading…
Reference in new issue