- 新增 PDF 文档支持,包括格式校验和完整性检查 - 扩展 Word、TXT 和 ZIP 格式附件处理能力 - 添加 Excel 附件可选择仅作为业务文件不解析行数据 - 实现业务助手表格布局配置新增库存和预算字段 - 集成通知、反馈通知等新的业务助手动作类型 - 完善部门管理包含父级部门和排序字段支持 - 增强业务规则校验涵盖更多系统管理场景 - 优化附件上传限制提示信息更准确 - 添加生成文件下载的安全性校验机制 - 扩展测试覆盖确保各业务动作规则正确性
50 lines
8.9 KiB
TypeScript
50 lines
8.9 KiB
TypeScript
import React,{useCallback,useEffect,useRef,useState} from 'react';
|
||
import {Image,KeyboardAvoidingView,Platform,Pressable,ScrollView,Text,View} from 'react-native';
|
||
import {Dialog as Alert} from '../dialogs';
|
||
import * as ImagePicker from 'expo-image-picker';
|
||
import {ApiClient,ApiError} from '../core/network';
|
||
import {feedbackPayload,feedbackStatuses} from '../core/account';
|
||
import {Button,Card,Field,Heading,Icon,Note,s,useTheme} from '../ui';
|
||
import {ProtectedImage} from '../files';
|
||
import type {Row} from '../core/modules';
|
||
import {useLeaveGuard,type LeaveGuard} from './Account';
|
||
|
||
export function Feedback({api,mode,bindLeave,open}:{api:ApiClient;mode:'create'|'list';bindLeave:(guard:LeaveGuard|null)=>void;open:(name:string)=>void}){
|
||
const t=useTheme(),[type,setType]=useState('BUG'),[title,setTitle]=useState(''),[description,setDescription]=useState(''),[images,setImages]=useState<Row[]>([]),[busy,setBusy]=useState(false),[error,setError]=useState(''),[uncertain,setUncertain]=useState(false),[success,setSuccess]=useState<Row|null>(null),[rows,setRows]=useState<Row[]>([]),[page,setPage]=useState(1),[total,setTotal]=useState(0),[detail,setDetail]=useState<Row|null>(null);
|
||
const lock=useRef(false),mounted=useRef(true),seq=useRef(0);
|
||
const dirty=mode==='create'&&!success&&!!(title||description||images.length);
|
||
useLeaveGuard(bindLeave,dirty,mode==='create'&&busy);
|
||
useEffect(()=>{if(!detail)return;bindLeave(()=>{if(!lock.current)setDetail(null);});return()=>bindLeave(null);},[bindLeave,detail]);
|
||
useEffect(()=>{mounted.current=true;return()=>{mounted.current=false;seq.current++;};},[]);
|
||
const load=useCallback(async(n:number)=>{if(lock.current)return;lock.current=true;setBusy(true);setError('');const serial=++seq.current;try{const data=await api.request<Row>('/system/feedback/list',{query:{all:false,pageNum:n,pageSize:10}});if(mounted.current&&serial===seq.current){setRows(data.rows||[]);setTotal(data.total||0);setPage(n);}}
|
||
catch(e){if(mounted.current)setError((e as Error).message);}finally{lock.current=false;if(mounted.current)setBusy(false);}},[api]);
|
||
useEffect(()=>{if(mode==='list')void load(1);},[load,mode]);
|
||
async function choose(){if(lock.current||uncertain||images.length>=5)return;lock.current=true;setBusy(true);setError('');try{
|
||
const result=await ImagePicker.launchImageLibraryAsync({mediaTypes:['images'],quality:.85,allowsMultipleSelection:false});if(result.canceled||!mounted.current)return;
|
||
const asset=result.assets[0];if(asset.fileSize!=null&&(asset.fileSize<=0||asset.fileSize>5*1024*1024)){setError('请选择不超过 5 MB 的图片(PNG、JPG 或 WebP)。');return;}
|
||
const name=asset.fileName||`feedback-${Date.now()}.jpg`,mime=asset.mimeType||'image/jpeg';if(!['image/png','image/jpeg','image/webp'].includes(mime)){setError('请使用 PNG、JPG 或 WebP 图片。');return;}
|
||
const body=new FormData();if(Platform.OS==='web'){body.append('file',await(await fetch(asset.uri)).blob(),name);}else body.append('file',{uri:asset.uri,type:mime,name} as unknown as Blob);
|
||
const data=await api.request<Row>('/system/feedback/images',{method:'POST',body});if(mounted.current)setImages(old=>[...old,{...data.data,uri:asset.uri}]);
|
||
}catch(e){if(mounted.current)setError((e as Error).message);}finally{lock.current=false;if(mounted.current)setBusy(false);}}
|
||
async function submit(){if(lock.current||uncertain)return;let body;try{body=feedbackPayload(type,title,description,images.map(i=>i.id));}catch(e){setError((e as Error).message);return;}
|
||
lock.current=true;setBusy(true);setError('');try{const data=await api.request<Row>('/system/feedback',{method:'POST',body});if(mounted.current){setSuccess(data.data);setTitle('');setDescription('');setImages([]);}}
|
||
catch(e){if(mounted.current){const unknown=e instanceof ApiError&&e.code===503;setUncertain(unknown);setError((e as Error).message+(unknown?' 请先在“我的反馈”核对是否已提交,不要重复发送。':''));}}
|
||
finally{lock.current=false;if(mounted.current)setBusy(false);}}
|
||
async function inspect(id:number){if(lock.current)return;lock.current=true;setBusy(true);setError('');try{const data=await api.request<Row>(`/system/feedback/${id}`);if(mounted.current)setDetail(data.data);}catch(e){if(mounted.current)setError((e as Error).message);}finally{lock.current=false;if(mounted.current)setBusy(false);}}
|
||
return <KeyboardAvoidingView style={s.fill} behavior={Platform.OS==='ios'?'padding':undefined}><ScrollView keyboardShouldPersistTaps="handled" contentContainerStyle={s.page}>
|
||
{mode==='create'?(success?<Card><Icon name="checkmark-circle" size={46} color="#239b78"/><Heading>你的建议,我们收到了</Heading><Note>反馈编号 #{success.id}。后续进展和回复可以在“我的反馈”中查看。</Note><Button title="查看我的反馈" onPress={()=>open('feedback-list')}/><Button title="继续反馈" secondary onPress={()=>setSuccess(null)}/></Card>:<>
|
||
<Heading>一起把系统变得更好</Heading><Note>遇到问题或有新的想法,都可以告诉我们。请勿上传密码、授权码等敏感信息。</Note>
|
||
<View style={s.row}><Button title="发现问题" secondary={type!=='BUG'} disabled={busy||uncertain} onPress={()=>setType('BUG')}/><Button title="功能建议" secondary={type!=='FEATURE'} disabled={busy||uncertain} onPress={()=>setType('FEATURE')}/></View>
|
||
<Field label="用一句话描述" value={title} onChange={setTitle} maxLength={100} disabled={busy||uncertain}/><Field label={type==='BUG'?'发生了什么?怎样操作会出现?':'你希望增加什么?能解决什么问题?'} value={description} onChange={setDescription} multiline maxLength={5000} disabled={busy||uncertain}/>
|
||
<Note>添加图片 · {images.length}/5 · 每张不超过 5 MB</Note><View style={s.row}>{images.map((image,index)=><View key={image.id} style={{gap:5,width:100}}><Image source={{uri:image.uri}} style={{width:100,height:85,borderRadius:12}}/><Button title={`移除第 ${index+1} 张`} secondary disabled={busy||uncertain} onPress={()=>setImages(old=>old.filter(i=>i.id!==image.id))}/></View>)}
|
||
{images.length<5&&<Pressable accessibilityRole="button" accessibilityLabel="添加反馈图片" disabled={busy||uncertain} onPress={()=>void choose()} style={{width:100,height:100,borderRadius:12,borderWidth:1,borderColor:t.line,borderStyle:'dashed',alignItems:'center',justifyContent:'center',gap:8}}><Icon name="add"/><Note>添加图片</Note></Pressable>}</View>
|
||
<Note>提交后会按后台通知配置交由负责人处理。你只能查看自己的反馈。</Note><Button title="提交反馈" busy={busy} disabled={uncertain||!title.trim()||!description.trim()} onPress={()=>Alert.alert('提交反馈','确认将以上描述和图片发送给系统负责人?',[{text:'再检查一下',style:'cancel'},{text:'确认提交',onPress:()=>void submit()}])}/>
|
||
<Button title="我的反馈" secondary disabled={busy} onPress={()=>open('feedback-list')}/>
|
||
</>):detail?<><Button title="返回反馈列表" secondary onPress={()=>setDetail(null)}/><Card><Note>{detail.type==='BUG'?'问题反馈':'功能建议'} · {feedbackStatuses[detail.status]||detail.status}</Note><Heading>{detail.title}</Heading><Note>{detail.createTime}</Note><Text selectable style={{color:t.text,lineHeight:24}}>{detail.description}</Text><View style={s.row}>{(detail.images||[]).map((image:Row)=><ProtectedImage key={image.id} api={api} path={`/system/feedback/images/${image.id}/content`} label={image.name}/>)}</View></Card><Card><Heading>处理进展</Heading><Note>{detail.reply||'负责人尚未回复,请稍后再来查看。'}</Note>{!!detail.handledTime&&<Note>更新于 {detail.handledTime}</Note>}</Card></>:<><View style={[s.row,{justifyContent:'space-between'}]}><Heading>我的反馈</Heading><Button title="刷新" secondary busy={busy} onPress={()=>void load(page)}/></View><Note>共 {total} 条 · 仅展示当前账号提交的反馈。</Note>
|
||
{!busy&&!rows.length&&<Card><Icon name="chatbubbles-outline" size={40}/><Heading>还没有反馈</Heading><Note>遇到问题时,欢迎随时告诉我们。</Note><Button title="提交反馈" onPress={()=>open('feedback')}/></Card>}
|
||
{rows.map(row=><Pressable accessibilityRole="button" accessibilityLabel={`查看反馈:${row.title}`} disabled={busy} key={row.id} onPress={()=>void inspect(row.id)}><Card><View style={[s.row,{justifyContent:'space-between'}]}><Text style={{color:t.accent,fontSize:12}}>{row.type==='BUG'?'问题反馈':'功能建议'}</Text><Note>{feedbackStatuses[row.status]||row.status}</Note></View><Text style={{color:t.text,fontSize:17,fontWeight:'600'}}>{row.title}</Text><Note>{row.createTime}</Note><Icon name="arrow-forward" size={18}/></Card></Pressable>)}
|
||
<View style={[s.row,{justifyContent:'space-between'}]}><Button title="上一页" secondary disabled={busy||page<=1} onPress={()=>void load(page-1)}/><Button title="下一页" secondary disabled={busy||page*10>=total} onPress={()=>void load(page+1)}/></View>
|
||
</>}{!!error&&<Note>{error}</Note>}
|
||
</ScrollView></KeyboardAvoidingView>;
|
||
}
|