// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! using Hardware.Info; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Controllers; namespace Admin.NET.Core.Service; /// /// 系统通用服务 🧩 /// [ApiDescriptionSettings(Order = 101, Description = "通用接口")] [AllowAnonymous] public class SysCommonService : IDynamicApiController, ITransient { private readonly IApiDescriptionGroupCollectionProvider _apiProvider; private readonly SysCacheService _sysCacheService; private readonly IHttpRemoteService _httpRemoteService; public SysCommonService(IApiDescriptionGroupCollectionProvider apiProvider, SysCacheService sysCacheService, IHttpRemoteService httpRemoteService) { _apiProvider = apiProvider; _sysCacheService = sysCacheService; _httpRemoteService = httpRemoteService; } /// /// 获取国密公钥私钥对 🏆 /// /// [DisplayName("获取国密公钥私钥对")] public SmKeyPairOutput GetSmKeyPair() { return CryptogramUtil.GetSmKeyPair(); } /// /// 获取MD5加密字符串 🏆 /// /// /// /// [DisplayName("获取MD5加密字符串")] public string GetMD5Encrypt(string text, bool uppercase = false) { return MD5Encryption.Encrypt(text, uppercase, is16: false); } /// /// 国密SM2加密字符串 🔖 /// /// /// [DisplayName("国密SM2加密字符串")] public string SM2Encrypt([Required] string plainText) { return CryptogramUtil.SM2Encrypt(plainText); } /// /// 国密SM2解密字符串 🔖 /// /// /// [DisplayName("国密SM2解密字符串")] public string SM2Decrypt([Required] string cipherText) { return CryptogramUtil.SM2Decrypt(cipherText); } /// /// 获取所有接口/动态API 🔖 /// /// /// /// [DisplayName("获取所有接口/动态API")] public List GetApiList([FromQuery] string groupName = "", [FromQuery] bool isAppApi = false) { var apiList = new List(); //// 路由前缀 //var defaultRoutePrefix = App.GetOptions().DefaultRoutePrefix; //var menuIdList = _userManager.SuperAdmin ? new List() : await GetMenuIdList(); // 获取所有接口分组 var apiDescriptionGroups = _apiProvider.ApiDescriptionGroups.Items; foreach (ApiDescriptionGroup group in apiDescriptionGroups) { if (!string.IsNullOrWhiteSpace(groupName) && group.GroupName != groupName) continue; var apiOuput = new ApiOutput { Name = "", Text = string.IsNullOrWhiteSpace(group.GroupName) ? "系统接口" : group.GroupName, Route = "", }; // 获取分组的所有接口 var actions = group.Items; foreach (ApiDescription action in actions) { // 路由 var route = action.RelativePath.Contains('{') ? action.RelativePath[..(action.RelativePath.IndexOf('{') - 1)] : action.RelativePath; // 去掉路由参数 route = route[(route.IndexOf('/') + 1)..]; // 去掉路由前缀 // 接口分组/控制器信息 if (action.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) continue; // 是否只获取所有的移动端/AppApi接口 if (isAppApi) { var appApiDescription = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute(true); if (appApiDescription == null) continue; } var apiDescription = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttribute(true); var controllerName = controllerActionDescriptor.ControllerName; var actionName = controllerActionDescriptor.ActionName; var controllerText = apiDescription?.Description; if (!apiOuput.Children.Exists(u => u.Name == controllerName)) { apiOuput.Children.Add(new ApiOutput { Name = controllerName, Text = string.IsNullOrWhiteSpace(controllerText) ? controllerName : controllerText, Route = "", Order = apiDescription?.Order ?? 0, }); } // 接口信息 var apiController = apiOuput.Children.FirstOrDefault(u => u.Name.Equals(controllerName)); apiDescription = controllerActionDescriptor.MethodInfo.GetCustomAttribute(true); var apiText = apiDescription?.Description; if (string.IsNullOrWhiteSpace(apiText)) apiText = controllerActionDescriptor.MethodInfo.GetCustomAttribute(true)?.DisplayName; apiController.Children.Add(new ApiOutput { Name = "", Text = apiText, Route = route, Action = actionName, HttpMethod = action.HttpMethod, Order = apiDescription?.Order ?? 0, }); // 接口分组/控制器排序 apiOuput.Children = [.. apiOuput.Children.OrderByDescending(u => u.Order)]; } apiList.Add(apiOuput); } return apiList; } /// /// 获取所有移动端接口 🔖 /// /// [DisplayName("获取所有移动端接口")] public List GetAppApiList() { var apiList = _sysCacheService.Get>(CacheConst.KeyAppApi); if (apiList == null) { apiList = []; var allApiList = GetApiList("", true); foreach (var apiOutput in allApiList) { foreach (var controller in apiOutput.Children) apiList.AddRange(controller.Children.Select(u => u.Route)); } _sysCacheService.Set(CacheConst.KeyAppApi, apiList, TimeSpan.FromDays(7)); } return apiList; } /// /// 生成所有移动端接口文件 🔖 /// /// [HttpGet] [DisplayName("生成所有移动端接口文件")] public void GenerateAppApi([FromQuery] string groupName = "", [FromQuery] bool isAppApi = true) { var defaultRoutePrefix = App.GetOptions().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()); } } } /// /// 下载标记错误的临时 Excel(全局) 🔖 /// /// [DisplayName("下载标记错误的临时 Excel")] public async Task DownloadErrorExcelTemp([FromQuery] string fileName = null) { var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value; var resultStream = _sysCacheService.Get(CacheConst.KeyExcelTemp + userId) ?? throw Oops.Oh("错误标记文件已过期。"); return await Task.FromResult(new FileStreamResult(resultStream, "application/octet-stream") { FileDownloadName = $"{(string.IsNullOrEmpty(fileName) ? "错误标记_" + DateTime.Now.ToString("yyyyMMddhhmmss") : fileName)}.xlsx" }); } /// /// 获取机器序列号 🔖 /// /// [DisplayName("获取机器序列号")] public string GetMachineSerialKey() { try { HardwareInfo hardwareInfo = new(); 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}"); } } /// /// 性能压力测试 🔖 /// /// [DisplayName("性能压力测试")] public async Task StressTest(StressTestInput input) { var stressTestHarnessResult = await _httpRemoteService.SendAsync(HttpRequestBuilder.StressTestHarness(input.RequestUri) .SetNumberOfRequests(input.NumberOfRequests) // 并发请求数量 .SetNumberOfRounds(input.NumberOfRounds) // 压测轮次 .SetMaxDegreeOfParallelism(input.MaxDegreeOfParallelism), // 最大并发度 builder => builder.WithHeaders(input.Headers) .WithQueryParameters(input.QueryParameters) .WithPathParameters(input.PathParameters) .SetJsonContent(input.JsonContent)); return stressTestHarnessResult; } /// /// 从 china.sqlite 中获取区划数据 /// /// 区划编码 /// 级数(从当前code所在级别往下级数) /// [DisplayName("从 china.sqlite 中获取区划数据")] public async Task GetRegionTree(string code, int level) { level = level > 5 ? 5 : level; var sqlitePath = "C:\\china.sqlite"; var db = new SqlSugarScope(new ConnectionConfig() { ConnectionString = $"Data Source={sqlitePath};Cache=Shared", DbType = SqlSugar.DbType.Sqlite, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); var regionList = new List(); // 判断编码所属层级 int startLevel = 1; // 省 switch (code.Length) { case 4: startLevel = 2; // 市 break; case 6: startLevel = 3; // 区县 break; case 9: startLevel = 4; // 街道 break; case 12: startLevel = 5; // 社区/村 break; default: break; } var region1List = GetRegionList(code, startLevel, db); if (region1List.Count == 0) return; region1List.ForEach(u => u.Pid = 0); regionList.AddRange(region1List); if (level == 1 || startLevel == 5) goto result; startLevel++; var region2List = new List(); foreach (var item in region1List) { region2List.AddRange(GetRegionList(item.Code, startLevel, db)); } regionList.AddRange(region2List); if (level == 2 || startLevel == 5 || region2List.Count == 0) goto result; startLevel++; var region3List = new List(); foreach (var item in region2List) { region3List.AddRange(GetRegionList(item.Code, startLevel, db)); } regionList.AddRange(region3List); if (level == 3 || startLevel == 5 || region3List.Count == 0) goto result; startLevel++; var region4List = new List(); foreach (var item in region3List) { region4List.AddRange(GetRegionList(item.Code, startLevel, db)); } regionList.AddRange(region4List); if (level == 4 || startLevel == 5 || region4List.Count == 0) goto result; startLevel++; var region5List = new List(); region5List.AddRange(GetVillageList(region4List.Select(u => u.Code).ToList(), db)); regionList.AddRange(region5List); result: // 保存行政区划树 var defaultDbConfig = App.GetOptions().ConnectionConfigs[0]; db = new SqlSugarScope(new ConnectionConfig() { ConnectionString = defaultDbConfig.ConnectionString, DbType = defaultDbConfig.DbType, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); await db.Deleteable().ExecuteCommandAsync(); await db.Insertable(regionList).ExecuteCommandAsync(); } /// /// 根据层级及父级编码获取区域集合 /// /// /// /// /// private static List GetRegionList(string pCode, int level, SqlSugarScope db) { string table = ""; switch (level) { case 1: table = "province"; break; case 2: table = "city"; break; case 3: table = "area"; break; case 4: table = "street"; break; case 5: table = "village"; break; default: break; } if (string.IsNullOrWhiteSpace(table)) return []; var condition = string.IsNullOrWhiteSpace(pCode) || pCode == "0" ? "" : $" and code like '{pCode}%'"; var sql = $"select * from {table} where 1=1 {condition}"; var regions = db.Ado.SqlQuery(sql); if (regions.Count == 0) return []; foreach (var item in regions) { item.Pid = string.IsNullOrWhiteSpace(pCode) || item.Code == pCode || level == 1 ? 0 : Convert.ToInt64(pCode); item.Level = level; item.Id = Convert.ToInt64(item.Code); } return regions; } /// /// 获取社区/村集合 /// /// /// /// private static List GetVillageList(List pCodes, SqlSugarScope db) { var condition = pCodes == null || pCodes.Count == 0 ? "" : $" and streetCode in ('{pCodes.Join("','")}')"; var sql = $"select * from village where 1=1 {condition}"; var regions = db.Ado.SqlQuery(sql); if (regions.Count == 0) return []; var regionList = new List(); foreach (var item in regions) { var region = new SysRegion { Name = item.name, Code = item.code, Pid = Convert.ToInt64(item.streetCode), Level = 5, Id = Convert.ToInt64(item.code) }; regionList.Add(region); } return regionList; } }