feat(credit): 增强信用订单页面的文件上传和预览功能

- 添加 guessFileType 工具函数用于推断文件类型
- 扩展 addUploadedRecords 函数支持更多文件字段包括 downloadUrl 和 path
- 优化文件上传逻辑支持从临时文件路径批量上传
- 集成文件类型检测功能到文档预览中提高兼容性
- 调整城市选择组件限制最多选择2个城市并优化显示逻辑
This commit is contained in:
2026-03-16 23:15:33 +08:00
parent 138f28793f
commit 093bbff33e

View File

@@ -24,6 +24,17 @@ const isHttpUrl = (url?: string) => {
return /^https?:\/\//i.test(url) 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() { export default function CreditOrderAddPage() {
const formRef = useRef<any>(null) const formRef = useRef<any>(null)
@@ -44,19 +55,18 @@ export default function CreditOrderAddPage() {
text: a.label, text: a.label,
children: (a.children || []).map(b => ({ children: (a.children || []).map(b => ({
value: b.label, value: b.label,
text: b.label, text: b.label
children: (b.children || []).map(c => ({
value: c.label,
text: c.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 const next = incoming
.map((r, idx) => { .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 if (!url) return null
const name = String(r.name || (opts.isImage ? `图片${idx + 1}` : `文件${idx + 1}`)).trim() const name = String(r.name || (opts.isImage ? `图片${idx + 1}` : `文件${idx + 1}`)).trim()
const id = `${Date.now()}-${Math.random().toString(16).slice(2)}` const id = `${Date.now()}-${Math.random().toString(16).slice(2)}`
@@ -124,15 +134,17 @@ export default function CreditOrderAddPage() {
// @ts-ignore // @ts-ignore
const tempFiles = (res?.tempFiles || []) as Array<{ path?: string; name?: string }> const tempFiles = (res?.tempFiles || []) as Array<{ path?: string; name?: string }>
const paths = tempFiles.map(f => f?.path).filter(Boolean) as string[] const picked = tempFiles
if (!paths.length) return .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) setUploading(true)
try { try {
const uploaded = [] const uploaded = []
for (let i = 0; i < paths.length; i++) { for (const f of picked) {
const record = await uploadFileByPath(paths[i]) const record = await uploadFileByPath(f.path)
const name = tempFiles[i]?.name const name = f?.name
uploaded.push({ ...(record as any), name: name || (record as any)?.name }) uploaded.push({ ...(record as any), name: name || (record as any)?.name })
} }
addUploadedRecords(uploaded, { isImage: false }) addUploadedRecords(uploaded, { isImage: false })
@@ -179,7 +191,10 @@ export default function CreditOrderAddPage() {
filePath = dl?.tempFilePath 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) { } catch (e) {
console.error('预览文件失败:', e) console.error('预览文件失败:', e)
Taro.showToast({ title: '无法打开该文件', icon: 'none' }) Taro.showToast({ title: '无法打开该文件', icon: 'none' })
@@ -363,9 +378,9 @@ export default function CreditOrderAddPage() {
options={cityOptions as any} options={cityOptions as any}
title="选择城市" title="选择城市"
onChange={(value: any[]) => { 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) setCityValue(list)
const txt = list.slice(0, 2).join(' ') const txt = list.join(' ')
setCityText(txt || '') setCityText(txt || '')
setCityVisible(false) setCityVisible(false)
}} }}