u
This commit is contained in:
parent
73d810197d
commit
9f005f2112
83
index.js
83
index.js
@ -30,35 +30,19 @@ module.exports = {
|
||||
return [error, response]
|
||||
},
|
||||
|
||||
async pickupFile({type='image', count=1, mediaType, sizeType, sourceType, compress=false, url, header={}, formData={}, name='file'}){ // choose and upload file
|
||||
let picked
|
||||
if (type==='image'){
|
||||
picked = await uni.chooseImage({count, sizeType})
|
||||
}else if (type==='video'){
|
||||
picked = await uni.chooseVideo({count, compressed:compress, sourceType})
|
||||
async pickupFile({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], url, header = {}, formData = {}, name = 'file' } = {}) {
|
||||
let filePath
|
||||
if (mediaType === 'image') {
|
||||
let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
|
||||
filePath = tempFilePaths[0]
|
||||
} else if (mediaType === 'video') {
|
||||
let [errorChoose, { tempFilePath }] = await uni.chooseVideo({ sourceType })
|
||||
filePath = tempFilePath
|
||||
} else {
|
||||
picked = await uni.chooseMedia({count, mediaType, sizeType, sourceType})
|
||||
return [{ _ERROR: 'UNKNOWN_MEDIATYPE' }, null]
|
||||
}
|
||||
let [errorChoose, {tempFilePaths, tempFiles}={}] = picked
|
||||
|
||||
if (!errorChoose){
|
||||
|
||||
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 (filePath) {
|
||||
|
||||
if (uni.getStorageSync('_passtoken')) {
|
||||
header._passtoken = uni.getStorageSync('_passtoken')
|
||||
@ -71,7 +55,7 @@ module.exports = {
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
if (response && response.data) {
|
||||
@ -81,13 +65,13 @@ module.exports = {
|
||||
}
|
||||
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)
|
||||
if (uni.getStorageSync('_passtoken')) {
|
||||
header._passtoken = uni.getStorageSync('_passtoken')
|
||||
@ -108,6 +92,45 @@ module.exports = {
|
||||
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) {
|
||||
// #ifdef APP-PLUS
|
||||
plus.runtime.openURL(url)
|
||||
|
Loading…
Reference in New Issue
Block a user