import Taro from '@tarojs/taro';
export const chooseAndUploadImage = (url, user = 'test') => {
  return new Promise((resolve, reject) => {
    Taro.chooseImage({
      success (chooseRes) {
        const tempFilePaths = chooseRes.tempFilePaths;
        
        if (tempFilePaths.length === 0) {
          reject(new Error('No image selected'));
          return;
        }

        Taro.uploadFile({
          url: url, // 上传的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': user
          },
          success (uploadRes) {
            const data = uploadRes.data;
            resolve(data); // 成功上传后返回数据
          },
          fail (error) {
            reject(error); // 上传失败时返回错误
          }
        });
      },
      fail (chooseError) {
        reject(chooseError); // 选择图片失败时返回错误
      }
    });
  });
};
