2026-11-03 17:57:12 +08:00
|
|
|
const { API_DOMAIN, REQUEST_TIMEOUT, ENV_VERSION } = require('./config')
|
2026-06-27 15:19:32 +08:00
|
|
|
const request = require('./request')
|
|
|
|
|
|
2026-10-31 21:02:37 +08:00
|
|
|
function normalizeUploadResponse(response, fallbackMessage, url) {
|
2026-06-27 15:19:32 +08:00
|
|
|
if (response.statusCode === 401) {
|
2026-10-31 21:02:37 +08:00
|
|
|
throw request.handleUnauthorized({ data: response.data }, url)
|
2026-06-27 15:19:32 +08:00
|
|
|
}
|
|
|
|
|
if (response.statusCode >= 400) {
|
|
|
|
|
const message = response.data && response.data.message ? response.data.message : fallbackMessage
|
|
|
|
|
throw new Error(message)
|
|
|
|
|
}
|
|
|
|
|
const payload = response.data
|
|
|
|
|
if (payload && typeof payload === 'object' && Object.prototype.hasOwnProperty.call(payload, 'code')) {
|
|
|
|
|
if (payload.code === 200) {
|
|
|
|
|
return payload.data
|
|
|
|
|
}
|
|
|
|
|
throw new Error(payload.message || fallbackMessage)
|
|
|
|
|
}
|
|
|
|
|
return payload
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function preserveResponseId(rawText, payload) {
|
|
|
|
|
if (!payload || typeof payload !== 'object' || !payload.data || typeof payload.data !== 'object') {
|
|
|
|
|
return payload
|
|
|
|
|
}
|
2026-10-30 03:46:24 +08:00
|
|
|
const raw = String(rawText || '')
|
|
|
|
|
const idMatch = raw.match(/"id"\s*:\s*"?(\d+)"?/)
|
|
|
|
|
if (idMatch && idMatch[1]) {
|
|
|
|
|
payload.data.id = idMatch[1]
|
|
|
|
|
}
|
|
|
|
|
const fileIdMatch = raw.match(/"fileId"\s*:\s*"?(\d+)"?/)
|
|
|
|
|
if (fileIdMatch && fileIdMatch[1]) {
|
|
|
|
|
payload.data.fileId = fileIdMatch[1]
|
2026-06-27 15:19:32 +08:00
|
|
|
}
|
|
|
|
|
return payload
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadFile(options) {
|
|
|
|
|
const method = 'POST'
|
2026-10-27 11:50:18 +08:00
|
|
|
const baseUrl = options.baseUrl || API_DOMAIN
|
2026-06-27 15:19:32 +08:00
|
|
|
const url = `${baseUrl}${options.url}`
|
|
|
|
|
const signatureHeaders = request.buildSignatureHeaders({
|
|
|
|
|
url: options.url,
|
|
|
|
|
requestUrl: url,
|
|
|
|
|
method,
|
|
|
|
|
data: options.formData || {},
|
|
|
|
|
params: options.formData || {},
|
|
|
|
|
header: {
|
|
|
|
|
'Content-Type': 'multipart/form-data'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
wx.uploadFile({
|
|
|
|
|
url,
|
|
|
|
|
filePath: options.filePath,
|
|
|
|
|
name: options.name,
|
|
|
|
|
timeout: options.timeout || REQUEST_TIMEOUT,
|
|
|
|
|
formData: options.formData || {},
|
2026-11-03 17:57:12 +08:00
|
|
|
header: { ...signatureHeaders, 'X-Mini-Program-Env-Version': ENV_VERSION },
|
2026-06-27 15:19:32 +08:00
|
|
|
success(response) {
|
|
|
|
|
try {
|
2026-10-31 21:02:37 +08:00
|
|
|
let parsedData = {}
|
|
|
|
|
if (response.data) {
|
|
|
|
|
try {
|
|
|
|
|
parsedData = JSON.parse(response.data)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (response.statusCode !== 401) throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const data = preserveResponseId(response.data, parsedData)
|
|
|
|
|
resolve(normalizeUploadResponse({ ...response, data }, options.errorMessage || '上传失败', options.url))
|
2026-06-27 15:19:32 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
reject(error)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail(error) {
|
|
|
|
|
reject(new Error(`文件上传失败:${url},原因:${error.errMsg || '未知错误'}`))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function chooseImageFilePath() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
wx.chooseMedia({
|
|
|
|
|
count: 1,
|
|
|
|
|
mediaType: ['image'],
|
|
|
|
|
sourceType: ['album', 'camera'],
|
|
|
|
|
sizeType: ['compressed'],
|
|
|
|
|
success(result) {
|
|
|
|
|
const filePath = result.tempFiles && result.tempFiles[0] ? result.tempFiles[0].tempFilePath : ''
|
|
|
|
|
resolve(filePath)
|
|
|
|
|
},
|
|
|
|
|
fail(error) {
|
|
|
|
|
const message = error && error.errMsg ? error.errMsg : ''
|
|
|
|
|
if (message.includes('cancel')) {
|
|
|
|
|
resolve('')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
reject(new Error(`选择图片失败:${message || '未知错误'}`))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadObjectFile(filePath) {
|
|
|
|
|
return uploadFile({
|
|
|
|
|
url: '/object-storage-file/upload',
|
|
|
|
|
filePath,
|
|
|
|
|
name: 'file',
|
|
|
|
|
errorMessage: '文件上传失败'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadProfileImage(filePath) {
|
|
|
|
|
return uploadFile({
|
|
|
|
|
url: '/user/upload-profile-image',
|
|
|
|
|
baseUrl: API_DOMAIN,
|
|
|
|
|
filePath,
|
|
|
|
|
name: 'file',
|
|
|
|
|
errorMessage: '头像上传失败'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2026-10-30 03:46:24 +08:00
|
|
|
uploadFile,
|
2026-06-27 15:19:32 +08:00
|
|
|
chooseImageFilePath,
|
|
|
|
|
uploadObjectFile,
|
|
|
|
|
uploadProfileImage
|
|
|
|
|
}
|