import { request } from '@/utils/request'
import { BASE_URL, DEFAULT_HEADERS } from '@/utils/config'

// 上传文件到 OSS
export function uploadFile(filePath) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token') || ''
    uni.uploadFile({
      url: BASE_URL + '/resource/oss/upload',
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': token ? `Bearer ${token}` : '',
        ...DEFAULT_HEADERS
      },
      success: (res) => {
        const resData = JSON.parse(res.data)
        if (resData.code === 200) {
          resolve(resData.data)
        } else {
          uni.showToast({ title: resData.msg || '上传失败', icon: 'none' })
          reject(resData.msg)
        }
      },
      fail: (err) => {
        uni.showToast({ title: '网络异常', icon: 'none' })
        reject(err)
      }
    })
  })
}
