app-cas-details-editer.component.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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, ViewContainerRef, 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 { NzUploadFile, NzUploadChangeParam } from 'ng-zorro-antd/upload';
  24. import { AppsCasDetails } from '../../../entity/AppsCasDetails';
  25. import { AppsCasDetailsService } from '../../../service/apps-cas-details.service';
  26. import { AppsService } from '../../../service/apps.service';
  27. import { SelectAdaptersComponent } from '../../config/adapters/select-adapters/select-adapters.component';
  28. const getBase64 = (file: File): Promise<string | ArrayBuffer | null> =>
  29. new Promise((resolve, reject) => {
  30. const reader = new FileReader();
  31. reader.readAsDataURL(file);
  32. reader.onload = () => resolve(reader.result);
  33. reader.onerror = error => reject(error);
  34. });
  35. @Component({
  36. selector: 'app-app-cas-details-editer',
  37. templateUrl: './app-cas-details-editer.component.html',
  38. styles: [
  39. `
  40. nz-tabset {
  41. width: 90%;
  42. }
  43. nz-form-item {
  44. width: 50%;
  45. }
  46. `
  47. ],
  48. styleUrls: ['./app-cas-details-editer.component.less']
  49. })
  50. export class AppCasDetailsEditerComponent implements OnInit {
  51. @Input() id?: String;
  52. @Input() isEdit?: boolean;
  53. form: {
  54. submitting: boolean;
  55. model: AppsCasDetails;
  56. } = {
  57. submitting: false,
  58. model: new AppsCasDetails()
  59. };
  60. formGroup: FormGroup = new FormGroup({});
  61. fileList: NzUploadFile[] = [];
  62. previewImage: string | ArrayBuffer | undefined | null = '';
  63. previewVisible = false;
  64. constructor(
  65. private modal: NzModalRef,
  66. private modalService: NzModalService,
  67. private appsCasDetailsService: AppsCasDetailsService,
  68. private appsService: AppsService,
  69. private viewContainerRef: ViewContainerRef,
  70. private fb: FormBuilder,
  71. private msg: NzMessageService,
  72. @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
  73. private cdr: ChangeDetectorRef
  74. ) { }
  75. ngOnInit(): void {
  76. if (this.isEdit) {
  77. this.appsCasDetailsService.get(`${this.id}`).subscribe(res => {
  78. this.form.model.init(res.data);
  79. this.previewImage = this.form.model.iconBase64.toString();
  80. this.fileList = [
  81. {
  82. uid: this.form.model.id.toString(),
  83. name: this.form.model.appName.toString(),
  84. status: 'done',
  85. url: this.previewImage
  86. }
  87. ];
  88. });
  89. } else {
  90. this.appsCasDetailsService.init().subscribe(res => {
  91. this.form.model.id = res.data.id;
  92. this.form.model.protocol = res.data.protocol;
  93. this.form.model.secret = res.data.secret;
  94. });
  95. }
  96. this.cdr.detectChanges();
  97. }
  98. handlePreview = async (file: NzUploadFile): Promise<void> => {
  99. let preview;
  100. if (!file.url) {
  101. preview = await getBase64(file.originFileObj!);
  102. }
  103. this.previewImage = file.url || preview;
  104. this.previewVisible = true;
  105. };
  106. uploadImageChange(uploadChange: NzUploadChangeParam): void {
  107. if (uploadChange.file.status === 'done') {
  108. this.form.model.iconId = uploadChange.file.response.data;
  109. this.cdr.detectChanges();
  110. }
  111. }
  112. onGenerate(e: MouseEvent): void {
  113. e.preventDefault();
  114. }
  115. onGenerateSecret(e: MouseEvent): void {
  116. e.preventDefault();
  117. this.appsService.generateSecret('base').subscribe(res => {
  118. this.form.model.secret = res.data;
  119. this.cdr.detectChanges();
  120. });
  121. }
  122. onSelectAdapter(e: MouseEvent): void {
  123. e.preventDefault();
  124. const modal = this.modalService.create({
  125. nzContent: SelectAdaptersComponent,
  126. nzViewContainerRef: this.viewContainerRef,
  127. nzComponentParams: {
  128. protocol: 'CAS'
  129. },
  130. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  131. });
  132. // Return a result when closed
  133. modal.afterClose.subscribe(result => {
  134. if (result.refresh) {
  135. this.form.model.adapter = result.data.adapter;
  136. this.form.model.adapterName = result.data.name;
  137. this.form.model.adapterId = result.data.id;
  138. }
  139. });
  140. }
  141. onClose(e: MouseEvent): void {
  142. e.preventDefault();
  143. this.modal.destroy({ refresh: false });
  144. }
  145. onSubmit(e: MouseEvent): void {
  146. e.preventDefault();
  147. this.form.submitting = true;
  148. this.form.model.trans();
  149. (this.isEdit ? this.appsCasDetailsService.update(this.form.model) : this.appsCasDetailsService.add(this.form.model)).subscribe(res => {
  150. if (res.code == 0) {
  151. this.msg.success(this.i18n.fanyi(this.isEdit ? 'mxk.alert.update.success' : 'mxk.alert.add.success'));
  152. } else {
  153. this.msg.error(this.i18n.fanyi(this.isEdit ? 'mxk.alert.update.error' : 'mxk.alert.add.error'));
  154. }
  155. this.form.submitting = false;
  156. this.modal.destroy({ refresh: true });
  157. this.cdr.detectChanges();
  158. });
  159. }
  160. }