From b9cad0ae619faa1bbe9d82657ee34a4e7d7e0c56 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 18 Sep 2020 19:43:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=20uni.chooseImage=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../helpers/protocol/media/choose-image.js | 34 +-- .../app-plus/service/api/file/file.js | 8 +- .../service/api/media/choose-image.js | 213 ++++++++---------- src/platforms/app-plus/service/api/util.js | 5 + 4 files changed, 109 insertions(+), 151 deletions(-) diff --git a/src/core/helpers/protocol/media/choose-image.js b/src/core/helpers/protocol/media/choose-image.js index ba626eddc..d73829c6f 100644 --- a/src/core/helpers/protocol/media/choose-image.js +++ b/src/core/helpers/protocol/media/choose-image.js @@ -17,22 +17,9 @@ export const chooseImage = { required: false, default: SIZE_TYPES, validator (sizeType, params) { - // 非必传的参数,不符合预期时处理为默认值。 - const length = sizeType.length - if (!length) { - params.sizeType = SIZE_TYPES - } else if (typeof sizeType === 'string') { - if (!~SIZE_TYPES.indexOf(sizeType)) { - params.sizeType = SIZE_TYPES - } - } else { - for (let i = 0; i < length; i++) { - if (typeof sizeType[i] !== 'string' || !~SIZE_TYPES.indexOf(sizeType[i])) { - params.sizeType = SIZE_TYPES - break - } - } - } + sizeType = typeof sizeType === 'string' ? [sizeType] : sizeType + sizeType = sizeType.filter(sizeType => SIZE_TYPES.includes(sizeType)) + params.sizeType = sizeType.length ? sizeType : SIZE_TYPES } }, sourceType: { @@ -40,17 +27,8 @@ export const chooseImage = { required: false, default: SOURCE_TYPES, validator (sourceType, params) { - const length = sourceType.length - if (!length) { - params.sourceType = SOURCE_TYPES - } else { - for (let i = 0; i < length; i++) { - if (typeof sourceType[i] !== 'string' || !~SOURCE_TYPES.indexOf(sourceType[i])) { - params.sourceType = SOURCE_TYPES - break - } - } - } + sourceType = sourceType.filter(sourceType => SOURCE_TYPES.includes(sourceType)) + params.sourceType = sourceType.length ? sourceType : SOURCE_TYPES } } -} +} diff --git a/src/platforms/app-plus/service/api/file/file.js b/src/platforms/app-plus/service/api/file/file.js index 5f8456049..706fe9ee8 100644 --- a/src/platforms/app-plus/service/api/file/file.js +++ b/src/platforms/app-plus/service/api/file/file.js @@ -1,6 +1,7 @@ import { warpPlusMethod, - warpPlusErrorCallback + warpPlusErrorCallback, + getFileName } from '../util' import { @@ -19,11 +20,6 @@ function getSavedFileDir (success, fail) { }, fail) } -function getFileName (path) { - const array = path.split('/') - return array[array.length - 1] -} - export function saveFile ({ tempFilePath } = {}, callbackId) { diff --git a/src/platforms/app-plus/service/api/media/choose-image.js b/src/platforms/app-plus/service/api/media/choose-image.js index d4393c000..af3ccad10 100644 --- a/src/platforms/app-plus/service/api/media/choose-image.js +++ b/src/platforms/app-plus/service/api/media/choose-image.js @@ -6,6 +6,11 @@ import { invoke } from '../../bridge' +import { + warpPlusErrorCallback, + getFileName +} from '../util' + /** * 获取文件信息 * @param {string} filePath 文件路径 @@ -14,142 +19,116 @@ import { function getFileInfo (filePath) { return new Promise((resolve, reject) => { plus.io.resolveLocalFileSystemURL(filePath, function (entry) { - entry.getMetadata(function (meta) { - resolve({ - size: meta.size - }) - }, reject, false) + entry.getMetadata(resolve, reject, false) }, reject) }) } -const invokeChooseImage = function (callbackId, type, sizeType, tempFilePaths = []) { - if (!tempFilePaths.length) { - invoke(callbackId, { - code: sizeType, - errMsg: `chooseImage:${type}` +function compressImage (tempFilePath) { + const dstPath = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName(tempFilePath)}` + return new Promise((resolve, reject) => { + plus.nativeUI.showWaiting() + plus.zip.compressImage({ + src: tempFilePath, + dst: dstPath, + overwrite: true + }, () => { + plus.nativeUI.closeWaiting() + resolve(dstPath) + }, (error) => { + plus.nativeUI.closeWaiting() + reject(error) }) - return - } - var tempFiles = [] - // plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行 - tempFilePaths.reduce((promise, tempFilePath, index, array) => { - return promise - .then(() => { - return getFileInfo(tempFilePath) - }) - .then(fileInfo => { - var size = fileInfo.size + }) +} + +export function chooseImage ({ + count, + sizeType, + sourceType +} = {}, callbackId) { + const errorCallback = warpPlusErrorCallback(callbackId, 'chooseImage', 'cancel') + + function successCallback (paths) { + const tempFiles = [] + const tempFilePaths = [] + // plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行 + paths.reduce((promise, path) => { + return promise.then(() => { + return getFileInfo(path) + }).then(fileInfo => { + const size = fileInfo.size // 压缩阈值 0.5 兆 - var threshold = 1024 * 1024 * 0.5 + const THRESHOLD = 1024 * 1024 * 0.5 // 判断是否需要压缩 - if ((sizeType.indexOf('compressed') >= 0 && sizeType.indexOf('original') < 0) || ((( - sizeType.indexOf( - 'compressed') < 0 && sizeType.indexOf('original') < 0) || (sizeType - .indexOf('compressed') >= 0 && sizeType.indexOf( - 'original') >= 0)) && size > threshold)) { - return new Promise((resolve, reject) => { - var dstPath = TEMP_PATH + '/compressed/' + Date.now() + ( - tempFilePath.match(/\.\S+$/) || [''])[0] - plus.nativeUI.showWaiting() - plus.zip.compressImage({ - src: tempFilePath, - dst: dstPath, - overwrite: true - }, () => { - resolve(dstPath) - }, (error) => { - reject(error) - }) + if (sizeType.includes('compressed') && size > THRESHOLD) { + return compressImage(path).then(dstPath => { + path = dstPath + return getFileInfo(path) }) - .then(dstPath => { - array[index] = tempFilePath = dstPath - return getFileInfo(tempFilePath) - }) - .then(fileInfo => { - return tempFiles.push({ - path: tempFilePath, - size: fileInfo.size - }) - }) } - return tempFiles.push({ - path: tempFilePath, - size: size + return fileInfo + }).then(({ size }) => { + tempFilePaths.push(path) + tempFiles.push({ + path, + size }) }) - }, Promise.resolve()) - .then(() => { - plus.nativeUI.closeWaiting() + }, Promise.resolve()).then(() => { invoke(callbackId, { - errMsg: `chooseImage:${type}`, + errMsg: 'chooseImage:ok', tempFilePaths, tempFiles }) - }).catch(() => { - plus.nativeUI.closeWaiting() - invoke(callbackId, { - errMsg: `chooseImage:${type}` + }).catch(errorCallback) + } + + function openCamera () { + const camera = plus.camera.getCamera() + camera.captureImage(path => successCallback([path]), + errorCallback, { + filename: TEMP_PATH + '/camera/', + resolution: 'high' }) + } + + function openAlbum () { + plus.gallery.pick(({ files }) => successCallback(files), errorCallback, { + maximum: count, + multiple: true, + system: false, + filename: TEMP_PATH + '/gallery/' }) -} -const openCamera = function (callbackId, sizeType) { - const camera = plus.camera.getCamera() - camera.captureImage(e => invokeChooseImage(callbackId, 'ok', sizeType, [e]), - e => invokeChooseImage(callbackId, 'fail', 1), { - filename: TEMP_PATH + '/camera/', - resolution: 'high' - }) -} -const openAlbum = function (callbackId, sizeType, count) { - // TODO Android 需要拷贝到 temp 目录 - plus.gallery.pick(e => invokeChooseImage(callbackId, 'ok', sizeType, e.files.map(file => { - return file - })), e => { - invokeChooseImage(callbackId, 'fail', 2) - }, { - maximum: count, - multiple: true, - system: false, - filename: TEMP_PATH + '/gallery/' - }) -} + } -export function chooseImage ({ - count = 9, - sizeType = ['original', 'compressed'], - sourceType = ['album', 'camera'] -} = {}, callbackId) { - let fallback = true if (sourceType.length === 1) { - if (sourceType[0] === 'album') { - fallback = false - openAlbum(callbackId, sizeType, count) - } else if (sourceType[0] === 'camera') { - fallback = false - openCamera(callbackId, sizeType) + if (sourceType.includes('album')) { + openAlbum() + return + } else if (sourceType.includes('camera')) { + openCamera() + return } } - if (fallback) { - plus.nativeUI.actionSheet({ - cancel: '取消', - buttons: [{ - title: '拍摄' - }, { - title: '从手机相册选择' - }] - }, (e) => { - switch (e.index) { - case 0: - invokeChooseImage(callbackId, 'fail', 0) - break - case 1: - openCamera(callbackId, sizeType) - break - case 2: - openAlbum(callbackId, sizeType, count) - break - } - }) - } + plus.nativeUI.actionSheet({ + cancel: '取消', + buttons: [{ + title: '拍摄' + }, { + title: '从手机相册选择' + }] + }, (e) => { + switch (e.index) { + case 1: + openCamera() + break + case 2: + openAlbum() + break + default: + errorCallback() + break + } + }) } diff --git a/src/platforms/app-plus/service/api/util.js b/src/platforms/app-plus/service/api/util.js index 94591ab39..764e7d661 100644 --- a/src/platforms/app-plus/service/api/util.js +++ b/src/platforms/app-plus/service/api/util.js @@ -206,3 +206,8 @@ export function warpPlusMethod (origin, name, before) { })) } } + +export function getFileName (path) { + const array = path.split('/') + return array[array.length - 1] +} -- GitLab