cmvr-wms/mobile-app/src/core/upload-file.ts
lixiaolong 89c962223a feat(warehouse): 优化采购报价权限控制和文件管理功能
- 实现基于角色的采购报价权限检查,仅超级管理员和采购经理角色可查看报价
- 替换原有的仓库采购管理权限检查为新的报价权限函数
- 在采购单据组件中更新报价字段显示权限逻辑
- 集成文件管理功能,支持文档附件上传下载
- 添加移动端应用更新提示和发布功能
- 增强采购历史更正服务的安全访问控制
- 重构报价访问工具类以支持细粒度字段过滤
- 添加单元测试验证新的权限控制逻辑
2026-10-29 09:58:51 +08:00

33 lines
2.6 KiB
TypeScript

// Expo fetch accepts Blob parts, while the Android picker returns local URI parts.
// Read only local files through the native file reader; HTTP uploads still use
// the authenticated API transport and its redirect/cancellation policy.
type LocalFile = {uri:string;name?:string;type?:string};
function readLocalFile(file:LocalFile,signal:AbortSignal):Promise<Blob>{
if(!/^(?:file|content):\/\//i.test(file.uri))return Promise.reject(new Error('附件地址无效,请重新选择文件。'));
return new Promise((resolve,reject)=>{
if(signal.aborted){reject(new Error('文件读取已取消。'));return;}
const request=new XMLHttpRequest();let settled=false;
const finish=(error?:Error,blob?:Blob)=>{if(settled)return;settled=true;signal.removeEventListener('abort',abort);request.onload=request.onerror=request.onabort=request.ontimeout=null;if(error)reject(error);else resolve(blob!);};
const abort=()=>{request.abort();finish(new Error('文件读取已取消。'));};
request.onload=()=>{const blob=request.response;if((request.status!==0&&request.status!==200)||!(blob instanceof Blob)||!blob.size){finish(new Error('无法读取本地附件,请重新拍摄或选择文件后重试。'));return;}finish(undefined,blob.type===file.type||!file.type?blob:blob.slice(0,blob.size,file.type));};
request.onerror=()=>finish(new Error('无法读取本地附件,请重新拍摄或选择文件后重试。'));
request.onabort=()=>finish(new Error('文件读取已取消。'));
request.ontimeout=()=>finish(new Error('读取本地附件超时,请重新选择文件。'));
signal.addEventListener('abort',abort,{once:true});
try{request.open('GET',file.uri,true);request.responseType='blob';request.timeout=30000;request.send();}
catch{finish(new Error('无法读取本地附件,请重新拍摄或选择文件后重试。'));}
});
}
export async function readableMultipart(body:FormData,signal:AbortSignal):Promise<FormData>{
const entries=Array.from((body as FormData & {entries():IterableIterator<[string,string|Blob|LocalFile]>}).entries());
const local=(value:unknown):value is LocalFile=>!!value&&typeof value==='object'&&!(value instanceof Blob)&&typeof (value as LocalFile).uri==='string';
if(!entries.some(([,value])=>local(value)))return body;
const prepared=new FormData();
for(const [name,value] of entries){
if(local(value))prepared.append(name,await readLocalFile(value,signal),value.name||'attachment');
else if(value instanceof Blob)prepared.append(name,value,(value as File).name||'attachment');
else prepared.append(name,value);
}
return prepared;
}