Browse Source

改为Promise.all提高性能

htc 3 days ago
parent
commit
0311bd2812
1 changed files with 41 additions and 14 deletions
  1. 41 14
      pagesHome/pdf.vue

+ 41 - 14
pagesHome/pdf.vue

@@ -258,19 +258,47 @@
 		});
 		});
     },
     },
     methods: {
     methods: {
-		async createPdf(){
-			uni.showLoading({
-				title:'正在生成PDF所需的图片...'
-			})
-			await this.downloadZtzdfxImg();
-			for(const d of this.reportData.dimensionAnalysis){
-				const fileurl = await this.generateScoreImage(d);
-				this.pdfImages.push(fileurl)
-			}
+		// async createPdf(){
+		// 	uni.showLoading({
+		// 		title:'正在生成PDF所需的图片...'
+		// 	})
+		// 	const imgUrl = await this.downloadZtzdfxImg();
+		//  this.pdfImages.push(imgUrl);
+		
+		// 	for(const d of this.reportData.dimensionAnalysis){
+		// 		const fileurl = await this.generateScoreImage(d);
+		// 		this.pdfImages.push(fileurl)
+		// 	}
 			
 			
-			this.$showToast(`生成成功,共计${this.pdfImages.length}张`);
-			uni.hideLoading();
-			console.log(this.pdfImages);
+		// 	this.$showToast(`生成成功,共计${this.pdfImages.length}张`);
+		// 	uni.hideLoading();
+		// 	console.log(this.pdfImages);
+		// },
+		// Promise.all方法,性能更高
+		async createPdf(){
+		    uni.showLoading({
+		        title:'正在生成PDF所需的图片...'
+		    })
+		    try {
+		        const ztzdfxImgPromise = this.downloadZtzdfxImg();
+		        const dimensionImagePromises = this.reportData.dimensionAnalysis.map(d => {
+		            return this.generateScoreImage(d);
+		        });
+		
+		        const allImageUrls = await Promise.all([
+		            ztzdfxImgPromise, 
+		            ...dimensionImagePromises
+		        ]);
+		        this.pdfImages = allImageUrls;
+		
+		        uni.hideLoading();
+		        this.$showToast(`生成成功,共计${this.pdfImages.length}张`);
+				console.log(this.pdfImages);
+		    } catch (error) {
+		        uni.hideLoading();
+		        console.error('生成图片过程中发生错误:', error);
+		        uni.showToast({ title: '生成图片失败,请重试', icon: 'none' });
+		    }
 		},
 		},
 		// 绘制主函数
 		// 绘制主函数
 		async generateScoreImage(dimensionData) {
 		async generateScoreImage(dimensionData) {
@@ -560,8 +588,7 @@
 					success: async (res) => {
 					success: async (res) => {
 						const imgUrl = await this.uploadFilePromise(res.tempFilePath);
 						const imgUrl = await this.uploadFilePromise(res.tempFilePath);
 						console.log(imgUrl,'imgUrl');
 						console.log(imgUrl,'imgUrl');
-						this.pdfImages.push(imgUrl);
-						resolve()
+						resolve(imgUrl)
 					},
 					},
 					fail: (err) => {
 					fail: (err) => {
 						console.log('生成图片失败:', err);
 						console.log('生成图片失败:', err);