import { UploadAttachFilesDto } from './../../shared-manage-model'; import { RhSharedManageService } from './../../shared-manage.service'; import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { NzSizeLDSType } from 'ng-zorro-antd/core/types'; import { NzButtonType } from 'ng-zorro-antd/button'; import { NzUploadFile } from 'ng-zorro-antd/upload'; import { UploadFileOption, RhSafeAny, RhBaseDto } from 'rh-base/model'; import { Observable, Observer } from 'rxjs'; import { MsgHelper } from 'rh-base/core'; @Component({ selector: 'rh-attach-file-upload', templateUrl: './attach-file-upload.component.html', styleUrls: ['./attach-file-upload.component.less'] }) export class RhAttachFileUploadComponent implements OnInit { @Input() rhOrder!: RhBaseDto; @Input() rhOrderType!: string; /** 导入button的type */ @Input() rhContent = '附件上传'; /** button类型 */ @Input() rhType: NzButtonType = 'default'; /** 导入button的size */ @Input() rhSize: NzSizeLDSType = 'small'; @Input() rhTitle = '附件上传'; @Output() rhSuccess = new EventEmitter(); @Output() rhFail = new EventEmitter(); isShowUploadModal = false; isDirectoryUploadEnable = false; uploading = false; /** 上传的文件列表 */ uploadFileList: NzUploadFile[] = []; /** 上传的文件数据列表 */ uploadDatas: UploadFileOption[] = []; constructor(private operator: RhSharedManageService) { } ngOnInit(): void { } closeUploadFileModal() { this.isShowUploadModal = false; this.uploadFileList = []; this.uploadDatas = []; } openUploadFileModal() { this.isShowUploadModal = true; } beforeUpload = (file: NzUploadFile) => { return new Observable((observer: Observer) => { const reader = new FileReader(); reader.readAsDataURL(file as RhSafeAny); reader.onload = () => { const dto = UploadFileOption.create(); dto.FileStream = reader.result as RhSafeAny; dto.FileName = file.name; const list = dto.FileName.split('.'); dto.FileExtensionName = list.length === 2 ? list[1] : null; this.uploadDatas = this.uploadDatas.concat(dto); this.uploadFileList = this.uploadFileList.concat(file); observer.next(false); observer.complete(); }; reader.onerror = (e) => { MsgHelper.ShowWarningModal(`读取文件失败!${e}`); observer.next(false); observer.complete(); }; }); }; removeFile = (file: NzUploadFile) => { const itemIndex = this.uploadDatas.findIndex((data) => data.FileName === file.name); if (itemIndex !== -1) { this.uploadDatas.splice(itemIndex, 1); } return true; }; handleUpload() { this.uploading = true; const dto = UploadAttachFilesDto.create(); dto.OrderCode = this.rhOrder['OrderCode']; dto.OrderType = this.rhOrderType; dto.UploadFileOptions = this.uploadDatas; this.operator.UploadAttachFiles(dto).subscribe( (result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage('附件数据上传成功!'); this.closeUploadFileModal(); this.rhSuccess.emit(); } else { MsgHelper.ShowWarningModal(`附件数据上传失败!${result.Message}`); this.rhFail.emit(); } this.uploading = false; }, () => (this.uploading = false) ); } }