| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { BASE_URL } from '../config/api.js'
- export function uploadFile(filePath, folder = 'feedback') {
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: `${BASE_URL}/public/attachment/upload`,
- filePath,
- name: 'file',
- formData: { folder },
- success: (res) => {
- try {
- const body = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
- if (body.code === 200) {
- resolve(body.data)
- return
- }
- uni.showToast({ title: body.message || '上传失败', icon: 'none' })
- reject(body)
- } catch (e) {
- reject(e)
- }
- },
- fail: reject
- })
- })
- }
- export function chooseAndUploadImages(count = 3, folder = 'feedback') {
- return new Promise((resolve, reject) => {
- uni.chooseImage({
- count,
- success: async (res) => {
- try {
- const urls = []
- for (const path of res.tempFilePaths || []) {
- const data = await uploadFile(path, folder)
- if (data?.url) urls.push(data.url)
- }
- resolve(urls)
- } catch (e) {
- reject(e)
- }
- },
- fail: reject
- })
- })
- }
|