import { RhQualityManageSharedService } from './../quality-manage-shared.service'; import { RhStorageService } from 'rh-base/core'; import { RhSafeAny } from 'rh-base/model'; import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'; import { MsgHelper } from 'rh-base/core'; import { UploadAttachFilesDto, UploadFileOption, InspectUploadCode, IQualityUploadTypeDto } from '../../qmp-model'; import { RhQmpService } from '../../qmp.service'; import { Observable, Observer } from 'rxjs'; import { RhInspectionAttachFilesPreviewComponent } from '../inspection-attach-files-preview/inspection-attach-files-preview.component'; import { NzUploadFile, UploadFilter } from 'ng-zorro-antd/upload'; @Component({ selector: 'rh-inspection-attach-files-upload', templateUrl: './inspection-attach-files-upload.component.html', styleUrls: ['./inspection-attach-files-upload.component.less'] }) export class RhInspectionAttachFilesUploadComponent implements OnInit { @ViewChild(RhInspectionAttachFilesPreviewComponent) priviewFile!: RhInspectionAttachFilesPreviewComponent; @Input() rhData!: IQualityUploadTypeDto; // @Output() rhClose = new EventEmitter(); @Input() rhOrderType = 'IqcAttachFileModule'; @Input() rhOrderCode!: InspectUploadCode; @Input() isShowPreview = true; /** 允许上传的最大单个文件大小,单位:M */ @Input() rhMaxFileSize = 50; @Output() rhSuccess = new EventEmitter(); @Output() rhFail = new EventEmitter(); uploadFileDto!: UploadAttachFilesDto; uploadFileList: NzUploadFile[] = []; /**上传loading*/ uploading = false; /**上传文件限制*/ fileReg = /\.(pdf|doc|docx|ppt|pptx|xls|xlsx|png|jpg|jpeg|bmp|gif|webp|psd|svg|tiff)$/; filters: UploadFilter[] = [ { name: 'type', fn: (fileList: NzUploadFile[]) => { const filterFileList = fileList.filter((f) => this.fileReg.test(f.name.toLocaleLowerCase())); if (fileList.length !== filterFileList.length) { MsgHelper.ShowWarningMessage('请上传WORD、EXCEL、PPT、PDF、图片格式的文件!'); return filterFileList; } return fileList; } }, { name: 'size', fn: (fileList: NzUploadFile[]) => { const filterFileList = fileList.filter((f) => (f.size as number) < 50 * 1024 * 1024); //console.log(fileList,filterFileList); if (fileList.length !== filterFileList.length) { MsgHelper.ShowWarningMessage('请上传大小在50M以下的文件!'); return filterFileList; } return fileList; } } ]; constructor(private operator: RhQmpService, public sharedSer: RhQualityManageSharedService, private storage: RhStorageService) {} ngOnInit() { this.uploadFileDto = this.iniUploadFileDto(); // this.createUploadForm(UploadAttachFilesDto.create()); // this.getInspectTypes(); } // createUploadForm(dto: UploadAttachFilesDto) { // this.uploadForm = this.fb.group({ // /** 单据类型 */ // OrderType: [dto.OrderType, [Validators.required]], // /** 单据代码 */ // OrderCode: [dto.OrderCode, [Validators.required]], // /** 工厂代码 */ // FactoryCode: [dto.FactoryCode, [Validators.required]], // /** 附件信息 */ // UploadFileOptions: [dto.UploadFileOptions, [Validators.required]] // }); // } // getInspectTypes() { // this.apiUrlSer.getSettingEntry('QualityManagement', 'AuxiliaryBasic', 'InspectionType').subscribe((result) => { // if (result.Success) { // this.inspectTypes = result.Attach; // } // }); // } beforeUpload = (file: NzUploadFile) => { // this.fileList = this.fileList.concat(file); // if (this.fileList.length === 2) { // this.fileList = this.fileList.splice(this.fileList.length - 1, 1); // } // return false; return new Observable((observer: Observer) => { const reader = new FileReader(); reader.readAsDataURL(file as RhSafeAny); reader.onload = () => { const uploadFileOption = UploadFileOption.create(); uploadFileOption.FileName = file.name; const nameList = file.name.split('.'); uploadFileOption.FileExtensionName = nameList[nameList.length - 1]; uploadFileOption.FileStream = reader.result as RhSafeAny; this.uploadFileDto.UploadFileOptions?.push(uploadFileOption); this.uploadFileList = this.uploadFileList.concat(file); observer.next(false); observer.complete(); }; reader.onerror = (e) => { MsgHelper.ShowWarningModal(`读取文件失败!${e}`); observer.next(false); observer.complete(); }; }); }; handleUpload() { if (!this.uploadFileList?.length) { MsgHelper.ShowWarningModal('没有需要上传的文件!'); return; } this.uploading = true; this.operator.UploadAttachFiles(this.uploadFileDto).subscribe( (result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage('文件上传成功!'); this.uploadFileDto = this.iniUploadFileDto(); this.uploadFileList = []; this.priviewFile.getFileDatas(); this.rhSuccess.emit(); } else { MsgHelper.ShowWarningModal(`文件上传失败!${result.Message}`); this.rhSuccess.emit(); } this.uploading = false; }, () => (this.uploading = false) ); // if (!this.fileReg.test('.' + this.uploadDatas.FileExtensionName.toLowerCase())) { // MsgHelper.ShowWarningModal('请上传office文件或pdf格式的文件!'); // return; // } // 创建上传对象 // const uploadDto = UploadFileOption.create(); // FormHelper.YGSubmitForm(uploadDto, this.uploadForm, (dto) => { // dto.FileName = this.fileList[0].name; // const nameList = dto.FileName.split('.'); // dto.FileExtensionName = nameList[nameList.length - 1]; // //dto.InspectSchemaCode = this.rhData.InspectSchemaCode; // const reader = new FileReader(); // reader.readAsDataURL(this.fileList[0]); // const p = new Promise((resolve, reject) => { // reader.onload = function () { // dto.FileStream = this.result.toString(); // 转化为base64 // resolve(dto); // }; // }) // .then((dto: UploadAttachFilesDto) => { // this.uploadFile(dto); // }) // .catch(() => { // console.error('文件打开错误!'); // }); // }); } handleChange({ file }: { [key: string]: RhSafeAny }): void { const status = file.status; if (status === 'error') { this.uploading = false; } } removeFile = (file: NzUploadFile) => { const itemIndex = this.uploadFileDto.UploadFileOptions?.findIndex((data) => data.FileName === file.name); if (itemIndex !== -1) { this.uploadFileDto.UploadFileOptions?.splice(itemIndex as number, 1); } return true; }; // async uploadFile(dto: UploadAttachFilesDto) { // this.uploading = true; // let result = null; // let msg = null; // dto.OrderCode = this.rhOrderCode; // dto.OrderType = this.rhOrderType; // try { // result = await this.operator.UploadAttachFiles(dto).toPromise(); // msg = '上传'; // if (result.Success) { // MsgHelper.ShowSuccessMessage(`${msg}成功!`); // this.rhClose.emit(); // } else { // MsgHelper.ShowWarningMessage(`${msg}失败!${result.Message}`); // } // } catch (error) { // MsgHelper.ShowWarningMessage(`接口执行出错!${error}`); // } finally { // this.uploading = false; // } // } private iniUploadFileDto() { const temp = UploadAttachFilesDto.create(); temp.OrderType = `${this.rhOrderType}`; temp.OrderCode = `${this.sharedSer.orderCodeHandler(this.rhOrderCode, this.rhData)}`; temp.UploadFileOptions = []; temp.FactoryCode = this.storage.getUserSession()?.User?.FactoryCode; return temp; } }