Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Velpro187
uni-app
提交
b9cad0ae
U
uni-app
项目概览
Velpro187
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b9cad0ae
编写于
9月 18, 2020
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 优化 uni.chooseImage 错误回调
上级
825345e2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
109 addition
and
151 deletion
+109
-151
src/core/helpers/protocol/media/choose-image.js
src/core/helpers/protocol/media/choose-image.js
+6
-28
src/platforms/app-plus/service/api/file/file.js
src/platforms/app-plus/service/api/file/file.js
+2
-6
src/platforms/app-plus/service/api/media/choose-image.js
src/platforms/app-plus/service/api/media/choose-image.js
+96
-117
src/platforms/app-plus/service/api/util.js
src/platforms/app-plus/service/api/util.js
+5
-0
未找到文件。
src/core/helpers/protocol/media/choose-image.js
浏览文件 @
b9cad0ae
...
...
@@ -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
}
}
}
}
src/platforms/app-plus/service/api/file/file.js
浏览文件 @
b9cad0ae
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
)
{
...
...
src/platforms/app-plus/service/api/media/choose-image.js
浏览文件 @
b9cad0ae
...
...
@@ -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
}
})
}
src/platforms/app-plus/service/api/util.js
浏览文件 @
b9cad0ae
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录