135 lines
3.3 KiB
JavaScript
135 lines
3.3 KiB
JavaScript
import request from '@/utils/request'
|
||
import config from '@/config'
|
||
import { useUserStore } from '@/store/modules/user'
|
||
|
||
const BASE_URL = config.baseUrl
|
||
|
||
/**
|
||
* 上传单个图片
|
||
* @param {File} file 图片文件
|
||
* @returns {Promise}
|
||
*/
|
||
export function uploadImage(file) {
|
||
const formData = new FormData()
|
||
formData.append('file', file)
|
||
|
||
return request({
|
||
url: '/upload/image',
|
||
method: 'post',
|
||
data: formData,
|
||
header: {
|
||
'Content-Type': 'multipart/form-data'
|
||
}
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 上传多个图片
|
||
* @param {Array<File>} files 图片文件数组
|
||
* @returns {Promise}
|
||
*/
|
||
export function uploadImages(files) {
|
||
const formData = new FormData()
|
||
files.forEach(file => {
|
||
formData.append('files', file)
|
||
})
|
||
|
||
return request({
|
||
url: '/upload/images',
|
||
method: 'post',
|
||
data: formData,
|
||
header: {
|
||
'Content-Type': 'multipart/form-data'
|
||
}
|
||
})
|
||
}
|
||
|
||
/**
|
||
* uni-app 上传单个图片(使用 uni.uploadFile)
|
||
* @param {String} filePath 本地文件路径
|
||
* @param {String} type 文件类型 image/video
|
||
* @returns {Promise}
|
||
*/
|
||
export function uniUploadImage(filePath, type = 'image') {
|
||
return new Promise((resolve, reject) => {
|
||
// 使用pinia store获取token
|
||
const userStore = useUserStore()
|
||
|
||
// 检查登录状态
|
||
if (!userStore.isLogin) {
|
||
reject(new Error('请先登录'))
|
||
return
|
||
}
|
||
|
||
const token = userStore.token
|
||
const endpoint = type === 'video' ? '/upload/video' : '/upload/image'
|
||
|
||
console.log('开始上传文件:', {
|
||
filePath,
|
||
type,
|
||
endpoint,
|
||
url: BASE_URL + endpoint,
|
||
hasToken: !!token,
|
||
isLogin: userStore.isLogin
|
||
})
|
||
|
||
uni.uploadFile({
|
||
url: BASE_URL + endpoint,
|
||
filePath: filePath,
|
||
name: 'file',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`
|
||
},
|
||
success: (res) => {
|
||
console.log('上传响应:', res)
|
||
|
||
if (res.statusCode === 200) {
|
||
try {
|
||
const data = JSON.parse(res.data)
|
||
console.log('解析后的响应数据:', data)
|
||
|
||
if (data.code === 0) {
|
||
resolve(data.data)
|
||
} else {
|
||
reject(new Error(data.message || '上传失败'))
|
||
}
|
||
} catch (e) {
|
||
console.error('解析响应失败:', e, res.data)
|
||
reject(new Error('解析响应失败'))
|
||
}
|
||
} else if (res.statusCode === 401) {
|
||
// token过期,清除登录状态
|
||
userStore.logout()
|
||
reject(new Error('登录已过期,请重新登录'))
|
||
} else {
|
||
console.error('上传失败,状态码:', res.statusCode, res)
|
||
reject(new Error(`上传失败(${res.statusCode})`))
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
console.error('上传请求失败:', err)
|
||
reject(new Error(err.errMsg || '网络错误'))
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* uni-app 上传视频
|
||
* @param {String} filePath 本地文件路径
|
||
* @returns {Promise}
|
||
*/
|
||
export function uniUploadVideo(filePath) {
|
||
return uniUploadImage(filePath, 'video')
|
||
}
|
||
|
||
/**
|
||
* uni-app 上传多个图片(使用 uni.uploadFile)
|
||
* @param {Array<String>} filePaths 本地文件路径数组
|
||
* @returns {Promise}
|
||
*/
|
||
export function uniUploadImages(filePaths) {
|
||
const uploadPromises = filePaths.map(filePath => uniUploadImage(filePath))
|
||
return Promise.all(uploadPromises)
|
||
}
|