This commit is contained in:
陆柯 2020-07-13 13:24:10 +08:00
parent 73d810197d
commit 9f005f2112

View File

@ -30,35 +30,19 @@ module.exports = {
return [error, response] return [error, response]
}, },
async pickupFile({type='image', count=1, mediaType, sizeType, sourceType, compress=false, url, header={}, formData={}, name='file'}){ // choose and upload file async pickupFile({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], url, header = {}, formData = {}, name = 'file' } = {}) {
let picked let filePath
if (type==='image'){ if (mediaType === 'image') {
picked = await uni.chooseImage({count, sizeType}) let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
}else if (type==='video'){ filePath = tempFilePaths[0]
picked = await uni.chooseVideo({count, compressed:compress, sourceType}) } else if (mediaType === 'video') {
let [errorChoose, { tempFilePath }] = await uni.chooseVideo({ sourceType })
filePath = tempFilePath
} else { } else {
picked = await uni.chooseMedia({count, mediaType, sizeType, sourceType}) return [{ _ERROR: 'UNKNOWN_MEDIATYPE' }, null]
} }
let [errorChoose, {tempFilePaths, tempFiles}={}] = picked
if (!errorChoose){ if (filePath) {
if (compress && tempFiles[0].size>1048576){
console.log('========= compressing -------')
await uni.compressImage({ // compressImage not implemented yet in H5
src:tempFiles[0].path,
quality: 50,
success: res => {
console.log('======= compressed-------')
console.log(res)
tempFilePaths[0] = res.tempFilePath
},
fail: err => {
console.log('======= compress failed ====')
console.log(err)
}
})
}
if (uni.getStorageSync('_passtoken')) { if (uni.getStorageSync('_passtoken')) {
header._passtoken = uni.getStorageSync('_passtoken') header._passtoken = uni.getStorageSync('_passtoken')
@ -71,7 +55,7 @@ module.exports = {
} }
uni.showLoading() uni.showLoading()
let [errorUpload, response] = await uni.uploadFile({ filePath: tempFilePaths[0], url: this.makeUrl(url), header, formData, name }) let [errorUpload, response] = await uni.uploadFile({ filePath: filePath, url: this.makeUrl(url), header, formData, name })
uni.hideLoading() uni.hideLoading()
if (response && response.data) { if (response && response.data) {
@ -81,13 +65,13 @@ module.exports = {
} }
return [errorUpload, response] return [errorUpload, response]
}else{
return [{ _ERROR:'USER_CANCELED'}, null]
} }
return [{ _ERROR: 'USER_CANCELED' }, null]
}, },
async uploadFile({url, name='file', formData={}, header={}}){ async uploadFile({ url, name = 'file', formData = {}, header = {} } = {}) {
url = this.makeUrl(url) url = this.makeUrl(url)
if (uni.getStorageSync('_passtoken')) { if (uni.getStorageSync('_passtoken')) {
header._passtoken = uni.getStorageSync('_passtoken') header._passtoken = uni.getStorageSync('_passtoken')
@ -108,6 +92,45 @@ module.exports = {
return [error, response] return [error, response]
}, },
async pickupFile2Cloud({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'] } = {}) {
let filePath, cloudPath
if (mediaType === 'image') {
let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
filePath = tempFilePaths[0]
cloudPath = 'cloud.jpg'
// #ifdef H5
cloudPath = tempFiles[0].name
// #endif
} else if (mediaType === 'video') {
let [errorChoose, { tempFilePath, tempFile, name }] = await uni.chooseVideo({ sourceType })
filePath = tempFilePath
cloudPath = 'cloud.mp4'
// #ifdef H5
cloudPath = name
// #endif
} else {
return { _state: 'FAIL' }
}
if (filePath) {
uni.showLoading()
const { fileID, requestId } = await uniCloud.uploadFile({
filePath: filePath,
cloudPath: cloudPath,
fileType: mediaType,
onUploadProgress: function (progressEvent) {
console.log(progressEvent);
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
}
})
uni.hideLoading()
console.log('文件上传结果:', { fileID, requestId })
return { _state: 'SUCCESS', fileID, requestId }
}
return { _state: 'FAIL' }
},
openUrl(url) { openUrl(url) {
// #ifdef APP-PLUS // #ifdef APP-PLUS
plus.runtime.openURL(url) plus.runtime.openURL(url)