Role.razor 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. @page "/system/role"
  2. @attribute [ReuseTabsPage(Title = "角色管理")]
  3. <Spin Spinning="Loading">
  4. <GridRow Gutter="8">
  5. <GridCol Span="18">
  6. <Table @ref="Table"
  7. TItem="SystemRole"
  8. @bind-PageSize="Ps"
  9. @bind-PageIndex="Pi"
  10. Total="Total"
  11. DataSource="DataSource"
  12. @bind-SelectedRows="SelectedRows"
  13. OnRowClick="OnRowClick"
  14. OnChange="OnChange">
  15. <TitleTemplate>
  16. <Flex Justify="FlexJustify.Start" Gap="@("10")">
  17. <Input Width="300" Placeholder="输入名称" @bind-Value="@Q_Name" />
  18. <Button OnClick="Search">搜索</Button>
  19. <Button OnClick="ResetQuery">重置</Button>
  20. <Button Type="ButtonType.Primary" Color="Color.Green6" OnClick="() => StartEdit(default)">新增</Button>
  21. </Flex>
  22. </TitleTemplate>
  23. <ColumnDefinitions Context="row">
  24. <PropertyColumn Align="ColumnAlign.Center" Property="c=>c.Id" Width="100" Title="ID" />
  25. <PropertyColumn Align="ColumnAlign.Center" Property="c=>c.Name" Title="角色名称" />
  26. <PropertyColumn Align="ColumnAlign.Center" Property="c=>c.RoleType" Title="角色类型">
  27. @{
  28. TagColor color = TagColor.Green;
  29. string tagText = "管理员";
  30. if (row.RoleType == RoleType.System)
  31. {
  32. color = TagColor.Green;
  33. tagText = "系统";
  34. }
  35. else if (row.RoleType == RoleType.Normal)
  36. {
  37. color = TagColor.Blue;
  38. tagText = "一般";
  39. }
  40. else
  41. {
  42. color = TagColor.Magenta;
  43. tagText = "其它";
  44. }
  45. }
  46. <Tag Color="@color">@tagText</Tag>
  47. </PropertyColumn>
  48. <PropertyColumn Align="ColumnAlign.Center" Width="100" Property="c=>c.Enabled" Title="是否启用">
  49. <Switch Checked="@row.Enabled" @bind-Value="@row.Enabled" CheckedChildren="启用" UnCheckedChildren="禁用" OnChange="() => CheckedChanged(row)" />
  50. </PropertyColumn>
  51. <PropertyColumn Align="ColumnAlign.Center" Property="c => c.CreateTime" Title="创建时间">
  52. @{
  53. var formattedTime = row.CreateTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "N/A";
  54. }
  55. @formattedTime
  56. </PropertyColumn>
  57. <ActionColumn Title="操作">
  58. <Button Type="ButtonType.Primary" Color="Color.Blue6" OnClick="() => StartEdit(row)">编辑</Button>
  59. <Button Type="ButtonType.Primary" Color="Color.Red6" Danger OnClick="() => Delete(row)">删除</Button>
  60. </ActionColumn>
  61. </ColumnDefinitions>
  62. <PaginationTemplate>
  63. <Pagination Class="@(context.PaginationClass + " my-custom-pagination")"
  64. Total="context.Total"
  65. PageSize="context.PageSize"
  66. Current="context.PageIndex"
  67. ShowSizeChanger
  68. ShowQuickJumper
  69. ShowTotal="ShowTotal"
  70. OnChange="context.HandlePageChange" />
  71. </PaginationTemplate>
  72. </Table>
  73. </GridCol>
  74. <GridCol Span="6">
  75. <Divider Orientation="DividerOrientation.Left">操作</Divider>
  76. <Space>
  77. <SpaceItem>
  78. <Button OnClick="() => { Tree.CheckAll(); }">全选</Button>
  79. </SpaceItem>
  80. <SpaceItem>
  81. <Button OnClick="() => { Tree.UncheckAll(); }">取消全选</Button>
  82. </SpaceItem>
  83. <SpaceItem>
  84. <Button Type="ButtonType.Primary" Color="Color.Blue6" OnClick="() => UpdateMenu()">提交选择</Button>
  85. </SpaceItem>
  86. </Space>
  87. <Alert Style="margin-top:10px" Message="选择菜单节点前,必须先选择一个角色" Type="AlertType.Warning" ShowIcon="true" />
  88. <Divider Orientation="DividerOrientation.Left">选择菜单</Divider>
  89. <Tree @ref="Tree"
  90. ShowIcon
  91. Checkable
  92. ExpandOnClickNode
  93. DisabledExpression="x=>x.DataItem.Id == 1"
  94. TItem="SystemMenu"
  95. DefaultCheckedKeys="@DefaultCheckedKeys"
  96. CheckedKeys="@CheckedKeys"
  97. DataSource="@Global.Menus"
  98. KeyExpression="x=>x.DataItem.Id.ToString()"
  99. TitleExpression="x => x.DataItem.Name"
  100. ChildrenExpression="x => x.DataItem.Children"
  101. IconExpression="x => x.DataItem.Icon"
  102. CheckableExpression="x=>x.DataItem.Children is null" />
  103. </GridCol>
  104. </GridRow>
  105. </Spin>
  106. @inject ModalService ModalService;
  107. @inject ConfirmService ComfirmService;
  108. @inject IMessageService MessageService;
  109. @code {
  110. private void CheckAll()
  111. {
  112. Tree.CheckAll();
  113. }
  114. private void UncheckAll()
  115. {
  116. Tree.UncheckAll();
  117. }
  118. private void StartEdit(SystemRole row)
  119. {
  120. var data = row ?? new();
  121. ModalRef<bool> modalRef = default;
  122. IForm form = default;
  123. modalRef = ModalService.CreateModal<bool>(new()
  124. {
  125. DestroyOnClose = true,
  126. MaskClosable = false,
  127. Title = data.Id > 0 ? "编辑" : "新增",
  128. Content =
  129. @<Form @ref="form" Model="data" OnFinish="()=> modalRef.OkAsync(true)" LabelColSpan="6" WrapperColSpan="18">
  130. <FormItem Label="角色名称" Required>
  131. <Input @bind-Value="@data.Name" />
  132. </FormItem>
  133. <FormItem Label="是否启用" Required>
  134. <Select @bind-Value="@data.Enabled"
  135. TItemValue="bool"
  136. TItem="string"
  137. DefaultActiveFirstOption="false">
  138. <SelectOptions>
  139. <SelectOption TItemValue="bool" TItem="string" Value="true" Label="启用" />
  140. <SelectOption TItemValue="bool" TItem="string" Value="false" Label="禁用" />
  141. </SelectOptions>
  142. </Select>
  143. </FormItem>
  144. <FormItem Label="角色类型" Required>
  145. <Select @bind-Value="@context.RoleType"
  146. TItemValue="RoleType"
  147. TItem="string"
  148. DefaultActiveFirstOption="true">
  149. <SelectOptions>
  150. @foreach (var roleType in Enum.GetValues(typeof(RoleType)).Cast<RoleType>())
  151. {
  152. <SelectOption TItemValue="RoleType" TItem="string" Value="@roleType" Label="@roleType.Description()" />
  153. }
  154. </SelectOptions>
  155. </Select>
  156. </FormItem>
  157. </Form>,
  158. OnOk = async (e) =>
  159. {
  160. if (!form.Validate())
  161. {
  162. return;
  163. }
  164. modalRef.SetConfirmLoading(true);
  165. var flag = await InsertOrUpdate(data);
  166. if (flag)
  167. {
  168. MessageService.Success("操作成功");
  169. await modalRef.CloseAsync();
  170. Table.ReloadData(Pi, Ps);
  171. StateHasChanged();
  172. }
  173. else
  174. {
  175. MessageService.Error("操作失败");
  176. }
  177. modalRef.SetConfirmLoading(false);
  178. },
  179. OnCancel = async (e) =>
  180. {
  181. if (form.IsModified && (!await Comfirm("已修改内容,确定退出吗?")))
  182. {
  183. return;
  184. }
  185. await modalRef.CloseAsync();
  186. }
  187. });
  188. }
  189. private async Task<bool> Comfirm(string message)
  190. {
  191. return await ComfirmService.Show(message, "提示", ConfirmButtons.YesNo, ConfirmIcon.Warning) == ConfirmResult.Yes;
  192. }
  193. }