wx-shop/api/upload.js
2025-11-16 18:59:10 +08:00

31 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function uploadFile(filePath) {
return new Promise((resolve, reject) => {
if (!filePath) {
const err = new Error('uploadFile requires a filePath parameter');
console.error('上传失败:未提供文件路径', err);
return reject(err);
}
// 获取token有就丢进请求头
const tokenString = wx.getStorageSync('access_token');
const header = {
'Authorization': `${tokenString}`
};
wx.uploadFile({
filePath: filePath,
name: 'file',
header: header,
url: 'http://scrm.1024tool.vip/api/xcx/upload/image',
success: (res) => {
const data = JSON.parse(res.data);
resolve('http://scrm.1024tool.vip/' + data.preview_image_url);
},
fail: (err) => {
console.error('上传失败', err);
reject(err);
}
});
})
}
export default uploadFile;