role-editer.component.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 { Component, ChangeDetectorRef, Input, 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 from 'date-fns/format';
  21. import { NzMessageService } from 'ng-zorro-antd/message';
  22. import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
  23. import { Roles } from 'src/app/entity/Roles';
  24. import { TreeNodes } from '../../../../entity/TreeNodes';
  25. import { OrganizationsService } from '../../../../service/organizations.service';
  26. import { RolesService } from '../../../../service/roles.service';
  27. @Component({
  28. selector: 'app-role-editer',
  29. templateUrl: './role-editer.component.html',
  30. styles: [
  31. `
  32. nz-form-item {
  33. width: 100%;
  34. }
  35. `
  36. ],
  37. styleUrls: ['./role-editer.component.less']
  38. })
  39. export class RoleEditerComponent implements OnInit {
  40. @Input() id?: String;
  41. @Input() isEdit?: boolean;
  42. form: {
  43. submitting: boolean;
  44. model: Roles;
  45. } = {
  46. submitting: false,
  47. model: new Roles()
  48. };
  49. // TreeNodes
  50. treeNodes = new TreeNodes(false);
  51. selectValues: string[] = [];
  52. formGroup: FormGroup = new FormGroup({});
  53. constructor(
  54. private modalRef: NzModalRef,
  55. private rolesService: RolesService,
  56. private orgsService: OrganizationsService,
  57. private fb: FormBuilder,
  58. private msg: NzMessageService,
  59. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  60. private cdr: ChangeDetectorRef
  61. ) { }
  62. ngOnInit(): void {
  63. this.tree();
  64. if (this.isEdit) {
  65. this.rolesService.get(`${this.id}`).subscribe(res => {
  66. this.form.model.init(res.data);
  67. this.selectValues = this.form.model.orgIdsList.split(',');
  68. this.cdr.detectChanges();
  69. });
  70. }
  71. }
  72. tree(): void {
  73. this.orgsService.tree({}).subscribe(res => {
  74. this.treeNodes.init(res.data);
  75. this.treeNodes.nodes = this.treeNodes.build();
  76. this.cdr.detectChanges();
  77. });
  78. }
  79. onClose(e: MouseEvent): void {
  80. e.preventDefault();
  81. this.modalRef.destroy({ refresh: false });
  82. }
  83. onSubmit(e: MouseEvent): void {
  84. e.preventDefault();
  85. this.form.submitting = true;
  86. this.form.model.trans();
  87. this.form.model.orgIdsList = '';
  88. this.selectValues.forEach(value => {
  89. this.form.model.orgIdsList = `${this.form.model.orgIdsList + value},`;
  90. });
  91. (this.isEdit ? this.rolesService.update(this.form.model) : this.rolesService.add(this.form.model)).subscribe(res => {
  92. if (res.code == 0) {
  93. this.msg.success(this.i18n.fanyi(this.isEdit ? 'mxk.alert.update.success' : 'mxk.alert.add.success'));
  94. } else {
  95. this.msg.error(this.i18n.fanyi(this.isEdit ? 'mxk.alert.update.error' : 'mxk.alert.add.error'));
  96. }
  97. this.form.submitting = false;
  98. this.modalRef.destroy({ refresh: true });
  99. this.cdr.detectChanges();
  100. });
  101. }
  102. }