149 lines
5.0 KiB
JavaScript
149 lines
5.0 KiB
JavaScript
/**
|
|
* Purpose: request supports file.js by holding or executing the request page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const request = require('../utils/request')
|
|
/**
|
|
* Purpose: actionGuard supports file.js by holding or executing the action guard page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const actionGuard = require('../utils/action-guard')
|
|
const { ADMIN_API_BASE_URL, REQUEST_TIMEOUT } = require('../utils/config')
|
|
|
|
/**
|
|
* Purpose: normalizeUploadResponse supports file.js by holding or executing the normalize upload response page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function normalizeUploadResponse(response) {
|
|
if (!response) {
|
|
throw new Error('上传失败')
|
|
}
|
|
|
|
if (response.statusCode === 401) {
|
|
throw new Error('登录已过期,请重新登录')
|
|
}
|
|
|
|
if (response.statusCode >= 400) {
|
|
throw new Error('上传失败')
|
|
}
|
|
|
|
let payload = response.data
|
|
if (typeof payload === 'string') {
|
|
try {
|
|
payload = JSON.parse(payload)
|
|
} catch (error) {}
|
|
}
|
|
|
|
if (payload && typeof payload === 'object' && Object.prototype.hasOwnProperty.call(payload, 'code')) {
|
|
if (payload.code === 200) {
|
|
return payload.data
|
|
}
|
|
throw new Error(payload.message || '上传失败')
|
|
}
|
|
|
|
return payload
|
|
}
|
|
|
|
module.exports = {
|
|
/**
|
|
* Purpose: initChunkUpload supports file.js by holding or executing the init chunk upload page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
initChunkUpload(data) {
|
|
return request({
|
|
url: '/object-storage-file/upload/init',
|
|
method: 'POST',
|
|
data
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: uploadChunkFile supports file.js by holding or executing the upload chunk file page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
uploadChunkFile(options) {
|
|
const signedHeaders = request.buildSignatureHeaders({
|
|
url: '/object-storage-file/upload/chunk',
|
|
method: 'POST',
|
|
formData: {
|
|
uploadId: String(options.uploadId),
|
|
chunkIndex: String(options.chunkIndex),
|
|
totalChunks: String(options.totalChunks)
|
|
}
|
|
})
|
|
|
|
return actionGuard.run(
|
|
null,
|
|
`POST:/object-storage-file/upload/chunk:${String(options.uploadId)}:${String(options.chunkIndex)}`,
|
|
() => new Promise((resolve, reject) => {
|
|
const task = wx.uploadFile({
|
|
url: `${ADMIN_API_BASE_URL}/object-storage-file/upload/chunk`,
|
|
filePath: options.filePath,
|
|
name: 'chunk',
|
|
timeout: REQUEST_TIMEOUT,
|
|
formData: {
|
|
uploadId: String(options.uploadId),
|
|
chunkIndex: String(options.chunkIndex),
|
|
totalChunks: String(options.totalChunks)
|
|
},
|
|
header: signedHeaders,
|
|
success: response => {
|
|
try {
|
|
resolve(normalizeUploadResponse(response))
|
|
} catch (error) {
|
|
reject(error)
|
|
}
|
|
},
|
|
fail: error => {
|
|
reject(new Error(error.errMsg || '上传分片失败'))
|
|
}
|
|
})
|
|
|
|
if (task && typeof task.onProgressUpdate === 'function' && typeof options.onProgress === 'function') {
|
|
task.onProgressUpdate(progress => {
|
|
options.onProgress(progress)
|
|
})
|
|
}
|
|
}),
|
|
{
|
|
message: '上传处理中,请勿重复操作'
|
|
}
|
|
)
|
|
},
|
|
|
|
/**
|
|
* Purpose: completeChunkUpload supports file.js by holding or executing the complete chunk upload page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
completeChunkUpload(uploadId) {
|
|
return request({
|
|
url: '/object-storage-file/upload/complete',
|
|
method: 'POST',
|
|
data: {
|
|
uploadId
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: cancelChunkUpload supports file.js by holding or executing the cancel chunk upload page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
cancelChunkUpload(uploadId) {
|
|
return request({
|
|
url: `/object-storage-file/upload/${uploadId}`,
|
|
method: 'DELETE'
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: getSignedUrl supports file.js by holding or executing the get signed url page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
getSignedUrl(fileId) {
|
|
return request({
|
|
url: `/object-storage-file/get-signed-url/${fileId}`
|
|
})
|
|
}
|
|
}
|