翼度科技»论坛 编程开发 .net 查看内容

使用中台 Admin.Core 实现了一个Razor模板的通用代码生成器

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
前言

前面使用 Admin.Core 的代码生成器生成了通用代码生成器的基础模块 分组,模板,项目,项目模型,项目字段的基础功能,本篇继续完善,实现最核心的模板生成功能,并提供生成预览及代码文件压缩下载
准备

首先清楚几个模块的关系,如何使用,简单画一个流程图

前面完成了基础的模板组,模板管理,项目,模型,字段管理,都是由 Admin.Core 框架的代码生成器完成,感兴趣的可参考前篇使用,文末也会给出仓库地址,有问题欢迎交流

本文主要分享项目代码的生成,先放出效果图

  • 项目生成管理,支持多模板组


  • 模板生成预览,预览页可以直接编辑模板


  • 模板生成,将生成压缩包并下载

实现

需要实现上面效果的代码生成器,基础的增删改查都可以借由代码生成器生成,也写过几篇了这里就不再赘述,我们只需要关注核心部分:如何生成?如何预览?如何下载?
根据模板生成对应项目的代码文件

当我们有了一个模板(模板内容),也有了对应的配置项(项目&项目模型&项目字段),中间肯定是需要一个模板引擎来将模板根据配置项解析出来的。
市面上有很多模板引擎,比如 ejs,art 等,但既然是搞C#的,那不妨试试看 Razor (之前的Admin.Core代码生成器也是基于razor模板引擎,也可以换其他的或者支持多种模板引擎),C#的语法写起来还是挺舒服的,并且其实还可以新建一个.net core 的项目添加页面后可以复制模板和模型到页面为自己的模板增加智能提示
模板引擎的使用


  • 项目中引用包:RazorEngine.NetCore

    • 原版只支持 framework,大佬打包的 netcore 版本

  1. <ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup>
复制代码

  • 使用方式:指定模型,内容,模板名称即可
  1. var code="模板内容";
  2. var key="模板名";
  3. //模型名称
  4. var model=new DevProjectRazorRenderModel();
  5. RazorEngine.Engine.Razor.RunCompile(new LoadedTemplateSource(code), key, model.GetType(), model);
复制代码

  • 模板内容的写法

    • 我这里定义了固定模型,所以需要先什么模型的变量,这里指定了 gen
    • 模板语法文档

  1. @{
  2. var gen = Model as ZhonTai.Module.Dev.DevProjectRazorRenderModel;
  3. }
复制代码

  • 这里定义了一个公共的项目模型,后续增加项目模型字段的信息都无需更搞代码,生成即可,另外也可以再属性模型中添加字典等属性,即可灵魂的再模板中使用动态配置了
  1. //模型渲染var gen = new DevProjectRazorRenderModel(){<ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup><ItemGroup>
  4.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  5. </ItemGroup>Project = Mapper.Map(project),<ItemGroup>
  6.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  7. </ItemGroup><ItemGroup>
  8.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  9. </ItemGroup>Model = Mapper.Map(model),<ItemGroup>
  10.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  11. </ItemGroup><ItemGroup>
  12.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  13. </ItemGroup>Fields = Mapper.Map(modelFields),};
复制代码
模板内容的生成


  • 这里分为了两部分,一个是文件路径,一个是文件内容
  • 因为要生成的路径可能也会包含一些模块或者模型的信息,所以可以将模板路径也使用模板生成,这里直接拼接一个模板即可
  1. var pathCodeText = @"@{
  2. var gen = Model as ZhonTai.Module.Dev.DevProjectRazorRenderModel;
  3. }" + outPath;//转换路径var outPath = RazorCompile(gen, $"{project.Code}_{model.Code}_{tpl.Name}_Path.tpl", pathCodeText).Trim();
复制代码
文末附完整代码
内容文件的下载


  • 因为是多模板组多模板,所以每次生成项目代码都基本是多个文件,一个个下载很明显不合理,所以可以将所有代码内容文件打包成压缩包进行下载,下面是核心压缩代码,无需引用包,暂时只在Windows中测试使用
  • 完整代码如下,做了一些文件和文件夹的判断处理,可自行封装
  • 通过GenerateAsync获取到文件信息后写入文件,再将目录进行打包返回即可
  1. /// /// 下载/// /// /// [HttpPost]public async Task DownAsync(DevProjectGenGenerateInput input){<ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup><ItemGroup>
  4.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  5. </ItemGroup>var path = Path.Combine(AppContext.BaseDirectory, "DownCodes", DateTime.Now.ToString("yyyyMMddHHmmss"));<ItemGroup>
  6.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  7. </ItemGroup><ItemGroup>
  8.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  9. </ItemGroup>var zipFileName = $"源码{DateTime.Now.ToString("yyyyMMddHHmmss")}.zip";<ItemGroup>
  10.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  11. </ItemGroup><ItemGroup>
  12.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  13. </ItemGroup>var zipPath = Path.Combine(AppContext.BaseDirectory, "DownCodes", zipFileName);<ItemGroup>
  14.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  15. </ItemGroup><ItemGroup>
  16.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  17. </ItemGroup>try<ItemGroup>
  18.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  19. </ItemGroup><ItemGroup>
  20.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  21. </ItemGroup>{<ItemGroup>
  22.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  23. </ItemGroup><ItemGroup>
  24.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  25. </ItemGroup><ItemGroup>
  26.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  27. </ItemGroup><ItemGroup>
  28.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  29. </ItemGroup>if (!Directory.Exists(path))<ItemGroup>
  30.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  31. </ItemGroup><ItemGroup>
  32.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  33. </ItemGroup><ItemGroup>
  34.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  35. </ItemGroup><ItemGroup>
  36.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  37. </ItemGroup>{<ItemGroup>
  38.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  39. </ItemGroup><ItemGroup>
  40.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  41. </ItemGroup><ItemGroup>
  42.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  43. </ItemGroup><ItemGroup>
  44.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  45. </ItemGroup><ItemGroup>
  46.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  47. </ItemGroup><ItemGroup>
  48.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  49. </ItemGroup>Directory.CreateDirectory(path);<ItemGroup>
  50.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  51. </ItemGroup><ItemGroup>
  52.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  53. </ItemGroup><ItemGroup>
  54.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  55. </ItemGroup><ItemGroup>
  56.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  57. </ItemGroup>}<ItemGroup>
  58.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  59. </ItemGroup><ItemGroup>
  60.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  61. </ItemGroup><ItemGroup>
  62.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  63. </ItemGroup><ItemGroup>
  64.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  65. </ItemGroup>//获取内容信息<ItemGroup>
  66.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  67. </ItemGroup><ItemGroup>
  68.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  69. </ItemGroup><ItemGroup>
  70.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  71. </ItemGroup><ItemGroup>
  72.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  73. </ItemGroup>var codes = await GenerateAsync(input);<ItemGroup>
  74.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  75. </ItemGroup><ItemGroup>
  76.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  77. </ItemGroup><ItemGroup>
  78.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  79. </ItemGroup><ItemGroup>
  80.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  81. </ItemGroup>foreach (var code in codes)<ItemGroup>
  82.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  83. </ItemGroup><ItemGroup>
  84.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  85. </ItemGroup><ItemGroup>
  86.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  87. </ItemGroup><ItemGroup>
  88.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  89. </ItemGroup>{<ItemGroup>
  90.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  91. </ItemGroup><ItemGroup>
  92.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  93. </ItemGroup><ItemGroup>
  94.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  95. </ItemGroup><ItemGroup>
  96.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  97. </ItemGroup><ItemGroup>
  98.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  99. </ItemGroup><ItemGroup>
  100.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  101. </ItemGroup>var codePath = Path.Combine(path, code.Path);<ItemGroup>
  102.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  103. </ItemGroup><ItemGroup>
  104.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  105. </ItemGroup><ItemGroup>
  106.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  107. </ItemGroup><ItemGroup>
  108.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  109. </ItemGroup><ItemGroup>
  110.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  111. </ItemGroup><ItemGroup>
  112.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  113. </ItemGroup>var directory = Path.GetDirectoryName(codePath);<ItemGroup>
  114.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  115. </ItemGroup><ItemGroup>
  116.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  117. </ItemGroup><ItemGroup>
  118.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  119. </ItemGroup><ItemGroup>
  120.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  121. </ItemGroup><ItemGroup>
  122.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  123. </ItemGroup><ItemGroup>
  124.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  125. </ItemGroup>if (!Directory.Exists(directory))<ItemGroup>
  126.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  127. </ItemGroup><ItemGroup>
  128.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  129. </ItemGroup><ItemGroup>
  130.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  131. </ItemGroup><ItemGroup>
  132.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  133. </ItemGroup><ItemGroup>
  134.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  135. </ItemGroup><ItemGroup>
  136.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  137. </ItemGroup>{<ItemGroup>
  138.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  139. </ItemGroup><ItemGroup>
  140.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  141. </ItemGroup><ItemGroup>
  142.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  143. </ItemGroup><ItemGroup>
  144.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  145. </ItemGroup><ItemGroup>
  146.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  147. </ItemGroup><ItemGroup>
  148.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  149. </ItemGroup><ItemGroup>
  150.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  151. </ItemGroup><ItemGroup>
  152.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  153. </ItemGroup>Directory.CreateDirectory(directory);<ItemGroup>
  154.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  155. </ItemGroup><ItemGroup>
  156.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  157. </ItemGroup><ItemGroup>
  158.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  159. </ItemGroup><ItemGroup>
  160.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  161. </ItemGroup><ItemGroup>
  162.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  163. </ItemGroup><ItemGroup>
  164.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  165. </ItemGroup>}<ItemGroup>
  166.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  167. </ItemGroup><ItemGroup>
  168.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  169. </ItemGroup><ItemGroup>
  170.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  171. </ItemGroup><ItemGroup>
  172.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  173. </ItemGroup><ItemGroup>
  174.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  175. </ItemGroup><ItemGroup>
  176.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  177. </ItemGroup>if (!File.Exists(codePath))<ItemGroup>
  178.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  179. </ItemGroup><ItemGroup>
  180.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  181. </ItemGroup><ItemGroup>
  182.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  183. </ItemGroup><ItemGroup>
  184.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  185. </ItemGroup><ItemGroup>
  186.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  187. </ItemGroup><ItemGroup>
  188.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  189. </ItemGroup>{<ItemGroup>
  190.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  191. </ItemGroup><ItemGroup>
  192.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  193. </ItemGroup><ItemGroup>
  194.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  195. </ItemGroup><ItemGroup>
  196.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  197. </ItemGroup><ItemGroup>
  198.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  199. </ItemGroup><ItemGroup>
  200.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  201. </ItemGroup><ItemGroup>
  202.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  203. </ItemGroup><ItemGroup>
  204.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  205. </ItemGroup>using (var fs = File.Open(codePath, FileMode.Create, FileAccess.ReadWrite))<ItemGroup>
  206.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  207. </ItemGroup><ItemGroup>
  208.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  209. </ItemGroup><ItemGroup>
  210.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  211. </ItemGroup><ItemGroup>
  212.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  213. </ItemGroup><ItemGroup>
  214.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  215. </ItemGroup><ItemGroup>
  216.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  217. </ItemGroup><ItemGroup>
  218.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  219. </ItemGroup><ItemGroup>
  220.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  221. </ItemGroup>{<ItemGroup>
  222.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  223. </ItemGroup><ItemGroup>
  224.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  225. </ItemGroup><ItemGroup>
  226.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  227. </ItemGroup><ItemGroup>
  228.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  229. </ItemGroup><ItemGroup>
  230.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  231. </ItemGroup><ItemGroup>
  232.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  233. </ItemGroup><ItemGroup>
  234.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  235. </ItemGroup><ItemGroup>
  236.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  237. </ItemGroup><ItemGroup>
  238.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  239. </ItemGroup><ItemGroup>
  240.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  241. </ItemGroup>await fs.WriteAsync(Encoding.UTF8.GetBytes(code.Content));<ItemGroup>
  242.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  243. </ItemGroup><ItemGroup>
  244.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  245. </ItemGroup><ItemGroup>
  246.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  247. </ItemGroup><ItemGroup>
  248.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  249. </ItemGroup><ItemGroup>
  250.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  251. </ItemGroup><ItemGroup>
  252.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  253. </ItemGroup><ItemGroup>
  254.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  255. </ItemGroup><ItemGroup>
  256.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  257. </ItemGroup>}<ItemGroup>
  258.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  259. </ItemGroup><ItemGroup>
  260.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  261. </ItemGroup><ItemGroup>
  262.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  263. </ItemGroup><ItemGroup>
  264.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  265. </ItemGroup><ItemGroup>
  266.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  267. </ItemGroup><ItemGroup>
  268.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  269. </ItemGroup>}<ItemGroup>
  270.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  271. </ItemGroup><ItemGroup>
  272.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  273. </ItemGroup><ItemGroup>
  274.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  275. </ItemGroup><ItemGroup>
  276.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  277. </ItemGroup>}<ItemGroup>
  278.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  279. </ItemGroup><ItemGroup>
  280.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  281. </ItemGroup><ItemGroup>
  282.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  283. </ItemGroup><ItemGroup>
  284.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  285. </ItemGroup>ZipFile.CreateFromDirectory(path, zipPath);<ItemGroup>
  286.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  287. </ItemGroup><ItemGroup>
  288.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  289. </ItemGroup><ItemGroup>
  290.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  291. </ItemGroup><ItemGroup>
  292.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  293. </ItemGroup>var bytes = await File.ReadAllBytesAsync(zipPath);<ItemGroup>
  294.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  295. </ItemGroup><ItemGroup>
  296.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  297. </ItemGroup><ItemGroup>
  298.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  299. </ItemGroup><ItemGroup>
  300.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  301. </ItemGroup>return new FileContentResult(bytes, "application/zip")<ItemGroup>
  302.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  303. </ItemGroup><ItemGroup>
  304.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  305. </ItemGroup><ItemGroup>
  306.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  307. </ItemGroup><ItemGroup>
  308.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  309. </ItemGroup>{<ItemGroup>
  310.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  311. </ItemGroup><ItemGroup>
  312.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  313. </ItemGroup><ItemGroup>
  314.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  315. </ItemGroup><ItemGroup>
  316.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  317. </ItemGroup><ItemGroup>
  318.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  319. </ItemGroup><ItemGroup>
  320.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  321. </ItemGroup>FileDownloadName = zipFileName<ItemGroup>
  322.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  323. </ItemGroup><ItemGroup>
  324.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  325. </ItemGroup><ItemGroup>
  326.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  327. </ItemGroup><ItemGroup>
  328.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  329. </ItemGroup>};<ItemGroup>
  330.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  331. </ItemGroup><ItemGroup>
  332.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  333. </ItemGroup>}<ItemGroup>
  334.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  335. </ItemGroup><ItemGroup>
  336.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  337. </ItemGroup>finally<ItemGroup>
  338.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  339. </ItemGroup><ItemGroup>
  340.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  341. </ItemGroup>{<ItemGroup>
  342.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  343. </ItemGroup><ItemGroup>
  344.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  345. </ItemGroup><ItemGroup>
  346.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  347. </ItemGroup><ItemGroup>
  348.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  349. </ItemGroup>if (Directory.Exists(path))<ItemGroup>
  350.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  351. </ItemGroup><ItemGroup>
  352.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  353. </ItemGroup><ItemGroup>
  354.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  355. </ItemGroup><ItemGroup>
  356.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  357. </ItemGroup>{<ItemGroup>
  358.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  359. </ItemGroup><ItemGroup>
  360.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  361. </ItemGroup><ItemGroup>
  362.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  363. </ItemGroup><ItemGroup>
  364.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  365. </ItemGroup><ItemGroup>
  366.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  367. </ItemGroup><ItemGroup>
  368.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  369. </ItemGroup>Directory.Delete(path, true);<ItemGroup>
  370.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  371. </ItemGroup><ItemGroup>
  372.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  373. </ItemGroup><ItemGroup>
  374.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  375. </ItemGroup><ItemGroup>
  376.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  377. </ItemGroup>}<ItemGroup>
  378.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  379. </ItemGroup><ItemGroup>
  380.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  381. </ItemGroup><ItemGroup>
  382.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  383. </ItemGroup><ItemGroup>
  384.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  385. </ItemGroup>if (File.Exists(zipPath))<ItemGroup>
  386.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  387. </ItemGroup><ItemGroup>
  388.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  389. </ItemGroup><ItemGroup>
  390.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  391. </ItemGroup><ItemGroup>
  392.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  393. </ItemGroup>{<ItemGroup>
  394.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  395. </ItemGroup><ItemGroup>
  396.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  397. </ItemGroup><ItemGroup>
  398.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  399. </ItemGroup><ItemGroup>
  400.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  401. </ItemGroup><ItemGroup>
  402.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  403. </ItemGroup><ItemGroup>
  404.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  405. </ItemGroup>File.Delete(zipPath);<ItemGroup>
  406.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  407. </ItemGroup><ItemGroup>
  408.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  409. </ItemGroup><ItemGroup>
  410.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  411. </ItemGroup><ItemGroup>
  412.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  413. </ItemGroup>}<ItemGroup>
  414.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  415. </ItemGroup><ItemGroup>
  416.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  417. </ItemGroup>}}
复制代码
前端对应需要支持文件下载,可以修改为生成对应下载URL,用GET请求直接打开新窗口进行下载
项目中前端页面的重点

整个框架主要使用者肯定是.net开发,如果没有写过 vue 项目,写起来的时候可能会有一些吃力,但因为现在有了代码生成器,大部分代码都可以生成,也可以做参考,所以这里只做一些关键点的说明
框架页面菜单的添加说明:具体可参考前文进行创建


  • 系统管理-添加视图->指定 vue 页面文件的路径,可再权限菜单中复用这个视图生成不同的路由地址
  • 接口管理-同步接口->将后端服务映射为权限点,对应后端功能权限
  • 权限管理-添加权限->添加菜单,功能点
  • 用户-角色->通过角色分配角色权限,用户管理角色
Vue 文档

有时间的话至少过一遍再开始,磨刀不误砍柴工
生命周期的一定花时间看看:官方文档
页面中获取路由信息与页面跳转

以项目生成页面调整到预览页面为例


  • 项目生成页和预览页引入定义
  1. //引入路由
  2. import { useRoute, useRouter } from 'vue-router'
  3. //路由信息
  4. const route = useRoute()
  5. //路由跳转
  6. const router = useRouter()
复制代码

  • 跳转到预览页
  1. <ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup>router.push({<ItemGroup>
  4.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  5. </ItemGroup><ItemGroup>
  6.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  7. </ItemGroup>path: '/dev/dev-project-gen/preview', query: {<ItemGroup>
  8.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  9. </ItemGroup><ItemGroup>
  10.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  11. </ItemGroup><ItemGroup>
  12.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  13. </ItemGroup>projectId: row.projectId,<ItemGroup>
  14.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  15. </ItemGroup><ItemGroup>
  16.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  17. </ItemGroup><ItemGroup>
  18.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  19. </ItemGroup>groupIds: row.groupIds_Values<ItemGroup>
  20.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  21. </ItemGroup><ItemGroup>
  22.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  23. </ItemGroup>}<ItemGroup>
  24.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  25. </ItemGroup>})
复制代码

  • 预览页获取生成列表传递的参数
  1. //从路由中获取query参数onMounted(() => {<ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup>state.filter.projectId = route.query.projectId<ItemGroup>
  4.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  5. </ItemGroup>state.filter.groupIds = route.query.groupIds})
复制代码
左侧树/列表右侧预览实现

将左侧的列表列封装为一个组件,右侧如果简单可封装,也可以直接写到预览页面
这里参考框架中用户列表做的
  1. <ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup><ItemGroup>
  4.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  5. </ItemGroup><ItemGroup>
  6.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  7. </ItemGroup><ItemGroup>
  8.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  9. </ItemGroup><ItemGroup>
  10.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  11. </ItemGroup><ItemGroup>
  12.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  13. </ItemGroup><ItemGroup>
  14.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  15. </ItemGroup><ItemGroup>
  16.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  17. </ItemGroup><ItemGroup>
  18.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  19. </ItemGroup><ItemGroup>
  20.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  21. </ItemGroup><ItemGroup>
  22.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  23. </ItemGroup><ItemGroup>
  24.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  25. </ItemGroup><ItemGroup>
  26.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  27. </ItemGroup><ItemGroup>
  28.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  29. </ItemGroup><ItemGroup>
  30.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  31. </ItemGroup><ItemGroup>
  32.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  33. </ItemGroup><ItemGroup>
  34.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  35. </ItemGroup><ItemGroup>
  36.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  37. </ItemGroup><ItemGroup>
  38.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  39. </ItemGroup><ItemGroup>
  40.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  41. </ItemGroup><ItemGroup>
  42.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  43. </ItemGroup><ItemGroup>
  44.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  45. </ItemGroup><ItemGroup>
  46.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  47. </ItemGroup><ItemGroup>
  48.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  49. </ItemGroup>内容预览部分<ItemGroup>
  50.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  51. </ItemGroup><ItemGroup>
  52.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  53. </ItemGroup><ItemGroup>
  54.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  55. </ItemGroup><ItemGroup>
  56.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  57. </ItemGroup><ItemGroup>
  58.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  59. </ItemGroup><ItemGroup>
  60.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  61. </ItemGroup>
复制代码
当然,封装了组件记得引入用到的组件
  1. const GroupTemplateMenu = defineAsyncComponent(() => import('./components/dev-group-template-menu.vue'))
  2. const MyLayout = defineAsyncComponent(() => import('/@/components/my-layout/index.vue'))
复制代码
组件中获取传递的参数示例
  1. interface Props {<ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup>modelValue: number[] | null | undefined<ItemGroup>
  4.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  5. </ItemGroup>selectFirstNode: boolean,<ItemGroup>
  6.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  7. </ItemGroup>projectId: number,<ItemGroup>
  8.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  9. </ItemGroup>groupIds: number[]}const props = withDefaults(defineProps(), {<ItemGroup>
  10.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  11. </ItemGroup>modelValue: () => [],<ItemGroup>
  12.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  13. </ItemGroup>selectFirstNode: false,<ItemGroup>
  14.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  15. </ItemGroup>projectId: 0,<ItemGroup>
  16.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  17. </ItemGroup>groupIds: () => []})
复制代码
在预览左侧菜单中,我们可以看到一个标记的小图标,用来直接编辑模板

这个弹窗其实是直接引用了编辑模板的组件
  1. ......defineExpose({<ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup>open,})
复制代码
如上可以看到我们使用和组件同名的 const devTemplateFormRef = ref() 即可获取到组件引用
另外调用的方法可以查看 dev-template-form.vue 是开放了方法的
  1. defineExpose({<ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup>open,})
复制代码
下载压缩包文件

首先需要后端返回文件流,然后调用对应接口的时候指定format格式为blob,并创建下载连接点击即可
  1. const genCode = async (row: DevProjectGenGetOutput) => {<ItemGroup>
  2.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  3. </ItemGroup>new DevProjectGenApi().down({ projectId: row.projectId, groupIds: row.groupIds_Values?.map(s => Number(s)) }, {<ItemGroup>
  4.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  5. </ItemGroup><ItemGroup>
  6.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  7. </ItemGroup>loading: false,<ItemGroup>
  8.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  9. </ItemGroup><ItemGroup>
  10.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  11. </ItemGroup>showErrorMessage: false,<ItemGroup>
  12.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  13. </ItemGroup><ItemGroup>
  14.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  15. </ItemGroup>format: 'blob'<ItemGroup>
  16.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  17. </ItemGroup>})<ItemGroup>
  18.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  19. </ItemGroup><ItemGroup>
  20.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  21. </ItemGroup>.then((res) => {<ItemGroup>
  22.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  23. </ItemGroup><ItemGroup>
  24.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  25. </ItemGroup><ItemGroup>
  26.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  27. </ItemGroup>const a = document.createElement('a');<ItemGroup>
  28.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  29. </ItemGroup><ItemGroup>
  30.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  31. </ItemGroup><ItemGroup>
  32.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  33. </ItemGroup>a.href = URL.createObjectURL(res as Blob);<ItemGroup>
  34.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  35. </ItemGroup><ItemGroup>
  36.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  37. </ItemGroup><ItemGroup>
  38.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  39. </ItemGroup>a.download = '源码.zip';<ItemGroup>
  40.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  41. </ItemGroup><ItemGroup>
  42.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  43. </ItemGroup><ItemGroup>
  44.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  45. </ItemGroup>a.click();<ItemGroup>
  46.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  47. </ItemGroup><ItemGroup>
  48.   <PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
  49. </ItemGroup>});}
复制代码
后语

本文所有代码皆在 yimogit/Emo.Dev 仓库中可以找到,觉得有用的来个 Star 吧
基于前面代码生成器生成的功能模块上,周末花了两天完善项目生成,终于算是搞定
后面还需要逐步完善生成器,欢迎点个赞,留个言,交流指点一二
相关文档


来源:https://www.cnblogs.com/morang/p/18294868/zhontai_admin_core_module_dev_common
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具