TestService.cs 7.6 KB

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