import CommonContent from "@/api/CommonContent";
import type { UploaderAction } from "@/components/form/Uploader";

export function useImageSimpleUploadCo(additionData?: Record<string, any>) {
  return (action: UploaderAction) => {
    action.onStart('正在上传');
    let task: UniApp.UploadTask | null = null;
    CommonContent.uploadFile(action.item.filePath, 'image', 'file', additionData, (t) => {
      task = t;
      task.onProgressUpdate((res) => {
        action.onProgress(res.progress);
      });
    })
      .then((res) => {
        action.onProgress(100);
        action.onFinish({
          uploadedUrl: res.fullurl,
          previewUrl: res.fullurl,
        }, '上传成功');
      }).catch((err) => {
        action.onError?.(err);
      })
    return () => {
      task?.abort();
    };
  }
}

