2024-06-15 13:02:35 +08:00
|
|
|
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|
|
|
|
|
|
2024-07-16 13:03:18 +08:00
|
|
|
|
using Hardware.Info;
|
2024-06-15 13:02:35 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Admin.NET.Core.Service;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 系统通用服务 🧩
|
|
|
|
|
|
/// </summary>
|
2024-09-05 14:08:51 +08:00
|
|
|
|
[ApiDescriptionSettings(Order = 101, Description = "通用接口")]
|
2024-06-15 13:02:35 +08:00
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public class SysCommonService : IDynamicApiController, ITransient
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IApiDescriptionGroupCollectionProvider _apiProvider;
|
2025-01-02 01:50:12 +08:00
|
|
|
|
private readonly SysCacheService _sysCacheService;
|
|
|
|
|
|
private readonly IHttpRemoteService _httpRemoteService;
|
2024-06-15 13:02:35 +08:00
|
|
|
|
|
2025-01-02 01:50:12 +08:00
|
|
|
|
public SysCommonService(IApiDescriptionGroupCollectionProvider apiProvider, SysCacheService sysCacheService, IHttpRemoteService httpRemoteService)
|
2024-06-15 13:02:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
_apiProvider = apiProvider;
|
2025-01-02 01:50:12 +08:00
|
|
|
|
_sysCacheService = sysCacheService;
|
|
|
|
|
|
_httpRemoteService = httpRemoteService;
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取国密公钥私钥对 🏆
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("获取国密公钥私钥对")]
|
|
|
|
|
|
public SmKeyPairOutput GetSmKeyPair()
|
|
|
|
|
|
{
|
2024-11-21 21:14:39 +08:00
|
|
|
|
return CryptogramUtil.GetSmKeyPair();
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取MD5加密字符串 🏆
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="text"></param>
|
|
|
|
|
|
/// <param name="uppercase"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("获取MD5加密字符串")]
|
|
|
|
|
|
public string GetMD5Encrypt(string text, bool uppercase = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return MD5Encryption.Encrypt(text, uppercase, is16: false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 11:51:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 国密SM2加密字符串 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="plainText"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("国密SM2加密字符串")]
|
|
|
|
|
|
public string SM2Encrypt([Required] string plainText)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CryptogramUtil.SM2Encrypt(plainText);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 国密SM2解密字符串 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cipherText"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("国密SM2解密字符串")]
|
|
|
|
|
|
public string SM2Decrypt([Required] string cipherText)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CryptogramUtil.SM2Decrypt(cipherText);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-15 13:02:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有接口/动态API 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="groupName"></param>
|
2024-10-21 02:15:46 +08:00
|
|
|
|
/// <param name="isAppApi"></param>
|
2024-06-15 13:02:35 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("获取所有接口/动态API")]
|
2024-10-21 02:15:46 +08:00
|
|
|
|
public List<ApiOutput> GetApiList([FromQuery] string groupName = "", [FromQuery] bool isAppApi = false)
|
2024-06-15 13:02:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
var apiList = new List<ApiOutput>();
|
|
|
|
|
|
|
|
|
|
|
|
//// 路由前缀
|
|
|
|
|
|
//var defaultRoutePrefix = App.GetOptions<DynamicApiControllerSettingsOptions>().DefaultRoutePrefix;
|
|
|
|
|
|
|
2024-08-01 12:34:31 +08:00
|
|
|
|
//var menuIdList = _userManager.SuperAdmin ? new List<long>() : await GetMenuIdList();
|
|
|
|
|
|
|
2024-06-15 13:02:35 +08:00
|
|
|
|
// 获取所有接口分组
|
|
|
|
|
|
var apiDescriptionGroups = _apiProvider.ApiDescriptionGroups.Items;
|
|
|
|
|
|
foreach (ApiDescriptionGroup group in apiDescriptionGroups)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(groupName) && group.GroupName != groupName)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var apiOuput = new ApiOutput
|
|
|
|
|
|
{
|
2024-09-05 14:08:51 +08:00
|
|
|
|
Name = "",
|
|
|
|
|
|
Text = string.IsNullOrWhiteSpace(group.GroupName) ? "系统接口" : group.GroupName,
|
2024-06-26 23:32:26 +08:00
|
|
|
|
Route = "",
|
2024-06-15 13:02:35 +08:00
|
|
|
|
};
|
2024-06-26 23:32:26 +08:00
|
|
|
|
// 获取分组的所有接口
|
2024-06-15 13:02:35 +08:00
|
|
|
|
var actions = group.Items;
|
|
|
|
|
|
foreach (ApiDescription action in actions)
|
|
|
|
|
|
{
|
2024-06-26 23:32:26 +08:00
|
|
|
|
// 路由
|
2024-07-22 02:54:41 +08:00
|
|
|
|
var route = action.RelativePath.Contains('{') ? action.RelativePath[..(action.RelativePath.IndexOf('{') - 1)] : action.RelativePath; // 去掉路由参数
|
2024-06-26 23:32:26 +08:00
|
|
|
|
route = route[(route.IndexOf('/') + 1)..]; // 去掉路由前缀
|
2024-06-15 13:02:35 +08:00
|
|
|
|
|
2024-06-26 23:32:26 +08:00
|
|
|
|
// 接口分组/控制器信息
|
2024-12-30 02:20:39 +08:00
|
|
|
|
if (action.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor)
|
2024-07-22 17:37:19 +08:00
|
|
|
|
continue;
|
2024-10-21 02:15:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 是否只获取所有的移动端/AppApi接口
|
|
|
|
|
|
if (isAppApi)
|
|
|
|
|
|
{
|
|
|
|
|
|
var appApiDescription = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute<AppApiDescriptionAttribute>(true);
|
|
|
|
|
|
if (appApiDescription == null) continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-26 23:32:26 +08:00
|
|
|
|
var apiDescription = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute<ApiDescriptionSettingsAttribute>(true);
|
|
|
|
|
|
var controllerName = controllerActionDescriptor.ControllerName;
|
2025-01-19 23:25:16 +08:00
|
|
|
|
var actionName = controllerActionDescriptor.ActionName;
|
2024-09-05 15:55:00 +08:00
|
|
|
|
var controllerText = apiDescription?.Description;
|
2024-06-26 23:32:26 +08:00
|
|
|
|
if (!apiOuput.Children.Exists(u => u.Name == controllerName))
|
|
|
|
|
|
{
|
|
|
|
|
|
apiOuput.Children.Add(new ApiOutput
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = controllerName,
|
2024-09-05 15:55:00 +08:00
|
|
|
|
Text = string.IsNullOrWhiteSpace(controllerText) ? controllerName : controllerText,
|
2024-06-26 23:32:26 +08:00
|
|
|
|
Route = "",
|
|
|
|
|
|
Order = apiDescription?.Order ?? 0,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-06-15 13:02:35 +08:00
|
|
|
|
|
2024-06-26 23:32:26 +08:00
|
|
|
|
// 接口信息
|
|
|
|
|
|
var apiController = apiOuput.Children.FirstOrDefault(u => u.Name.Equals(controllerName));
|
2024-09-06 01:22:59 +08:00
|
|
|
|
apiDescription = controllerActionDescriptor.MethodInfo.GetCustomAttribute<ApiDescriptionSettingsAttribute>(true);
|
|
|
|
|
|
var apiText = apiDescription?.Description;
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(apiText))
|
|
|
|
|
|
apiText = controllerActionDescriptor.MethodInfo.GetCustomAttribute<DisplayNameAttribute>(true)?.DisplayName;
|
2024-06-26 23:32:26 +08:00
|
|
|
|
apiController.Children.Add(new ApiOutput
|
2024-06-15 13:02:35 +08:00
|
|
|
|
{
|
2024-09-05 14:08:51 +08:00
|
|
|
|
Name = "",
|
2024-09-06 01:22:59 +08:00
|
|
|
|
Text = apiText,
|
2024-06-26 23:32:26 +08:00
|
|
|
|
Route = route,
|
2025-01-19 23:25:16 +08:00
|
|
|
|
Action = actionName,
|
2024-06-15 13:02:35 +08:00
|
|
|
|
HttpMethod = action.HttpMethod,
|
2024-06-26 23:32:26 +08:00
|
|
|
|
Order = apiDescription?.Order ?? 0,
|
2024-06-15 13:02:35 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-06-26 23:32:26 +08:00
|
|
|
|
// 接口分组/控制器排序
|
2024-12-30 02:20:39 +08:00
|
|
|
|
apiOuput.Children = [.. apiOuput.Children.OrderByDescending(u => u.Order)];
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
apiList.Add(apiOuput);
|
|
|
|
|
|
}
|
|
|
|
|
|
return apiList;
|
|
|
|
|
|
}
|
2024-07-12 14:29:34 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-12-30 15:38:04 +08:00
|
|
|
|
/// 获取所有移动端接口 🔖
|
2024-10-21 02:15:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("获取所有移动端接口")]
|
|
|
|
|
|
public List<string> GetAppApiList()
|
|
|
|
|
|
{
|
2025-01-02 01:50:12 +08:00
|
|
|
|
var apiList = _sysCacheService.Get<List<string>>(CacheConst.KeyAppApi);
|
2024-10-21 02:15:46 +08:00
|
|
|
|
if (apiList == null)
|
|
|
|
|
|
{
|
2024-12-30 02:20:39 +08:00
|
|
|
|
apiList = [];
|
2024-10-21 02:15:46 +08:00
|
|
|
|
var allApiList = GetApiList("", true);
|
|
|
|
|
|
foreach (var apiOutput in allApiList)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var controller in apiOutput.Children)
|
|
|
|
|
|
apiList.AddRange(controller.Children.Select(u => u.Route));
|
|
|
|
|
|
}
|
2025-01-02 01:50:12 +08:00
|
|
|
|
_sysCacheService.Set(CacheConst.KeyAppApi, apiList, TimeSpan.FromDays(7));
|
2024-10-21 02:15:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
return apiList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-19 23:25:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成所有移动端接口 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
[DisplayName("生成所有移动端接口")]
|
|
|
|
|
|
public void GenerateAppApi([FromQuery] string groupName = "", [FromQuery] bool isAppApi = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
var defaultRoutePrefix = App.GetOptions<DynamicApiControllerSettingsOptions>().DefaultRoutePrefix;
|
|
|
|
|
|
var apiPath = Path.Combine(App.WebHostEnvironment.ContentRootPath, @"App\api");
|
|
|
|
|
|
|
|
|
|
|
|
var allApiList = GetApiList("", false); // 此处暂时获取全部
|
|
|
|
|
|
foreach (var apiOutput in allApiList)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var controller in apiOutput.Children)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 以controller.Name为控制器名称,创建js文件.js
|
|
|
|
|
|
var controllerName = controller.Name;
|
|
|
|
|
|
var filePath = Path.Combine(apiPath, $"{controllerName}.js");
|
|
|
|
|
|
StringBuilder stringBuilder = new();
|
|
|
|
|
|
stringBuilder.Append(@"import { http } from 'uview-plus'");
|
|
|
|
|
|
stringBuilder.AppendLine();
|
|
|
|
|
|
stringBuilder.AppendLine();
|
|
|
|
|
|
foreach (var item in controller.Children)
|
|
|
|
|
|
{
|
|
|
|
|
|
var value = item.HttpMethod.Equals("get", StringComparison.CurrentCultureIgnoreCase) ? "params" : "data";
|
|
|
|
|
|
|
|
|
|
|
|
stringBuilder.Append($@"// {item.Text}");
|
|
|
|
|
|
stringBuilder.AppendLine();
|
|
|
|
|
|
stringBuilder.Append($@"export const {item.Action}Api = ({value}) => http.{item.HttpMethod.ToLower()}('/{defaultRoutePrefix}/{item.Route}', {value})");
|
|
|
|
|
|
stringBuilder.AppendLine();
|
|
|
|
|
|
stringBuilder.AppendLine();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果或文件夹文件不存在则创建,存在则覆盖
|
|
|
|
|
|
if (!Directory.Exists(apiPath))
|
|
|
|
|
|
Directory.CreateDirectory(apiPath);
|
|
|
|
|
|
File.WriteAllText(filePath, stringBuilder.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-21 02:15:46 +08:00
|
|
|
|
/// <summary>
|
2024-07-16 13:03:18 +08:00
|
|
|
|
/// 下载标记错误的临时 Excel(全局) 🔖
|
2024-07-12 14:29:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("下载标记错误的临时 Excel")]
|
|
|
|
|
|
public async Task<IActionResult> DownloadErrorExcelTemp([FromQuery] string fileName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
|
2025-01-02 01:50:12 +08:00
|
|
|
|
var resultStream = _sysCacheService.Get<MemoryStream>(CacheConst.KeyExcelTemp + userId) ?? throw Oops.Oh("错误标记文件已过期。");
|
2024-07-12 14:29:34 +08:00
|
|
|
|
|
|
|
|
|
|
return await Task.FromResult(new FileStreamResult(resultStream, "application/octet-stream")
|
|
|
|
|
|
{
|
2024-07-17 17:37:06 +08:00
|
|
|
|
FileDownloadName = $"{(string.IsNullOrEmpty(fileName) ? "错误标记_" + DateTime.Now.ToString("yyyyMMddhhmmss") : fileName)}.xlsx"
|
2024-07-12 14:29:34 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-07-16 13:03:18 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取机器序列号 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-08-14 02:51:40 +08:00
|
|
|
|
[DisplayName("获取机器序列号")]
|
2024-07-16 13:03:18 +08:00
|
|
|
|
public string GetMachineSerialKey()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-07-16 17:17:17 +08:00
|
|
|
|
HardwareInfo hardwareInfo = new();
|
2024-07-16 13:03:18 +08:00
|
|
|
|
hardwareInfo.RefreshBIOSList(); // 刷新 BIOS 信息
|
|
|
|
|
|
hardwareInfo.RefreshMotherboardList(); // 刷新主板信息
|
|
|
|
|
|
hardwareInfo.RefreshCPUList(false); // 刷新 CPU 信息
|
|
|
|
|
|
|
|
|
|
|
|
var biosSerialNumber = hardwareInfo.BiosList.MinBy(u => u.SerialNumber)?.SerialNumber;
|
|
|
|
|
|
var mbSerialNumber = hardwareInfo.MotherboardList.MinBy(u => u.SerialNumber)?.SerialNumber;
|
|
|
|
|
|
var cpuProcessorId = hardwareInfo.CpuList.MinBy(u => u.ProcessorId)?.ProcessorId;
|
|
|
|
|
|
// 根据 BIOS、主板和 CPU 信息生成 MD5 摘要
|
|
|
|
|
|
var md5Data = MD5Encryption.Encrypt($"{biosSerialNumber}_{mbSerialNumber}_{cpuProcessorId}", true);
|
|
|
|
|
|
var serialKey = $"{md5Data[..8]}-{md5Data[8..16]}-{md5Data[16..24]}-{md5Data[24..]}";
|
|
|
|
|
|
return serialKey;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw Oops.Oh($"获取机器码失败:{ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-22 01:08:20 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 性能压力测试 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("性能压力测试")]
|
|
|
|
|
|
public async Task<StressTestHarnessResult> StressTest(StressTestInput input)
|
|
|
|
|
|
{
|
2025-01-02 01:50:12 +08:00
|
|
|
|
var stressTestHarnessResult = await _httpRemoteService.SendAsync(HttpRequestBuilder.StressTestHarness(input.RequestUri)
|
2024-12-22 01:08:20 +08:00
|
|
|
|
.SetNumberOfRequests(input.NumberOfRequests) // 并发请求数量
|
|
|
|
|
|
.SetNumberOfRounds(input.NumberOfRounds) // 压测轮次
|
2024-12-24 16:06:39 +08:00
|
|
|
|
.SetMaxDegreeOfParallelism(input.MaxDegreeOfParallelism), // 最大并发度
|
|
|
|
|
|
builder => builder.WithHeaders(input.Headers)
|
|
|
|
|
|
.WithQueryParameters(input.QueryParameters)
|
|
|
|
|
|
.WithPathParameters(input.PathParameters)
|
|
|
|
|
|
.SetJsonContent(input.JsonContent));
|
2024-12-22 01:08:20 +08:00
|
|
|
|
return stressTestHarnessResult;
|
|
|
|
|
|
}
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|