feat(credit): 增强信用订单页面的文件上传和预览功能
- 添加 guessFileType 工具函数用于推断文件类型 - 扩展 addUploadedRecords 函数支持更多文件字段包括 downloadUrl 和 path - 优化文件上传逻辑支持从临时文件路径批量上传 - 集成文件类型检测功能到文档预览中提高兼容性 - 调整城市选择组件限制最多选择2个城市并优化显示逻辑
This commit is contained in:
@@ -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<any>(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)
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user