TestService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using Admin.NET.Application.Const;
  2. using Furion.DatabaseAccessor;
  3. using Furion.Localization;
  4. using Furion.Logging.Extensions;
  5. using Microsoft.AspNetCore.Authorization;
  6. using QuestPDF.Fluent;
  7. using QuestPDF.Helpers;
  8. using QuestPDF.Infrastructure;
  9. using System.IO;
  10. namespace Admin.NET.Application.Serice;
  11. /// <summary>
  12. /// 自己业务服务
  13. /// </summary>
  14. [ApiDescriptionSettings(TestConst.GroupName, Name = "XXX模块", Order = 200)]
  15. [AllowAnonymous]
  16. public class TestService : IDynamicApiController, ITransient
  17. {
  18. private readonly SqlSugarRepository<Test> _testRep;
  19. public TestService(SqlSugarRepository<Test> testRep)
  20. {
  21. _testRep = testRep;
  22. }
  23. /// <summary>
  24. /// 测试
  25. /// </summary>
  26. public string GetName()
  27. {
  28. return "Furion";
  29. }
  30. /// <summary>
  31. /// 获取列表
  32. /// </summary>
  33. /// <returns></returns>
  34. [HttpGet("/test/list")]
  35. public async Task<List<Test>> GetTestList()
  36. {
  37. return await _testRep.GetListAsync();
  38. }
  39. /// <summary>
  40. /// 事务和工作单元测试
  41. /// </summary>
  42. /// <returns></returns>
  43. [HttpGet("/test/list2")]
  44. [UnitOfWork]
  45. public async Task<List<Test>> TestUnitOfWork()
  46. {
  47. await _testRep.InsertAsync(new Test() { Name = "admin" });
  48. throw new Exception("异常");
  49. return await _testRep.GetListAsync();
  50. }
  51. /// <summary>
  52. /// 多语言测试
  53. /// </summary>
  54. /// <returns></returns>
  55. public string TestCulture()
  56. {
  57. "ddd".LogWarning();
  58. //L.SetCulture("zh-CN");
  59. //var a = L.GetSelectCulture();
  60. //var a1 = L.Text["API Interfaces"];
  61. //return $"当前语言【{a.Culture.Name}】 {a1}";
  62. L.SetCulture("en-US");
  63. var b = L.GetSelectCulture();
  64. var b1 = L.Text["API 接口"];
  65. return $"当前语言【{b.Culture.Name}】 {b1}";
  66. }
  67. /// <summary>
  68. /// 测试 Markdown
  69. /// </summary>
  70. /// <remarks>
  71. /// # 先知 / Furion ([探索版](https://gitee.com/dotnetchina/Furion/tree/experimental/))
  72. ///
  73. /// 一个应用程序框架,您可以将它集成到任何.NET/C# 应用程序中。
  74. ///
  75. /// An application framework that you can integrate into any.NET/C# application.
  76. ///
  77. /// ## 安装 / Installation
  78. ///
  79. /// - [Package Manager] (https://www.nuget.org/packages/Furion)
  80. ///
  81. /// ```powershell
  82. /// Install-Package Furion
  83. /// ```
  84. ///
  85. /// - [.NET CLI] (https://www.nuget.org/packages/Furion)
  86. ///
  87. /// ```powershell
  88. /// dotnet add package Furion
  89. /// ```
  90. ///
  91. /// ## 例子 / Examples
  92. ///
  93. /// 我们在[主页](https://dotnetchina.gitee.io/furion)上有不少例子,这是让您入门的第一个:
  94. ///
  95. /// We have several examples [on the website] (https://dotnetchina.gitee.io/furion). Here is the first one to get you started:
  96. ///
  97. /// ```cs
  98. /// Serve.Run();
  99. ///
  100. /// [DynamicApiController]
  101. /// public class HelloService
  102. /// {
  103. /// public string Say()
  104. /// {
  105. /// return "Hello, Furion";
  106. /// }
  107. /// }
  108. /// ```
  109. ///
  110. /// 打开浏览器访问 `https://localhost:5001`。
  111. ///
  112. /// Open browser access `https://localhost:5001`.
  113. ///
  114. /// ## 文档 / Documentation
  115. ///
  116. /// 您可以在[主页] (https://dotnetchina.gitee.io/furion)或[备份主页](https://furion.icu)找到 Furion 文档。
  117. ///
  118. /// You can find the Furion documentation[on the website](https://dotnetchina.gitee.io/furion) or [on the backup website](https://furion.icu).
  119. ///
  120. /// ## 贡献 / Contributing
  121. ///
  122. /// 该存储库的主要目的是继续发展 Furion 核心,使其更快、更易于使用。 Furion 的开发在[Gitee](https://gitee.com/dotnetchina/Furion) 上公开进行,我们感谢社区贡献错误修复和改进。
  123. ///
  124. /// 阅读[贡献指南] (https://dotnetchina.gitee.io/furion/docs/contribute)内容,了解如何参与改进 Furion。
  125. ///
  126. /// The main purpose of this repository is to continue evolving Furion core, making it faster and easier to use.Development of Furion happens in the open on[Gitee] (https://gitee.com/dotnetchina/Furion), and we are grateful to the community for contributing bugfixes and improvements.
  127. ///
  128. /// Read[contribution documents] (https://dotnetchina.gitee.io/furion/docs/contribute) to learn how you can take part in improving Furion.
  129. ///
  130. /// ## 许可证 / License
  131. ///
  132. /// Furion 采用[MulanPSL - 2.0](https://gitee.com/dotnetchina/Furion/blob/master/LICENSE) 开源许可证。
  133. ///
  134. /// Furion uses the[MulanPSL - 2.0] (https://gitee.com/dotnetchina/Furion/blob/master/LICENSE) open source license.
  135. ///
  136. /// ```
  137. /// Copyright(c) 2020-2022 百小僧, Baiqian Co., Ltd.
  138. /// Furion is licensed under Mulan PSL v2.
  139. /// You can use this software according to the terms andconditions of the Mulan PSL v2.
  140. /// You may obtain a copy of Mulan PSL v2 at:
  141. /// https://gitee.com/dotnetchina/Furion/blob/master/LICENSE
  142. /// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUTWARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  143. /// See the Mulan PSL v2 for more details.
  144. /// ```
  145. ///
  146. /// </remarks>
  147. /// <returns></returns>
  148. public string GetDescription()
  149. {
  150. return "Furion";
  151. }
  152. /// <summary>
  153. /// 生成PDF文件
  154. /// </summary>
  155. [NonUnify]
  156. public dynamic CreatePDF()
  157. {
  158. var logoPath = App.WebHostEnvironment.WebRootPath + @"\images\logo.png";
  159. byte[] imageByte = null;
  160. using (var fs = new FileStream(logoPath, FileMode.Open, FileAccess.Read))
  161. {
  162. using var br = new BinaryReader(fs);
  163. imageByte = br.ReadBytes((int)fs.Length);
  164. }
  165. var title = "基于Furion/.NET6实现的通用管理平台。整合最新技术,模块插件式开发,前后端分离,开箱即用。集成SqlSugar、多租户、缓存、数据校验、鉴权、事件总线、动态API、通讯、远程请求、任务调度、gRPC等众多黑科技。代码简洁、易扩展,让开发更简单、更通用、更流行!";
  166. var filePath = App.HostEnvironment.ContentRootPath + $"{DateTimeOffset.Now:yyyyMMddHHmmssfff}.PDF";
  167. Document.Create(container =>
  168. {
  169. container.Page(page =>
  170. {
  171. page.Size(PageSizes.A4);
  172. page.Margin(2, Unit.Centimetre);
  173. page.PageColor(Colors.White);
  174. page.DefaultTextStyle(x => x.FontSize(18));
  175. page.Header()
  176. .AlignCenter().Text("Admin.NET")
  177. .SemiBold().FontSize(24).FontColor(Colors.Red.Medium);
  178. page.Content()
  179. .PaddingVertical(1, Unit.Centimetre)
  180. .Column(x =>
  181. {
  182. x.Spacing(30);
  183. x.Item().Text(title).FontFamily("simhei");
  184. x.Item().AlignCenter().Height(100).Width(100).Image(imageByte, ImageScaling.Resize);
  185. });
  186. page.Footer()
  187. .AlignCenter()
  188. .Text(x =>
  189. {
  190. x.Span("Page ");
  191. x.CurrentPageNumber();
  192. });
  193. });
  194. }).GeneratePdf(filePath);
  195. return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = "xxx.PDF" };
  196. }
  197. }