提交 b9cad0ae 编写于 作者: Q qiang

fix: 优化 uni.chooseImage 错误回调

上级 825345e2
......@@ -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
}
}
}
}
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) {
......
......@@ -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
}
})
}
......@@ -206,3 +206,8 @@ export function warpPlusMethod (origin, name, before) {
}))
}
}
export function getFileName (path) {
const array = path.split('/')
return array[array.length - 1]
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册