Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Velpro187
uni-app
提交
bb6ef672
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,发现更多精彩内容 >>
提交
bb6ef672
编写于
6月 23, 2021
作者:
D
DCloud_LXH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(App): chooseImage、chooseVideo
上级
da4440c4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
313 addition
and
1 deletion
+313
-1
packages/uni-app-plus/src/helpers/file.ts
packages/uni-app-plus/src/helpers/file.ts
+9
-0
packages/uni-app-plus/src/helpers/plus.ts
packages/uni-app-plus/src/helpers/plus.ts
+1
-1
packages/uni-app-plus/src/service/api/media/chooseImage.ts
packages/uni-app-plus/src/service/api/media/chooseImage.ts
+172
-0
packages/uni-app-plus/src/service/api/media/chooseVideo.ts
packages/uni-app-plus/src/service/api/media/chooseVideo.ts
+131
-0
未找到文件。
packages/uni-app-plus/src/helpers/file.ts
0 → 100644
浏览文件 @
bb6ef672
export
function
getFileName
(
path
:
string
)
{
const
array
=
path
.
split
(
'
/
'
)
return
array
[
array
.
length
-
1
]
}
export
function
getExtName
(
path
:
string
)
{
const
array
=
path
.
split
(
'
.
'
)
return
array
.
length
>
1
?
'
.
'
+
array
[
array
.
length
-
1
]
:
''
}
packages/uni-app-plus/src/helpers/plus.ts
浏览文件 @
bb6ef672
...
...
@@ -24,7 +24,7 @@ export function warpPlusErrorCallback(
reject
:
(
errMsg
:
string
,
errRes
?:
any
)
=>
void
,
errMsg
?:
string
)
{
return
function
errorCallback
(
error
:
PlusError
)
{
return
function
errorCallback
(
error
?
:
PlusError
)
{
error
=
error
||
{}
// 一键登录errorCallback新增 appid、metadata、uid 参数返回
errMsg
=
error
.
message
||
errMsg
||
''
...
...
packages/uni-app-plus/src/service/api/media/chooseImage.ts
0 → 100644
浏览文件 @
bb6ef672
import
{
TEMP_PATH
}
from
'
../constants
'
import
{
warpPlusErrorCallback
}
from
'
../../../helpers/plus
'
import
{
getFileName
}
from
'
../../../helpers/file
'
import
{
API_TYPE_CHOOSE_IMAGE
,
API_CHOOSE_IMAGE
,
ChooseImageProtocol
,
ChooseImageOptions
,
defineAsyncApi
,
}
from
'
@dcloudio/uni-api
'
import
{
initI18nChooseImageMsgsOnce
,
useI18n
}
from
'
@dcloudio/uni-core
'
/**
* 获取文件信息
* @param {string} filePath 文件路径
* @returns {Promise} 文件信息Promise
*/
function
getFileInfo
(
filePath
:
string
):
Promise
<
PlusIoMetadata
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
plus
.
io
.
resolveLocalFileSystemURL
(
filePath
,
function
(
entry
)
{
entry
.
getMetadata
(
resolve
,
reject
,
false
)
},
reject
)
})
}
function
compressImage
(
tempFilePath
:
string
):
Promise
<
string
>
{
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
}
export
const
chooseImage
=
defineAsyncApi
<
API_TYPE_CHOOSE_IMAGE
>
(
API_CHOOSE_IMAGE
,
// @ts-ignore crop 属性App特有
({
count
,
sizeType
,
sourceType
,
crop
}
=
{},
{
resolve
,
reject
})
=>
{
initI18nChooseImageMsgsOnce
()
const
{
t
}
=
useI18n
()
const
errorCallback
=
warpPlusErrorCallback
(
reject
)
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
(()
=>
{
resolve
({
tempFilePaths
,
tempFiles
,
})
})
.
catch
(
errorCallback
)
}
function
openCamera
()
{
const
camera
=
plus
.
camera
.
getCamera
()
camera
.
captureImage
((
path
)
=>
successCallback
([
path
]),
errorCallback
,
{
filename
:
TEMP_PATH
+
'
/camera/
'
,
resolution
:
'
high
'
,
crop
,
})
}
function
openAlbum
()
{
// NOTE 5+此API分单选和多选,多选返回files:string[]
// @ts-ignore
plus
.
gallery
.
pick
(({
files
})
=>
successCallback
(
files
),
errorCallback
,
{
maximum
:
count
,
multiple
:
true
,
system
:
false
,
filename
:
TEMP_PATH
+
'
/gallery/
'
,
permissionAlert
:
true
,
crop
,
})
}
if
(
sourceType
!
.
length
===
1
)
{
if
(
sourceType
!
.
includes
(
'
album
'
))
{
openAlbum
()
return
}
else
if
(
sourceType
!
.
includes
(
'
camera
'
))
{
openCamera
()
return
}
}
plus
.
nativeUI
.
actionSheet
(
{
cancel
:
t
(
'
uni.chooseImage.cancel
'
),
buttons
:
[
{
title
:
t
(
'
uni.chooseImage.sourceType.camera
'
),
},
{
title
:
t
(
'
uni.chooseImage.sourceType.album
'
),
},
],
},
(
e
)
=>
{
switch
(
e
.
index
)
{
case
1
:
openCamera
()
break
case
2
:
openAlbum
()
break
default
:
errorCallback
()
break
}
}
)
},
ChooseImageProtocol
,
ChooseImageOptions
)
packages/uni-app-plus/src/service/api/media/chooseVideo.ts
0 → 100644
浏览文件 @
bb6ef672
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
,
API_CHOOSE_VIDEO
,
ChooseVideoOptions
,
ChooseVideoProtocol
,
defineAsyncApi
,
}
from
'
@dcloudio/uni-api
'
export
const
chooseVideo
=
defineAsyncApi
<
API_TYPE_CHOOSE_VIDEO
>
(
API_CHOOSE_VIDEO
,
({
sourceType
,
compressed
,
maxDuration
,
camera
},
{
resolve
,
reject
})
=>
{
initI18nChooseVideoMsgsOnce
()
const
{
t
}
=
useI18n
()
const
errorCallback
=
warpPlusErrorCallback
(
reject
)
function
successCallback
(
tempFilePath
:
string
=
''
)
{
const
filename
=
`
${
TEMP_PATH
}
/compressed/
${
Date
.
now
()}
_
${
getFileName
(
tempFilePath
)}
`
const
compressVideo
:
Promise
<
string
>
=
compressed
?
new
Promise
((
resolve
)
=>
{
plus
.
zip
.
compressVideo
(
{
src
:
tempFilePath
,
filename
,
},
({
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
openAlbum
()
{
plus
.
gallery
.
pick
(
// NOTE 5+此API分单选和多选,多选返回files:string[]
// @ts-ignore
({
files
})
=>
successCallback
(
files
[
0
]),
errorCallback
,
{
filter
:
'
video
'
,
system
:
false
,
// 不启用 multiple 时 system 无效
multiple
:
true
,
maximum
:
1
,
filename
:
TEMP_PATH
+
'
/gallery/
'
,
permissionAlert
:
true
,
}
)
}
function
openCamera
()
{
const
plusCamera
=
plus
.
camera
.
getCamera
()
plusCamera
.
startVideoCapture
(
successCallback
,
errorCallback
,
{
index
:
camera
===
'
front
'
?
'
2
'
:
'
1
'
,
videoMaximumDuration
:
maxDuration
,
filename
:
TEMP_PATH
+
'
/camera/
'
,
})
}
if
(
sourceType
!
.
length
===
1
)
{
if
(
sourceType
!
.
includes
(
'
album
'
))
{
openAlbum
()
return
}
else
if
(
sourceType
!
.
includes
(
'
camera
'
))
{
openCamera
()
return
}
}
plus
.
nativeUI
.
actionSheet
(
{
cancel
:
t
(
'
uni.chooseVideo.cancel
'
),
buttons
:
[
{
title
:
t
(
'
uni.chooseVideo.sourceType.camera
'
),
},
{
title
:
t
(
'
uni.chooseVideo.sourceType.album
'
),
},
],
},
(
e
)
=>
{
switch
(
e
.
index
)
{
case
1
:
openCamera
()
break
case
2
:
openAlbum
()
break
default
:
errorCallback
()
break
}
}
)
},
ChooseVideoProtocol
,
ChooseVideoOptions
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录