privileges.component.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { ChangeDetectionStrategy, ViewContainerRef, ChangeDetectorRef, Component, OnInit, Inject } from '@angular/core';
  17. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  18. import { I18NService } from '@core';
  19. import { _HttpClient, ALAIN_I18N_TOKEN, SettingsService } from '@delon/theme';
  20. import { format, addDays } from 'date-fns';
  21. import { NzSafeAny } from 'ng-zorro-antd/core/types';
  22. import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';
  23. import { NzMessageService } from 'ng-zorro-antd/message';
  24. import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
  25. import { NzTableQueryParams } from 'ng-zorro-antd/table';
  26. import { NzFormatEmitEvent, NzTreeNode, NzTreeNodeOptions } from 'ng-zorro-antd/tree';
  27. import { GroupPrivilegesService } from '../../../service/group-privileges.service';
  28. import { set2String } from '../../../shared/index';
  29. import { SelectGroupsComponent } from '../groups/select-groups/select-groups.component';
  30. import { PrivilegesEditerComponent } from './privileges-editer/privileges-editer.component';
  31. @Component({
  32. selector: 'app-privileges',
  33. templateUrl: './privileges.component.html',
  34. styleUrls: ['./privileges.component.less']
  35. })
  36. export class PrivilegesComponent implements OnInit {
  37. query: {
  38. params: {
  39. name: String;
  40. displayName: String;
  41. username: String;
  42. groupId: String;
  43. groupName: String;
  44. appName: String;
  45. appId: String;
  46. startDate: String;
  47. endDate: String;
  48. startDatePicker: Date;
  49. endDatePicker: Date;
  50. pageSize: number;
  51. pageNumber: number;
  52. pageSizeOptions: number[];
  53. };
  54. results: {
  55. records: number;
  56. rows: NzSafeAny[];
  57. };
  58. expandForm: Boolean;
  59. submitLoading: boolean;
  60. tableLoading: boolean;
  61. tableInitialize: boolean;
  62. tableCheckedId: Set<String>;
  63. indeterminate: boolean;
  64. checked: boolean;
  65. } = {
  66. params: {
  67. name: '',
  68. displayName: '',
  69. username: '',
  70. groupId: '',
  71. groupName: '',
  72. appName: '',
  73. appId: '',
  74. startDate: '',
  75. endDate: '',
  76. startDatePicker: addDays(new Date(), -30),
  77. endDatePicker: new Date(),
  78. pageSize: 10,
  79. pageNumber: 1,
  80. pageSizeOptions: [10, 20, 50]
  81. },
  82. results: {
  83. records: 0,
  84. rows: []
  85. },
  86. expandForm: false,
  87. submitLoading: false,
  88. tableLoading: false,
  89. tableInitialize: true,
  90. tableCheckedId: new Set<String>(),
  91. indeterminate: false,
  92. checked: false
  93. };
  94. constructor(
  95. private modalService: NzModalService,
  96. private groupPrivilegesService: GroupPrivilegesService,
  97. private viewContainerRef: ViewContainerRef,
  98. private fb: FormBuilder,
  99. private msg: NzMessageService,
  100. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  101. private cdr: ChangeDetectorRef,
  102. private http: _HttpClient
  103. ) { }
  104. ngOnInit(): void {
  105. this.query.tableInitialize = false;
  106. }
  107. onQueryParamsChange(tableQueryParams: NzTableQueryParams): void {
  108. this.query.params.pageNumber = tableQueryParams.pageIndex;
  109. this.query.params.pageSize = tableQueryParams.pageSize;
  110. if (!this.query.tableInitialize) {
  111. this.fetch();
  112. }
  113. }
  114. onSearch(): void {
  115. this.fetch();
  116. }
  117. onReset(): void { }
  118. onBatchDelete(e: MouseEvent): void {
  119. e.preventDefault();
  120. this.groupPrivilegesService.delete(set2String(this.query.tableCheckedId)).subscribe(res => {
  121. if (res.code == 0) {
  122. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  123. this.fetch();
  124. } else {
  125. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  126. }
  127. this.cdr.detectChanges();
  128. });
  129. }
  130. onAdd(e: MouseEvent): void {
  131. e.preventDefault();
  132. const modal = this.modalService.create({
  133. nzContent: PrivilegesEditerComponent,
  134. nzViewContainerRef: this.viewContainerRef,
  135. nzComponentParams: {
  136. isEdit: false,
  137. groupId: this.query.params.groupId
  138. },
  139. nzWidth: 700,
  140. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  141. });
  142. // Return a result when closed
  143. modal.afterClose.subscribe(result => {
  144. if (result.refresh) {
  145. this.fetch();
  146. }
  147. });
  148. }
  149. onSelect(e: MouseEvent): void {
  150. e.preventDefault();
  151. const modal = this.modalService.create({
  152. nzContent: SelectGroupsComponent,
  153. nzViewContainerRef: this.viewContainerRef,
  154. nzComponentParams: {},
  155. nzWidth: 700,
  156. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  157. });
  158. // Return a result when closed
  159. modal.afterClose.subscribe(result => {
  160. if (result.refresh) {
  161. this.query.params.groupName = result.data.name;
  162. this.query.params.groupId = result.data.id;
  163. console.log(result);
  164. this.fetch();
  165. }
  166. });
  167. }
  168. onDelete(e: MouseEvent, deleteId: String): void {
  169. e.preventDefault();
  170. this.groupPrivilegesService.delete(deleteId).subscribe(res => {
  171. if (res.code == 0) {
  172. this.msg.success(this.i18n.fanyi('mxk.alert.delete.success'));
  173. this.fetch();
  174. } else {
  175. this.msg.error(this.i18n.fanyi('mxk.alert.delete.error'));
  176. }
  177. this.cdr.detectChanges();
  178. });
  179. }
  180. fetch(): void {
  181. this.query.submitLoading = true;
  182. this.query.tableLoading = true;
  183. this.query.indeterminate = false;
  184. this.query.checked = false;
  185. this.query.tableCheckedId.clear();
  186. if (this.query.expandForm) {
  187. this.query.params.endDate = format(this.query.params.endDatePicker, 'yyyy-MM-dd HH:mm:ss');
  188. this.query.params.startDate = format(this.query.params.startDatePicker, 'yyyy-MM-dd HH:mm:ss');
  189. } else {
  190. this.query.params.endDate = '';
  191. this.query.params.startDate = '';
  192. }
  193. this.groupPrivilegesService.member(this.query.params).subscribe(res => {
  194. this.query.results = res.data;
  195. this.query.submitLoading = false;
  196. this.query.tableLoading = false;
  197. console.log(res.data);
  198. this.cdr.detectChanges();
  199. });
  200. }
  201. updateTableCheckedSet(id: String, checked: boolean): void {
  202. if (checked) {
  203. this.query.tableCheckedId.add(id);
  204. } else {
  205. this.query.tableCheckedId.delete(id);
  206. }
  207. }
  208. refreshTableCheckedStatus(): void {
  209. const listOfEnabledData = this.query.results.rows.filter(({ disabled }) => !disabled);
  210. this.query.checked = listOfEnabledData.every(({ id }) => this.query.tableCheckedId.has(id));
  211. this.query.indeterminate = listOfEnabledData.some(({ id }) => this.query.tableCheckedId.has(id)) && !this.query.checked;
  212. }
  213. onTableItemChecked(id: String, checked: boolean): void {
  214. this.updateTableCheckedSet(id, checked);
  215. this.refreshTableCheckedStatus();
  216. }
  217. onTableAllChecked(checked: boolean): void {
  218. this.query.results.rows.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateTableCheckedSet(id, checked));
  219. this.refreshTableCheckedStatus();
  220. }
  221. }