Browse Source

Merge branch 'master' of http://git.fmode.cn:3000/bin/edu-textbook

xll 7 months ago
parent
commit
082b60d704

+ 65 - 38
projects/textbook/src/app/comp-upload/comp-upload.component.ts

@@ -7,84 +7,111 @@ import { ProvierOssAli } from './provider-oss-aliyun';
 import { NzImageService } from 'ng-zorro-antd/image';
 import { NzImageModule } from 'ng-zorro-antd/image';
 
-
 @Component({
   selector: 'app-comp-upload',
   standalone: true,
-  imports: [NzUploadModule, CommonCompModule,NzImageModule],
+  imports: [NzUploadModule, CommonCompModule, NzImageModule],
   templateUrl: './comp-upload.component.html',
   styleUrls: ['./comp-upload.component.scss'],
 })
 export class CompUploadComponent implements OnInit {
-  @Input('title') title:string = '上传文件'
-  @Input('files') files:Array<any> = []
+  @Input('title') title: string = '上传文件';
+  @Input('files') files: Array<any> = [];
   @Output() change: EventEmitter<any> = new EventEmitter<any>();
-  @Input('width') width:number = 0
-  
-  fileList:any = [
+  @Input('width') width: number = 0;
+  @Input('maxlenght') maxlenght:number = 1 //文件数量限制
+
+  fileList: any = [
     // {
     //   uid: '1',
     //   name: 'xxx.png',
     //   status: 'done',
-    //   response: 'Server Error 500', 
+    //   response: 'Server Error 500',
     //   url: 'http://www.baidu.com/xxx.png'
     // },
   ];
-  ossProvider:{upload:any}|undefined
-  constructor(private msg: NzMessageService,private nzImageService: NzImageService) {
-    this.ossProvider = new ProvierOssAli()
+  ossProvider: { upload: any } | undefined;
+  constructor(
+    private msg: NzMessageService,
+    private nzImageService: NzImageService
+  ) {
+    this.ossProvider = new ProvierOssAli();
   }
 
   ngOnInit() {
-    this.fileList = this.files.map((item:any)=> {
+    this.fileList = this.files.map((item: any) => {
       return {
-        url: item
-      }
-    })
+        url: item,
+      };
+    });
   }
-  ossFileList:any
-  Previewfilelist:any
+  ossFileList: any;
+  Previewfilelist: any;
   async handleChange(info: NzUploadChangeParam) {
     console.log(info);
+    let fileList = [...info.fileList];
+    fileList = fileList.slice(-this.maxlenght);
+    fileList = fileList.map(file => {
+      if (file.response) {
+        file.url = file.response.url;
+      }
+      return file;
+    });
+    this.fileList = fileList;
+
     if (info.file.status !== 'uploading') {
       // 选择文件后,自动开始上传
-      console.log(info.file, info.fileList);
-      this.Previewfilelist = info.fileList
-      let ossFileList = await this.uploadAllFileList(info?.fileList)
-      this.ossFileList = ossFileList
-      this.change.emit(ossFileList?.map(item=>item?.url))
+      // console.log(info.file, info.fileList);
+      this.Previewfilelist = info.fileList;
+      let ossFileList = await this.uploadAllFileList(info?.fileList);
+      this.ossFileList = ossFileList;
+      this.change.emit(
+        ossFileList?.map((item) => {
+          // console.log(item);
+          let f = {
+            name:item.locaname,
+            url:item?.url
+          }
+          return f
+        })
+      );
     }
     if (info.file.status === 'done') {
       // this.msg.success(`${info.file.name} file uploaded successfully`);
+      // console.log(info);
     } else if (info.file.status === 'error') {
       // this.msg.error(`${info.file.name} file upload failed.`);
     }
-
     // this.change.emit(info.fileList)
-    
   }
 
-  async uploadAllFileList(fileList:Array<NzUploadFile>){
-    let ossFileList = []
+  async uploadAllFileList(fileList: Array<NzUploadFile>) {
+    let ossFileList = [];
     for (let index = 0; index < fileList.length; index++) {
       let file = fileList[index];
-      if(file){
-        let ossFile 
-        try{
+      if (file) {
+        let ossFile;
+        try {
           ossFile = await this.ossProvider?.upload(file?.originFileObj);
-        }catch(err){}
-        if(ossFile){
-          ossFileList.push(ossFile)
+        } catch (err) {}
+        if (ossFile) {
+          ossFile.locaname = file.name
+          ossFileList.push(ossFile);
         }
       }
     }
-    return ossFileList
+    return ossFileList;
   }
   /**预览图片 */
-  preview=async (e:any): Promise<void> => {
-    let index = this.Previewfilelist.findIndex((item:any)=>item.uid==e.uid)
-    this.nzImageService.preview([{src:this.ossFileList[index].url}], { nzZoom: 1, nzRotate: 0 });
+  preview = async (e: any): Promise<void> => {
+    let index = this.Previewfilelist.findIndex(
+      (item: any) => item.uid == e.uid
+    );
+    let file = this.ossFileList[index].url;
+    if (!/\.(jpg|jpeg|png|GIF|JPG|PNG)$/.test(file)) {
+      window.open(file);
+    } else {
+      this.nzImageService.preview([{ src: file }], { nzZoom: 1, nzRotate: 0 });
+    }
   };
-
-
 }

+ 1 - 1
projects/textbook/src/modules/login/account-info/account-info.component.ts

@@ -232,7 +232,7 @@ export class AccountInfoComponent implements OnInit {
   upload(e: any) {
     console.log(e);
     let file = e[0];
-    this.identityFile = file;
+    this.identityFile = file?.name;
   }
   //验证手机号或邮箱是否注册
   async authUser(): Promise<boolean> {