From 093bbff33e3f1d39b5225201bfdeb7db4f3c44c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Mon, 16 Mar 2026 23:15:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(credit):=20=E5=A2=9E=E5=BC=BA=E4=BF=A1?= =?UTF-8?q?=E7=94=A8=E8=AE=A2=E5=8D=95=E9=A1=B5=E9=9D=A2=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=92=8C=E9=A2=84=E8=A7=88=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 guessFileType 工具函数用于推断文件类型 - 扩展 addUploadedRecords 函数支持更多文件字段包括 downloadUrl 和 path - 优化文件上传逻辑支持从临时文件路径批量上传 - 集成文件类型检测功能到文档预览中提高兼容性 - 调整城市选择组件限制最多选择2个城市并优化显示逻辑 --- src/credit/order/add.tsx | 45 ++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/credit/order/add.tsx b/src/credit/order/add.tsx index daf35a8..1c28cd8 100644 --- a/src/credit/order/add.tsx +++ b/src/credit/order/add.tsx @@ -24,6 +24,17 @@ const isHttpUrl = (url?: string) => { return /^https?:\/\//i.test(url) } +const guessFileType = (nameOrUrl?: string) => { + const s = String(nameOrUrl || '').trim() + if (!s) return undefined + const clean = s.split('?')[0].split('#')[0] + const dot = clean.lastIndexOf('.') + if (dot < 0) return undefined + const ext = clean.slice(dot + 1).toLowerCase() + if (!/^[a-z0-9]+$/.test(ext)) return undefined + return ext +} + export default function CreditOrderAddPage() { const formRef = useRef(null) @@ -44,19 +55,18 @@ export default function CreditOrderAddPage() { text: a.label, children: (a.children || []).map(b => ({ value: b.label, - text: b.label, - children: (b.children || []).map(c => ({ - value: c.label, - text: c.label - })) + text: b.label })) })) }, []) - const addUploadedRecords = (incoming: Array<{ url?: string; thumbnail?: string; name?: string }>, opts: { isImage: boolean }) => { + const addUploadedRecords = ( + incoming: Array<{ url?: string; thumbnail?: string; downloadUrl?: string; path?: string; name?: string }>, + opts: { isImage: boolean } + ) => { const next = incoming .map((r, idx) => { - const url = String(r.url || r.thumbnail || '').trim() + const url = String(r.url || r.downloadUrl || r.thumbnail || r.path || '').trim() if (!url) return null const name = String(r.name || (opts.isImage ? `图片${idx + 1}` : `文件${idx + 1}`)).trim() const id = `${Date.now()}-${Math.random().toString(16).slice(2)}` @@ -124,15 +134,17 @@ export default function CreditOrderAddPage() { // @ts-ignore const tempFiles = (res?.tempFiles || []) as Array<{ path?: string; name?: string }> - const paths = tempFiles.map(f => f?.path).filter(Boolean) as string[] - if (!paths.length) return + const picked = tempFiles + .map(f => ({ path: f?.path, name: f?.name })) + .filter(f => Boolean(f.path)) as Array<{ path: string; name?: string }> + if (!picked.length) return setUploading(true) try { const uploaded = [] - for (let i = 0; i < paths.length; i++) { - const record = await uploadFileByPath(paths[i]) - const name = tempFiles[i]?.name + for (const f of picked) { + const record = await uploadFileByPath(f.path) + const name = f?.name uploaded.push({ ...(record as any), name: name || (record as any)?.name }) } addUploadedRecords(uploaded, { isImage: false }) @@ -179,7 +191,10 @@ export default function CreditOrderAddPage() { filePath = dl?.tempFilePath } - await Taro.openDocument({ filePath, showMenu: true }) + const fileType = guessFileType(a.name || a.url) + const openOpts: any = { filePath, showMenu: true } + if (fileType) openOpts.fileType = fileType + await Taro.openDocument(openOpts) } catch (e) { console.error('预览文件失败:', e) Taro.showToast({ title: '无法打开该文件', icon: 'none' }) @@ -363,9 +378,9 @@ export default function CreditOrderAddPage() { options={cityOptions as any} title="选择城市" onChange={(value: any[]) => { - const list = (value || []).filter(Boolean).slice(0, 3).map(v => String(v)) + const list = (value || []).filter(Boolean).slice(0, 2).map(v => String(v)) setCityValue(list) - const txt = list.slice(0, 2).join(' ') + const txt = list.join(' ') setCityText(txt || '') setCityVisible(false) }}