由于uni.chooseFile api不支持app端,所以使用h5+的api来实现
网址如下:
h5+方法网址如下
获取摄像头对象 HTML5+ API Reference
获取相册 HTML5+ API Reference
//在mounted中新建摄像头对象
mounted() {
this.camera = plus.camera.getCamera() //获取摄像头管理对象
}
methods: {
handleUploadType(type) {
// 1 拍照 2拍视频 3选择照片 4 选择视频
//下方上传成功的res返回的都是文件路径,拿到上传的文件路径传给后端拿到自己想要的信息
let _this = this
switch (type) {
case 1:
_this.camera.captureImage(function (res) {
_this.uploadFileImageSuccess(res)
})
break
case 2:
_this.camera.startVideoCapture(function (res) {
_this.uploadFileVideoSuccess(res)
})
break
case 3:
plus.gallery.pick(
function (res) {
_this.uploadFileImageSuccess(res)
},
function (e) {
console.log('取消选择图片', e)
},
{ filter: 'image' }
)
break
case 4:
plus.gallery.pick(
function (res) {
console.log('选择的视频', res)
_this.uploadFileVideoSuccess(res)
},
function (e) {
console.log('取消选择视频', e)
},
{ filter: 'video' }
)
break
}
_this.changeOverlay = false
},
}
uploadFileVideoSuccess(res) {
let _this = this
_this.upLoading = true
uploadFile(
{
url: '/infra/file/upload',
filePath: res,
name: 'file',
formData: {
fileType: 4,
businessCode: 6,
},
},
false
)
.then((data) => {
_this.upLoading = true
this.uploadList.push(data)
this.videoType = true
//获取拿到的视频的详细信息
uni.getVideoInfo({
src: res,
success: function (res) {
data.duration = _this.convertSecondsToTime(res.duration)
uni.getImageInfo({
src: data.videoPicPath,
success: function (image) {
_this.uploadVideoForm.snapshotHeight = image.height
_this.uploadVideoForm.snapshotWidth = image.width
_this.uploadVideoForm.snapshotSize = null
},
})
},
fail: function (err) {
console.log('获取失败', err)
},
})
})
.finally(() => {
_this.upLoading = false
})
},
uploadFileImageSuccess(res) {
let _this = this
_this.upLoading = true
uploadFile(
{
url: '/infra/file/upload',
filePath: res,
name: 'file',
formData: {},
},
false
)
.then((data) => {
_this.upLoading = true
this.uploadList.push(data)
})
.finally(() => {
_this.upLoading = false
})
},