import{request as e}from"@cqsjjb/jjb-common-lib/http.js";import{useState as o}from"react";import{UPLOAD_FILE_TYPE_ENUM as i,UPLOAD_FILE_PATH_ENUM as r}from"../../enum/uploadFile/gwj/index.js";function t(t="object"){const[n,l]=o(!1);let a=0;const s=o=>{if(o)return a++,a>0&&l(!0),new Promise((t,n)=>{const{files:s=[],single:p=!0,params:f}=o;if(!s)return void console.error("【uploadFile】 缺少 files 参数");if(!Array.isArray(s))return void console.error("【uploadFile】 的 files 需要是一个数组");if(!f)return void console.error("【uploadFile】 缺少 options.params 参数");if(!f.type)return void console.error("【uploadFile】 缺少 options.params.type 参数");if(!Object.values(i).includes(f.type))return void console.error(`【uploadFile】 传入的 type 不在 UPLOAD_FILE_TYPE_ENUM 中，当前传入的 type 是 ${f.type}`);const d=r[f.type];if(!d)return void console.error(`【uploadFile】未找到 type ${f.type} 对应的 path `);if(!p){if(!f.hasOwnProperty("foreignKey"))return void console.error("【uploadFile】 的 single 是 false，缺少 options.params.foreignKey 参数");void 0!==f.foreignKey&&null!==f.foreignKey||(f.foreignKey="")}if(0===s.length)return a--,a<=0&&l(!1),void t(p?{filePath:""}:{id:f.foreignKey});const c=new FormData;if(s.forEach(e=>{e.originFileObj&&c.append("files",e.originFileObj)}),Object.keys(f).forEach(e=>{c.append(e,f[e])}),c.append("path",d),0===c.getAll("files").length)return a--,a<=0&&l(!1),void t(p?{filePath:s[0].filePath}:{id:f.foreignKey});e(p?"/basicInfo/imgFiles/save":"/basicInfo/imgFiles/batchSave","post",c,{"Content-Type":"multipart/form-data"}).then(e=>{t(p?{filePath:e.data.filePath}:{id:e.data.foreignKey})}).catch(e=>{n(e)}).finally(()=>{a--,a<=0&&l(!1)})});console.error("【uploadFile】 缺少 options 参数")};return"array"===t?[n,s]:{loading:n,uploadFile:s}}export{t as default};
