import { request } from '@/utils/request'
import { getEnvBaseUrl } from '@/utils'

/** 上传图片 */
export const uploadImageAPI = (filePath: string, imageClassId: string) => {
  return new Promise<{ url: string }>((resolve, reject) => {
    uni.uploadFile({
      url: getEnvBaseUrl() + 'attachment/uploadImageForUser',
      filePath,
      name: 'img',
      formData: {
        image_class_id: imageClassId,
      },
      header: {
        'x-token': uni.getStorageSync('x-token') || '',
      },
      success: (res) => {
        const data = JSON.parse(res.data)
        if (data.code === 200) {
          resolve(data.data)
        } else {
          reject(new Error(data.message))
        }
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}
