43 行
1.2 KiB
JavaScript
43 行
1.2 KiB
JavaScript
/**
|
||
* 图片上传封装
|
||
* - 真实环境:POST /admin/upload(multipart),返回 { url }
|
||
*/
|
||
import config from '../config/index.js'
|
||
import storage from './storage.js'
|
||
|
||
/**
|
||
* 上传单张图片
|
||
* @param {String} filePath 本地临时路径
|
||
* @returns {Promise<{url:String}>}
|
||
*/
|
||
export function uploadImage(filePath) {
|
||
if (typeof uni === 'undefined' || !uni.uploadFile) {
|
||
return Promise.reject(new Error('当前环境不支持上传'))
|
||
}
|
||
return new Promise((resolve, reject) => {
|
||
uni.uploadFile({
|
||
url: `${config.baseURL}/${config.apiVersion}/admin/upload`,
|
||
filePath,
|
||
name: 'file',
|
||
header: {
|
||
Authorization: `Bearer ${storage.get(config.storageKeys.token, '')}`,
|
||
'X-Client': 'fenleixinxi',
|
||
},
|
||
success(res) {
|
||
try {
|
||
const body = JSON.parse(res.data)
|
||
if (body.code === 0 || body.code === 200) resolve(body.data || {})
|
||
else reject(new Error(body.message || '上传失败'))
|
||
} catch (e) {
|
||
reject(new Error('上传响应解析失败'))
|
||
}
|
||
},
|
||
fail(err) {
|
||
reject(new Error((err && err.errMsg) || '上传失败'))
|
||
},
|
||
})
|
||
})
|
||
}
|
||
|
||
export default { uploadImage }
|