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

26 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {Platform} from 'react-native';
import * as DocumentPicker from 'expo-document-picker';
import {File,FileMode,Paths,Directory} from 'expo-file-system';
import * as Sharing from 'expo-sharing';
import {fetch as expoFetch} from 'expo/fetch';
import type {ApiClient} from './core/network';
import type {Row} from './core/modules';
import type {UploadSource} from './core/managed-transfer';
export async function pickManagedFile():Promise<UploadSource|null>{
const chosen=await DocumentPicker.getDocumentAsync({type:'*/*',multiple:false,copyToCacheDirectory:true});if(chosen.canceled)return null;const asset=chosen.assets[0];
if(Platform.OS==='web'){const blob=asset.file;if(!blob)throw new Error('无法读取选择的文件');return {name:asset.name,size:blob.size,read:async(start,length)=>new Uint8Array(await blob.slice(start,start+length).arrayBuffer()),close(){}};}
const file=new File(asset.uri);if(file.size>1024**3||!file.size){try{if(file.uri.startsWith(Paths.cache.uri))file.delete();}catch{}throw new Error('请选择非空文件,单个最多1GB');}const handle=file.open(FileMode.ReadOnly),chunkFile=new File(Paths.cache,'upload-chunk-'+Date.now()+'-'+Math.random().toString(16).slice(2)+'.bin');
return {name:asset.name,size:file.size,read:async(start,length)=>{handle.offset=start;return handle.readBytes(length);},part:async bytes=>{chunkFile.write(bytes);return {uri:chunkFile.uri,name:'chunk.bin',type:'application/octet-stream'};},close(){handle.close();try{if(chunkFile.exists)chunkFile.delete();}catch{}try{if(file.uri.startsWith(Paths.cache.uri))file.delete();}catch{}}};
}
export async function downloadManagedFile(api:ApiClient,file:Row,signal:AbortSignal,onProgress:(value:number)=>void,action:'open'|'save'='save'){
const path=`/system/files/${encodeURIComponent(file.id)}/content`,name=String(file.name||'file').replace(/[\\/:*?"<>|\x00-\x1f]/g,'_').slice(0,160);
if(Platform.OS==='web'){
const picker=(globalThis as any).showSaveFilePicker;if(picker){const out=await(await picker({suggestedName:name})).createWritable();try{await api.download(path,b=>out.write(b),{signal,onProgress:(n,total)=>onProgress(total?Math.round(n/total*100):0)});await out.close();}catch(e){await out.abort();throw e;}return;}
const chunks:Uint8Array<ArrayBuffer>[]=[];await api.download(path,async b=>{chunks.push(Uint8Array.from(b));},{signal,maxBytes:50*1024**2,onProgress:(n,total)=>onProgress(total?Math.round(n/total*100):0)});const url=URL.createObjectURL(new Blob(chunks)),a=document.createElement('a');a.href=url;a.download=name;a.click();setTimeout(()=>URL.revokeObjectURL(url),10000);return;
}
const dir=new Directory(Paths.cache,'managed-downloads');dir.create({idempotent:true,intermediates:true});const output=new File(dir,Date.now()+'-'+name);output.create();const handle=output.open(FileMode.WriteOnly);
try{await api.download(path,b=>handle.writeBytes(b),{signal,transport:expoFetch as typeof fetch,onProgress:(n,total)=>onProgress(total?Math.round(n/total*100):0)});handle.close();if(signal.aborted)throw new Error('下载已取消');if(action==='open'&&await output.canPreview())await output.preview();else if(await Sharing.isAvailableAsync())await Sharing.shareAsync(output.uri,{dialogTitle:'保存或打开文件'});else throw new Error('系统没有可用于打开文件的应用');}
catch(error){try{handle.close();}catch{}try{output.delete();}catch{}throw error;}
// Keep successful files in cache so the selected viewer can finish reading them.
}