创建新的 pickupFile 方法,分流到 pickupFile2Server 和 pickupFile2Cloud,统一返回 {_state, fileUrl}
This commit is contained in:
parent
9e2cff1227
commit
a894117881
50
index.js
50
index.js
@ -129,7 +129,7 @@ module.exports = {
|
||||
async callBackend({ backend = this.BACKEND, httpMethod = 'POST', apiVersion = 'api', apiWho, apiTodo, apiWhat = {} }) {
|
||||
console.log('👇 < BackendRequest > ', { apiWho, apiTodo, apiWhat }, ' < /BackendRequest > 👇')
|
||||
let result = {}
|
||||
if (backend === 'CLOUD') {
|
||||
if (backend === 'UNICLOUD') {
|
||||
let { /* success, header, requestedId, */ result: resultCloud = {} } = await uniCloud
|
||||
.callFunction({
|
||||
name: apiWho,
|
||||
@ -182,7 +182,7 @@ module.exports = {
|
||||
return result
|
||||
},
|
||||
|
||||
async pickupFile({
|
||||
async pickupFile2Server({
|
||||
mediaType = 'image',
|
||||
count = 1,
|
||||
sizeType = ['original', 'compressed'],
|
||||
@ -192,11 +192,12 @@ module.exports = {
|
||||
formData = {},
|
||||
name = 'file',
|
||||
} = {}) {
|
||||
if (uni.getStorageSync('_passtoken')) {
|
||||
header._passtoken = uni.getStorageSync('_passtoken')
|
||||
} else {
|
||||
return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null]
|
||||
}
|
||||
// 有的管理后台不需要登录就允许上传,例如 cmctoy。因此不要在这里依赖登录状态。
|
||||
// if (uni.getStorageSync('_passtoken')) {
|
||||
// header._passtoken = uni.getStorageSync('_passtoken')
|
||||
// } else {
|
||||
// return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null]
|
||||
// }
|
||||
|
||||
let filePath
|
||||
if (mediaType === 'image') {
|
||||
@ -216,15 +217,24 @@ module.exports = {
|
||||
}
|
||||
|
||||
uni.showLoading()
|
||||
let [errorUpload, response] = await uni.uploadFile({ url: this.makeServerUrl(url), filePath, name, header, formData })
|
||||
let [errorUpload, { data, statusCode } = {}] = await uni.uploadFile({ url: this.makeServerUrl(url), filePath, name, header, formData })
|
||||
// url 用后台 Multer 处理返回 { _state, filename, originalname, mimetype, size, path },
|
||||
// uni.uploadFile 存在 data 里返回
|
||||
uni.hideLoading()
|
||||
|
||||
if (response && response.data) {
|
||||
if (typeof(data)==='string') {
|
||||
try {
|
||||
response.data = JSON.parse(response.data)
|
||||
data = JSON.parse(data)
|
||||
} catch (exception) {}
|
||||
}
|
||||
return [errorUpload, response]
|
||||
// return [errorUpload, response]
|
||||
if (data) {
|
||||
return { _state: 'SUCCESS', file: data }
|
||||
}else {
|
||||
return { _state: 'CLIENT_FAIL_UPLOAD_FILE', errorUpload }
|
||||
}
|
||||
}else {
|
||||
return { _state: 'CLIENT_FAIL_CHOOSE_FILE' }
|
||||
}
|
||||
|
||||
return [{ _ERROR: 'USER_CANCELED' }, null]
|
||||
@ -265,7 +275,7 @@ module.exports = {
|
||||
// 20200915测试,阿里云支持上传 *.mov 了。
|
||||
if (!/\.(mp4|mov)$/i.test(cloudPath)) cloudPath = cloudPath + '.mp4'
|
||||
} else {
|
||||
return { _state: 'UNKNOWN_MEDIA_TYPE' }
|
||||
return { _state: 'CLIENT_FAIL_UNKNOWN_MEDIA_TYPE' }
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
@ -290,7 +300,21 @@ module.exports = {
|
||||
return { _state: 'SUCCESS', fileID, requestId }
|
||||
}
|
||||
}
|
||||
return { _state: 'FAIL' }
|
||||
return { _state: 'CLIENT_FAIL_CHOOSE_FILE' }
|
||||
},
|
||||
|
||||
async pickupFile({
|
||||
backend = this.BACKEND,
|
||||
mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], maxDuration,
|
||||
url, header = {}, formData = {}, name = 'file',
|
||||
}) {
|
||||
if (backend==='UNICLOUD'){
|
||||
const resultCloud = await this.pickupFile2Cloud({mediaType, count, sizeType, sourceType, maxDuration })
|
||||
return Object.assign(resultCloud, { fileUrl: resultCloud.fileID })
|
||||
}else if (backend==='SERVER' && url){
|
||||
const resultServer = await this.pickupFile2Server({mediaType, count, sizeType, sourceType, maxDuration, url, header, formData, name})
|
||||
return Object.assign(resultServer, {fileUrl: resultServer.file ? this.makeBgUrl(resultServer.file.filename) : undefined})
|
||||
}
|
||||
},
|
||||
|
||||
openUrl(url) {
|
||||
|
Loading…
Reference in New Issue
Block a user