Files
Mycontent/miniprogram/utils/miniprogramUpload.js
乘风 2645212d47 feat: update API base URLs and enhance icon designs
- Updated the API base URLs in app.js to point to local development server for testing.
- Enhanced SVG icons with gradient fills for improved visual appeal across various components.
- Refactored the avatar upload functionality in avatar-nickname.js and profile-edit.js to utilize a new upload utility for better code maintainability.
- Improved the layout and styling of the my page and super article editor for a more user-friendly interface.

This update aims to streamline development processes and enhance the overall user experience in the application.
2026-05-09 16:00:16 +08:00

56 lines
1.7 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.

/**
* 小程序统一文件上传(卡若创业派对)
*
* 必须调用 soul-api`POST /api/miniprogram/upload`multipartfile + folder
* 禁止在小程序内配置 OSS AK/SK 或直传阿里云;对象存储由服务端按
* 「管理端 → 系统设置 → OSS」中的 oss_config 写入(未配置或失败时可落本地,以后端为准)。
*
* @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,
}