From e06a702102a5f6baf09474bd35388234eade35cf Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Fri, 17 Sep 2021 11:47:39 +0800 Subject: [PATCH] =?UTF-8?q?chore(App):=20chooseImage=E3=80=81chooseVideo?= =?UTF-8?q?=20optimize?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/service/api/media/chooseImage.ts | 64 +++--------------- .../src/service/api/media/chooseVideo.ts | 67 ++++++------------- 2 files changed, 30 insertions(+), 101 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 21712424d..6b8e49c42 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 @@ -58,7 +33,7 @@ type File = { export const chooseImage = defineAsyncApi( API_CHOOSE_IMAGE, - // @ts-ignore crop 属性App特有 + // @ts-expect-error crop 属性App特有 ({ count, sizeType, sourceType, crop } = {}, { resolve, reject }) => { initI18nChooseImageMsgsOnce() const { t } = useI18n() @@ -86,38 +61,17 @@ export const chooseImage = defineAsyncApi( function openCamera() { const camera = plus.camera.getCamera() - camera.captureImage( - (path) => { - // fix By Lxh 暂时添加拍照压缩逻辑,等客户端增加逻辑后修改 - // 判断是否需要压缩 - if (sizeType && sizeType.includes('compressed')) { - return getFileInfo(path) - .then(({ size }) => { - // 压缩阈值 0.5 兆 - const THRESHOLD = 1024 * 1024 * 0.5 - return size && size > THRESHOLD - ? compressImage(path).then((dstPath) => - successCallback([dstPath]) - ) - : successCallback([path]) - }) - .catch(errorCallback) - } - - return successCallback([path]) - }, - errorCallback, - { - filename: TEMP_PATH + '/camera/', - resolution: 'high', - crop, - } - ) + camera.captureImage((path) => successCallback([path]), errorCallback, { + filename: TEMP_PATH + '/camera/', + resolution: 'high', + crop, + // @ts-expect-error + sizeType, + }) } function openAlbum() { - // NOTE 5+此API分单选和多选,多选返回files:string[] - // @ts-ignore + // @ts-ignore 5+此API分单选和多选,多选返回files:string[] plus.gallery.pick(({ files }) => successCallback(files), errorCallback, { maximum: count, multiple: true, diff --git a/packages/uni-app-plus/src/service/api/media/chooseVideo.ts b/packages/uni-app-plus/src/service/api/media/chooseVideo.ts index 9cfee6137..e6867ae1e 100644 --- a/packages/uni-app-plus/src/service/api/media/chooseVideo.ts +++ b/packages/uni-app-plus/src/service/api/media/chooseVideo.ts @@ -1,6 +1,5 @@ import { TEMP_PATH } from '../constants' import { warpPlusErrorCallback } from '../../../helpers/plus' -import { getFileName } from '../../../helpers/file' import { initI18nChooseVideoMsgsOnce, useI18n } from '@dcloudio/uni-core' import { API_TYPE_CHOOSE_VIDEO, @@ -17,56 +16,28 @@ export const chooseVideo = defineAsyncApi( const { t } = useI18n() const errorCallback = warpPlusErrorCallback(reject) - function successCallback(tempFilePath: string = '') { - const filename = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName( - tempFilePath - )}` - const compressVideo: Promise = compressed - ? new Promise((resolve) => { - plus.zip.compressVideo( - { - src: tempFilePath, - filename, - quality: 'medium', - }, - ({ tempFilePath }: { tempFilePath: string }) => { - resolve(tempFilePath) - }, - () => { - resolve(tempFilePath) - } - ) - }) - : Promise.resolve(tempFilePath) - if (compressed) { - plus.nativeUI.showWaiting() - } - compressVideo.then((tempFilePath: string) => { - if (compressed) { - plus.nativeUI.closeWaiting() - } - plus.io.getVideoInfo({ - filePath: tempFilePath, - success(videoInfo) { - const result = { - errMsg: 'chooseVideo:ok', - tempFilePath: tempFilePath, - size: videoInfo.size, - duration: videoInfo.duration, - width: videoInfo.width, - height: videoInfo.height, - } - resolve(result as any) - }, - fail: errorCallback, - }) + function successCallback(tempFilePath: string) { + plus.io.getVideoInfo({ + filePath: tempFilePath, + success(videoInfo) { + const result = { + errMsg: 'chooseVideo:ok', + tempFilePath: tempFilePath, + size: videoInfo.size, + duration: videoInfo.duration, + width: videoInfo.width, + height: videoInfo.height, + } + // @ts-expect-error tempFile、name 仅H5支持 + resolve(result) + }, + fail: errorCallback, }) } function openAlbum() { plus.gallery.pick( - // NOTE 5+此API分单选和多选,多选返回files:string[] - // @ts-ignore + // @ts-ignore 5+此API分单选和多选,多选返回files:string[] ({ files }) => successCallback(files[0]), errorCallback, { @@ -77,6 +48,8 @@ export const chooseVideo = defineAsyncApi( maximum: 1, filename: TEMP_PATH + '/gallery/', permissionAlert: true, + // @ts-expect-error 新增参数,用于视频压缩 + videoCompress: compressed, } ) } @@ -87,6 +60,8 @@ export const chooseVideo = defineAsyncApi( index: camera === 'front' ? '2' : '1', videoMaximumDuration: maxDuration, filename: TEMP_PATH + '/camera/', + // @ts-expect-error 新增参数,用于视频压缩 + videoCompress: compressed, }) } -- GitLab