Skip to content

Commit

Permalink
[日常提交]:完善blazor模块---用户修改
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamXu96 committed Dec 24, 2021
1 parent 71bd4e7 commit d82789f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 24 deletions.
5 changes: 5 additions & 0 deletions WebApp/Blazor/Blazor.App/Dtos/PagedResultDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public class PagedResultDto<T> where T : class

public List<T> Items { get; set; }
}

public class ListResultDto<T> where T : class
{
public List<T> Items { get; set; }
}
}
5 changes: 4 additions & 1 deletion WebApp/Blazor/Blazor.App/Pages/Users.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@attribute [Authorize]

<Table TItem="UserDto" Items="@Items" HeaderStyle="TableHeaderStyle.Light" IsBordered="true" IsMultipleSelect="true"
ClickToSelect="true" ShowToolbar="true" ShowEditButton="false" OnQueryAsync="@OnQueryAsync" OnAddAsync="@OnAddAsync"
ClickToSelect="true" ShowToolbar="true" OnQueryAsync="@OnQueryAsync" OnAddAsync="@OnAddAsync"
OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync" ShowExtendButtons="true">
<TableColumns>
<TableColumn @bind-Field="@context.Id" Width="180" />
Expand Down Expand Up @@ -32,6 +32,9 @@
<DateTimePicker @bind-Value="@context.CreationTime">
</DateTimePicker>
</div>
<div class="col-12 col-sm-6">
<MultiSelect TValue="string" Items="@RoleItems" @bind-Value="@context.RoleNames"></MultiSelect>
</div>
</div>
</EditTemplate>
</Table>
134 changes: 111 additions & 23 deletions WebApp/Blazor/Blazor.App/Pages/Users.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,49 @@ public partial class Users : ComponentBase
[NotNull]
private List<UserDto>? Items { get; set; }

private List<SelectedItem> RoleItems { get; set; } = new List<SelectedItem>();

private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };

[Inject]
[NotNull]
public MessageService? MessageService { get; set; }

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
var result = await Http.GetFromJsonAsync<PagedResultDto<UserDto>>("/api/base/user");
var roles = await GetRoleListAsync();
Items = result.Items;
//Items = new List<UserDto>()
//{
// new UserDto
// {
// UserName="Aa",
// Name="a",
// Email="[email protected]",
// PhoneNumber="123",
// CreationTime=DateTime.Now
// }
//};
//var content = JsonContent.Create(new BookDto());
//var a = await Http.PostAsync("", content);
foreach (var role in roles)
{
RoleItems.Add(new SelectedItem
{
Text = role.Name,
Value = role.Name
});
}
}

private static Task<UserDto> OnAddAsync() => Task.FromResult(new UserDto());

private async Task<bool> OnDeleteAsync(IEnumerable<UserDto> items)
{
if (items.Count() > 1)
{
await MessageService.Show(new MessageOption()
{
Content = "暂不支持多选删除",
Icon = "fa fa-info-circle",
Color = Color.Warning
});

return false;
}
var result = await Http.DeleteAsync("/api/identity/users/" + items.First().Id);
return true;
}

private async Task<bool> OnSaveAsync(UserDto item, ItemChangedType changedType)
{
if (changedType == ItemChangedType.Add)
Expand All @@ -57,7 +76,6 @@ private async Task<bool> OnSaveAsync(UserDto item, ItemChangedType changedType)
}
else
{
//var oldItem = Items.FirstOrDefault(i => i.Id == item.Id);
//if (oldItem != null)
//{
// oldItem.Name = item.Name;
Expand All @@ -67,6 +85,12 @@ private async Task<bool> OnSaveAsync(UserDto item, ItemChangedType changedType)
// oldItem.Complete = item.Complete;
// oldItem.Education = item.Education;
//}
//var oldItem = Items.FirstOrDefault(i => i.Id == item.Id);
var jobList = await GetJobListAsync();
var OrgList = await GetOrgListAsync();
var user = await Http.GetFromJsonAsync<UserDto>("/api/base/user/" + item.Id);
var userRoles = await GetUserRoles(item.Id);
//item.RoleNames = string.Join(",", userRoles.Select(_ => _.Name).ToList());
}
return false;

Expand All @@ -76,13 +100,8 @@ private async Task<QueryData<UserDto>> OnQueryAsync(QueryPageOptions options)
{
var result = await Http.GetFromJsonAsync<PagedResultDto<UserDto>>("/api/base/user");
var items = result.Items;

// 设置记录总数
var total = result.TotalCount;

// 内存分页
//items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

return new QueryData<UserDto>()
{
Items = items,
Expand All @@ -93,10 +112,27 @@ private async Task<QueryData<UserDto>> OnQueryAsync(QueryPageOptions options)
};
}

private async Task<bool> OnDeleteAsync(IEnumerable<UserDto> items)
private async Task<List<RoleItem>> GetRoleListAsync()
{
var result = await Http.DeleteAsync("/api/identity/users/" + items.First().Id);
return true;
var result = await Http.GetFromJsonAsync<ListResultDto<RoleItem>>("/api/identity/roles/all");
return new List<RoleItem>(result.Items);
}

private async Task<List<JobItem>> GetJobListAsync()
{
var result = await Http.GetFromJsonAsync<ListResultDto<JobItem>>("/api/base/job/jobs");
return new List<JobItem>(result.Items);
}

private async Task<List<OrgItem>> GetOrgListAsync()
{
var result = await Http.GetFromJsonAsync<ListResultDto<OrgItem>>("/api/base/orgs/loadNodes");
return new List<OrgItem>(result.Items);
}
private async Task<List<RoleItem>> GetUserRoles(Guid id)
{
var result = await Http.GetFromJsonAsync<ListResultDto<RoleItem>>("/api/identity/users/" + id + "/roles");
return new List<RoleItem>(result.Items);
}
}

Expand Down Expand Up @@ -126,7 +162,9 @@ public class UserDto
[AutoGenerateColumn(Ignore = true)]
public bool LockoutEnabled { get; set; }

public List<string> RoleNames { get; set; }
[Display(Name = "角色")]
[AutoGenerateColumn(Ignore = true)]
public string RoleNames { get; set; }

[Display(Name = "密码")]
[AutoGenerateColumn(Ignore = true)]
Expand All @@ -144,4 +182,54 @@ public class UserDto
[AutoGenerateColumn(Ignore = true)]
public DateTime CreationTime { get; set; }
}

public class RoleItem
{
public Guid Id { get; set; }

public string Name { get; set; }

public bool IsDefault { get; set; }

public bool IsStatic { get; set; }

public bool IsPublic { get; set; }
}

public class JobItem
{
public Guid Id { get; set; }

public string Name { get; set; }

public bool Enabled { get; set; }

public int Sort { get; set; }

public string Description { get; set; }
}

public class OrgItem
{
public Guid Id { get; set; }

public int CategoryId { get; set; }

public Guid? Pid { get; set; }

public string Name { get; set; }


public string FullName { get; set; }

public int Sort { get; set; }

public bool Enabled { get; set; }

public bool HasChildren { get; set; }

public bool Leaf { get; set; }

public string Label { get; set; }
}
}

0 comments on commit d82789f

Please sign in to comment.