- Introduced a utility function to ensure absolute media URLs for user profile images. - Updated the user cache synchronization method to handle profile data more effectively, including avatar, nickname, phone, and MBTI. - Refactored the avatar upload process to utilize the new URL handling function, improving reliability. - Modified the super article editor to focus on image uploads instead of file uploads, enhancing user experience with clearer labeling and prompts. This update aims to streamline user profile management and improve the image upload experience in the application.
56 lines
1.7 KiB
JavaScript
56 lines
1.7 KiB
JavaScript
/**
|
||
* 小程序统一文件上传(卡若创业派对)
|
||
*
|
||
* 必须调用 soul-api:`POST /api/miniprogram/upload`(multipart:file + folder)。
|
||
* 禁止在小程序内配置 OSS AK/SK 或直传阿里云;对象存储由服务端按
|
||
* 「管理端 → 系统设置 → OSS」中的 oss_config 优先写 OSS;未配置或失败时服务端自动落本地 uploads/。
|
||
*
|
||
* @param {{ filePath: string, folder?: string, fileFieldName?: string }} opts
|
||
* @returns {Promise<{ success: boolean, url?: string, data?: { url?: string } }>}
|
||
*/
|
||
function getUploadUrl(app) {
|
||
const base = String((app && app.globalData && app.globalData.baseUrl) || '').replace(/\/$/, '')
|
||
return base + '/api/miniprogram/upload'
|
||
}
|
||
|
||
function uploadByApi(opts) {
|
||
const { filePath, folder = 'avatars', fileFieldName = 'file' } = opts || {}
|
||
if (!filePath) {
|
||
return Promise.reject(new Error('缺少文件路径'))
|
||
}
|
||
const app = getApp()
|
||
const url = getUploadUrl(app)
|
||
const token = wx.getStorageSync('token')
|
||
const header = {}
|
||
if (token) {
|
||
header.Authorization = `Bearer ${token}`
|
||
}
|
||
return new Promise((resolve, reject) => {
|
||
wx.uploadFile({
|
||
url,
|
||
filePath,
|
||
name: fileFieldName,
|
||
formData: { folder },
|
||
header,
|
||
success: (res) => {
|
||
try {
|
||
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||
if (data && data.success) resolve(data)
|
||
else reject(new Error((data && data.error) || '上传失败'))
|
||
} catch (_) {
|
||
reject(new Error('解析上传响应失败'))
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
const msg = (err && err.errMsg) || '网络错误'
|
||
reject(new Error(msg))
|
||
},
|
||
})
|
||
})
|
||
}
|
||
|
||
module.exports = {
|
||
uploadByApi,
|
||
getUploadUrl,
|
||
}
|