|
5 | 5 | @using BlazorAdmin.Core.Dynamic |
6 | 6 | @using BlazorAdmin.Data.Entities |
7 | 7 | @using System.ComponentModel.DataAnnotations.Schema |
| 8 | +@using System.IO.Compression |
8 | 9 | @using static BlazorAdmin.Component.Pages.PagePagination |
9 | 10 | @rendermode RenderMode.InteractiveServer |
10 | 11 | @inject IDbContextFactory<BlazorAdminDbContext> _dbFactory |
|
19 | 20 | } |
20 | 21 | </MudSelect> |
21 | 22 | </div> |
22 | | - <MudButton Color="Color.Primary" OnClick="GenerateCode" Class="mr-2">生成代码</MudButton> |
| 23 | + <MudButton Color="Color.Primary" OnClick="GenerateCode" Class="mr-2" Variant="Variant.Filled">生成代码</MudButton> |
| 24 | + <MudButton Color="Color.Primary" OnClick="DownloadZip" Class="mr-2" Variant="Variant.Filled">打包下载</MudButton> |
23 | 25 | </PageHeader> |
24 | 26 |
|
25 | 27 | <MudPaper Class="pa-4 mb-4 flex-1" Style="overflow:auto"> |
|
30 | 32 | @_pageSrc |
31 | 33 | </MudText> |
32 | 34 | } |
33 | | - @if (!string.IsNullOrEmpty(_createDialogSrc)) |
34 | | - { |
35 | | - <MudText Style="white-space: pre; font-family: monospace; " Class="border mt-2"> |
36 | | - @_createDialogSrc |
37 | | - </MudText> |
38 | | - } |
39 | 35 | @if (!string.IsNullOrEmpty(_editDialogSrc)) |
40 | 36 | { |
41 | 37 | <MudText Style="white-space: pre; font-family: monospace; " Class="border mt-2"> |
42 | 38 | @_editDialogSrc |
43 | 39 | </MudText> |
44 | 40 | } |
45 | | - </MudPaper> |
| 41 | +</MudPaper> |
46 | 42 |
|
47 | | - @code { |
| 43 | +@code { |
48 | 44 |
|
49 | 45 | private List<Type> entityTypes = new(); |
50 | 46 | private Type? selectedEntity; |
51 | 47 |
|
52 | 48 | private string _pageSrc = string.Empty; |
53 | 49 |
|
54 | | - private string _createDialogSrc = string.Empty; |
55 | | - |
56 | 50 | private string _editDialogSrc = string.Empty; |
57 | 51 |
|
58 | 52 | protected override async Task OnInitializedAsync() |
|
128 | 122 | @using BlazorAdmin.Core.Dynamic |
129 | 123 | @using BlazorAdmin.Data.Entities |
130 | 124 | @using static BlazorAdmin.Component.Pages.PagePagination |
| 125 | +@using BlazorAdmin.Component.Dialogs |
131 | 126 |
|
132 | 127 | @using {selectedEntity.Namespace} |
133 | 128 |
|
|
140 | 135 | <MudIconButton Size=""MudBlazor.Size.Medium"" Icon = ""@Icons.Material.Filled.SearchOff"" |
141 | 136 | OnClick=""SearchReset"" Variant = ""Variant.Outlined"" Color = ""Color.Primary"" ></MudIconButton > |
142 | 137 | </MudTooltip> |
| 138 | + <MudIconButton Size=""MudBlazor.Size.Medium"" Icon =""@Icons.Material.Filled.Add"" OnClick=""AddClick"" |
| 139 | + Variant=""Variant.Outlined"" Color=""Color.Primary"" > |
| 140 | + </MudIconButton> |
143 | 141 | </PageHeader> |
144 | 142 |
|
145 | 143 | <MudDataGrid Dense=PageDataGridConfig.Dense |
|
160 | 158 | {tableColumns} |
161 | 159 | <TemplateColumn Title=""操作"" HeaderStyle="" white-space:nowrap;""> |
162 | 160 | <CellTemplate> |
| 161 | + <MudIconButton Size=""@MudBlazor.Size.Small"" Icon =""@Icons.Material.Outlined.Delete"" Color=""Color.Error"" |
| 162 | + OnClick=""() =>DeleteClick(context.Item.Id)"" /> |
| 163 | + <MudIconButton Size=""@MudBlazor.Size.Small"" Icon =""@Icons.Material.Outlined.Edit"" Color=""Color.Primary"" |
| 164 | + OnClick=""() =>EditClick(context.Item.Id)"" /> |
163 | 165 | </CellTemplate> |
164 | 166 | </TemplateColumn> |
165 | 167 | </Columns> |
|
224 | 226 | dataGrid.ReloadServerData(); |
225 | 227 | }} |
226 | 228 |
|
| 229 | + private async Task AddClick() |
| 230 | + {{ |
| 231 | + var parameters = new DialogParameters<Edit{entityName}Dialog> {{ }}; |
| 232 | + var options = new DialogOptions() {{CloseButton = true,MaxWidth = MaxWidth.ExtraLarge}}; |
| 233 | + var dialog = await _dialogService.ShowAsync<Edit{entityName}Dialog>(""创建"", parameters, options); |
| 234 | + var result = await dialog.Result; |
| 235 | + if (!result.Canceled) |
| 236 | + {{ |
| 237 | + _snackbarService.Add(""创建成功"", Severity.Success); |
| 238 | + await dataGrid.ReloadServerData(); |
| 239 | + }} |
| 240 | + }} |
| 241 | +
|
| 242 | + private async Task EditClick(int id) |
| 243 | + {{ |
| 244 | + var parameters = new DialogParameters<Edit{entityName}Dialog> |
| 245 | + {{ |
| 246 | + {{ x=> x.Id, id}} |
| 247 | + }}; |
| 248 | + var options = new DialogOptions() {{CloseButton = true,MaxWidth = MaxWidth.ExtraLarge}}; |
| 249 | + var dialog = await _dialogService.ShowAsync<Edit{entityName}Dialog>(""编辑"", parameters, options); |
| 250 | + var result = await dialog.Result; |
| 251 | + if (!result.Canceled) |
| 252 | + {{ |
| 253 | + _snackbarService.Add(""修改成功"", Severity.Success); |
| 254 | + await dataGrid.ReloadServerData(); |
| 255 | + }} |
| 256 | + }} |
| 257 | +
|
| 258 | + private async Task DeleteClick(int id) |
| 259 | + {{ |
| 260 | + await _dialogService.ShowDeleteDialog(""确定是否删除"", null, |
| 261 | + async (e) => |
| 262 | + {{ |
| 263 | + using var context = await _dbFactory.CreateDbContextAsync(); |
| 264 | + var data = context.Set<{entityName}>().Find(id); |
| 265 | + if (data != null) |
| 266 | + {{ |
| 267 | + context.Remove(data); |
| 268 | + context.SaveChanges(); |
| 269 | +
|
| 270 | + _snackbarService.Add(""删除成功!"", Severity.Success); |
| 271 | + }} |
| 272 | + else |
| 273 | + {{ |
| 274 | + _snackbarService.Add(""信息不存在!"", Severity.Error); |
| 275 | + }} |
| 276 | + await dataGrid.ReloadServerData(); |
| 277 | + }}); |
| 278 | + }} |
| 279 | +
|
227 | 280 | private record SearchObject : PaginationModel |
228 | 281 | {{ |
229 | 282 | {searchProperties} |
|
242 | 295 | var formFields = string.Join("\n", properties.Select(p => |
243 | 296 | $" <MudTextField T=\"{GetCSharpTypeName(p.PropertyType)}\" @bind-Value=\"EditModel.{p.Name}\"\n " + |
244 | 297 | $" Label=\"{p.Name}\" Variant=\"Variant.Outlined\" For=\"()=>EditModel.{p.Name}\"/>")); |
245 | | - |
| 298 | + |
246 | 299 | var setEditModelProperties = string.Join(",\n", properties |
247 | 300 | .Select(p => |
248 | 301 | $" {p.Name} = entity.{p.Name}")); |
|
320 | 373 | }} |
321 | 374 | }}"; |
322 | 375 |
|
323 | | - _createDialogSrc = createDialogCode; |
324 | 376 | _editDialogSrc = createDialogCode; |
325 | 377 |
|
326 | 378 | } |
327 | 379 |
|
328 | | - private record SearchObject : PaginationModel |
| 380 | + private async Task DownloadZip() |
329 | 381 | { |
330 | | - } |
| 382 | + if (selectedEntity == null || string.IsNullOrEmpty(_pageSrc) || string.IsNullOrEmpty(_editDialogSrc)) return; |
| 383 | + |
| 384 | + var entityName = selectedEntity.Name; |
| 385 | + var tempPath = Path.Combine(Path.GetTempPath(), $"CodeGenerator_{DateTime.Now:yyyyMMddHHmmss}"); |
| 386 | + Directory.CreateDirectory(tempPath); |
| 387 | + |
| 388 | + try |
| 389 | + { |
| 390 | + // 保存页面文件 |
| 391 | + var pageFilePath = Path.Combine(tempPath, $"{entityName}Page.razor"); |
| 392 | + await File.WriteAllTextAsync(pageFilePath, _pageSrc); |
| 393 | + |
| 394 | + // 保存对话框文件 |
| 395 | + var dialogFilePath = Path.Combine(tempPath, $"Edit{entityName}Dialog.razor"); |
| 396 | + await File.WriteAllTextAsync(dialogFilePath, _editDialogSrc); |
| 397 | + |
| 398 | + // 创建zip文件 |
| 399 | + var zipPath = Path.Combine(Path.GetTempPath(), $"{entityName}_{DateTime.Now:yyyyMMddHHmmss}.zip"); |
| 400 | + ZipFile.CreateFromDirectory(tempPath, zipPath); |
| 401 | + |
| 402 | + // 读取zip文件并转换为base64字符串 |
| 403 | + var zipBytes = await File.ReadAllBytesAsync(zipPath); |
| 404 | + var base64String = Convert.ToBase64String(zipBytes); |
| 405 | + |
| 406 | + // 通过JavaScript触发下载 |
| 407 | + var commonModule = await _jsRuntime.InvokeAsync<IJSObjectReference>("import", "./js/common.js"); |
| 408 | + await commonModule.InvokeVoidAsync("downloadFileFromBase64", base64String, $"{entityName}_Code.zip"); |
331 | 409 |
|
| 410 | + // 清理临时文件 |
| 411 | + Directory.Delete(tempPath, true); |
| 412 | + File.Delete(zipPath); |
| 413 | + } |
| 414 | + catch (Exception ex) |
| 415 | + { |
| 416 | + _snackbarService.Add($"下载失败:{ex.Message}", Severity.Error); |
| 417 | + } |
| 418 | + } |
332 | 419 | private class EntityInfo |
333 | 420 | { |
334 | 421 | public string EntityName { get; set; } = string.Empty; |
|
0 commit comments