From 16da9f60a74bd9ea876a2f0c46db85abe113e3eb Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Mon, 9 Aug 2021 19:33:31 +0800 Subject: [PATCH] feat(App): chooseImage compress --- .../src/service/api/media/chooseImage.ts | 69 +++---------------- 1 file changed, 11 insertions(+), 58 deletions(-) diff --git a/packages/uni-app-plus/src/service/api/media/chooseImage.ts b/packages/uni-app-plus/src/service/api/media/chooseImage.ts index 55d543e59..be61a0c40 100644 --- a/packages/uni-app-plus/src/service/api/media/chooseImage.ts +++ b/packages/uni-app-plus/src/service/api/media/chooseImage.ts @@ -1,6 +1,5 @@ import { TEMP_PATH } from '../constants' import { warpPlusErrorCallback } from '../../../helpers/plus' -import { getFileName } from '../../../helpers/file' import { API_TYPE_CHOOSE_IMAGE, API_CHOOSE_IMAGE, @@ -27,30 +26,6 @@ function getFileInfo(filePath: string): Promise { }) } -function compressImage(tempFilePath: string): Promise { - const dst = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName( - tempFilePath - )}` - return new Promise((resolve) => { - plus.nativeUI.showWaiting() - plus.zip.compressImage( - { - src: tempFilePath, - dst, - overwrite: true, - }, - () => { - plus.nativeUI.closeWaiting() - resolve(dst) - }, - () => { - plus.nativeUI.closeWaiting() - resolve(tempFilePath) - } - ) - }) -} - type File = { path: string size: number @@ -67,39 +42,15 @@ export const chooseImage = defineAsyncApi( function successCallback(paths: string[]) { const tempFiles: File[] = [] const tempFilePaths: string[] = [] - // plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行 - paths - .reduce((promise, path) => { - return promise - .then(() => { - return getFileInfo(path) - }) - .then((fileInfo) => { - const size = fileInfo.size! - // 压缩阈值 0.5 兆 - const THRESHOLD = 1024 * 1024 * 0.5 - // 判断是否需要压缩 - if ( - !crop && - sizeType!.includes('compressed') && - size > THRESHOLD - ) { - return compressImage(path).then((dstPath) => { - path = dstPath - return getFileInfo(path) - }) - } - return fileInfo - }) - .then(({ size }) => { - tempFilePaths.push(path) - tempFiles.push({ - path, - size: size!, - }) - }) - }, Promise.resolve()) - .then(() => { + + Promise.all(paths.map((path) => getFileInfo(path))) + .then((filesInfo) => { + filesInfo.forEach((file, index) => { + const path = paths[index] + tempFilePaths.push(path) + tempFiles.push({ path, size: file.size! }) + }) + resolve({ tempFilePaths, tempFiles, @@ -127,6 +78,8 @@ export const chooseImage = defineAsyncApi( filename: TEMP_PATH + '/gallery/', permissionAlert: true, crop, + // @ts-expect-error + sizeType, }) } -- GitLab