优化pagetype列表页Link字段title_field为批量获取
This commit is contained in:
parent
c9c2d6ec62
commit
69f1c36bfd
@ -330,83 +330,97 @@ async function getPageTypeConfig(pagetype: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取记录的标题字段值
|
// 预加载Link字段的title_field值(优化:按pagetype分组批量请求)
|
||||||
async function getRecordTitle(pagetype: string, recordName: string): Promise<string> {
|
|
||||||
if (!pagetype || !recordName) return recordName || ''
|
|
||||||
|
|
||||||
// 检查缓存
|
|
||||||
const cacheKey = `${pagetype}_${recordName}`
|
|
||||||
if (linkTitleCache.value[cacheKey]) {
|
|
||||||
return linkTitleCache.value[cacheKey]
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 获取页面类型配置
|
|
||||||
const config = await getPageTypeConfig(pagetype)
|
|
||||||
const titleField = config.title_field || 'name'
|
|
||||||
const showTitleFieldInLink = config.show_title_field_in_link || false
|
|
||||||
|
|
||||||
// 如果标题字段就是name,直接返回
|
|
||||||
if (titleField === 'name') {
|
|
||||||
linkTitleCache.value[cacheKey] = recordName
|
|
||||||
return recordName
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果未启用show_title_field_in_link,直接返回原值
|
|
||||||
if (!showTitleFieldInLink) {
|
|
||||||
linkTitleCache.value[cacheKey] = recordName
|
|
||||||
return recordName
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取记录数据
|
|
||||||
const response = await axios.get(`/api/data/${encodeURIComponent(pagetype)}/${encodeURIComponent(recordName)}`, {
|
|
||||||
headers: get_session_api_headers(),
|
|
||||||
withCredentials: true
|
|
||||||
})
|
|
||||||
|
|
||||||
const recordData = response.data?.data || {}
|
|
||||||
const titleValue = recordData[titleField] || recordName
|
|
||||||
|
|
||||||
// 缓存结果
|
|
||||||
linkTitleCache.value[cacheKey] = titleValue
|
|
||||||
return titleValue
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('获取记录标题失败:', error)
|
|
||||||
return recordName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 预加载Link字段的title_field值
|
|
||||||
async function preloadLinkTitles() {
|
async function preloadLinkTitles() {
|
||||||
// 找到所有Link类型的字段
|
// 找到所有Link类型的字段
|
||||||
const linkFields = metaFields.value.filter(f => f.fieldtype === 'Link' && f.options)
|
const linkFields = metaFields.value.filter(f => f.fieldtype === 'Link' && f.options)
|
||||||
|
|
||||||
if (linkFields.length === 0) return
|
if (linkFields.length === 0) return
|
||||||
|
|
||||||
// 收集所有需要加载的Link字段值,使用Map去重
|
// 按pagetype分组收集需要加载的记录名,使用Map去重
|
||||||
const linkMap = new Map<string, {pagetype: string, recordName: string}>()
|
const pagetypeGroups = new Map<string, Set<string>>()
|
||||||
|
|
||||||
rows.value.forEach(row => {
|
rows.value.forEach(row => {
|
||||||
linkFields.forEach(field => {
|
linkFields.forEach(field => {
|
||||||
const value = row[field.fieldname]
|
const value = row[field.fieldname]
|
||||||
if (value) {
|
if (value) {
|
||||||
const key = `${field.options}::${value}`
|
const pagetype = field.options
|
||||||
if (!linkMap.has(key)) {
|
if (!pagetypeGroups.has(pagetype)) {
|
||||||
linkMap.set(key, {
|
pagetypeGroups.set(pagetype, new Set())
|
||||||
pagetype: field.options,
|
|
||||||
recordName: value
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
pagetypeGroups.get(pagetype)!.add(value)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// 批量加载title_field值
|
// 按pagetype批量加载(优化:每组使用一次API调用)
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Array.from(linkMap.values()).map(({ pagetype, recordName }) =>
|
Array.from(pagetypeGroups.entries()).map(async ([pagetype, recordNames]) => {
|
||||||
getRecordTitle(pagetype, recordName).catch(() => recordName)
|
// 先获取配置,检查是否需要加载title_field
|
||||||
)
|
const config = await getPageTypeConfig(pagetype)
|
||||||
|
const titleField = config.title_field || 'name'
|
||||||
|
const showTitleFieldInLink = config.show_title_field_in_link || false
|
||||||
|
|
||||||
|
// 如果标题字段就是name或未启用show_title_field_in_link,直接缓存
|
||||||
|
if (titleField === 'name' || !showTitleFieldInLink) {
|
||||||
|
recordNames.forEach(name => {
|
||||||
|
const cacheKey = `${pagetype}_${name}`
|
||||||
|
if (!linkTitleCache.value[cacheKey]) {
|
||||||
|
linkTitleCache.value[cacheKey] = name
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量获取记录(使用列表API,通过filters传入多个name)
|
||||||
|
const namesToLoad = Array.from(recordNames).filter(name => {
|
||||||
|
const cacheKey = `${pagetype}_${name}`
|
||||||
|
return !linkTitleCache.value[cacheKey]
|
||||||
|
})
|
||||||
|
|
||||||
|
if (namesToLoad.length === 0) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 使用列表API批量获取(name in [...])
|
||||||
|
const filters = [['name', 'in', namesToLoad]]
|
||||||
|
const fields = ['name', titleField]
|
||||||
|
const result = await axios.get(`/api/data/${encodeURIComponent(pagetype)}`, {
|
||||||
|
params: {
|
||||||
|
fields: JSON.stringify(fields),
|
||||||
|
filters: JSON.stringify(filters),
|
||||||
|
limit_page_length: namesToLoad.length
|
||||||
|
},
|
||||||
|
headers: get_session_api_headers(),
|
||||||
|
withCredentials: true
|
||||||
|
})
|
||||||
|
|
||||||
|
// 缓存结果
|
||||||
|
const records = result.data?.data || []
|
||||||
|
records.forEach((record: any) => {
|
||||||
|
const name = record.name
|
||||||
|
const titleValue = record[titleField] || name
|
||||||
|
const cacheKey = `${pagetype}_${name}`
|
||||||
|
linkTitleCache.value[cacheKey] = titleValue
|
||||||
|
})
|
||||||
|
|
||||||
|
// 对于API未返回的记录,缓存原值(可能是已删除的记录)
|
||||||
|
namesToLoad.forEach(name => {
|
||||||
|
const cacheKey = `${pagetype}_${name}`
|
||||||
|
if (!linkTitleCache.value[cacheKey]) {
|
||||||
|
linkTitleCache.value[cacheKey] = name
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`批量加载${pagetype}的标题失败:`, error)
|
||||||
|
// 失败时缓存原值
|
||||||
|
namesToLoad.forEach(name => {
|
||||||
|
const cacheKey = `${pagetype}_${name}`
|
||||||
|
if (!linkTitleCache.value[cacheKey]) {
|
||||||
|
linkTitleCache.value[cacheKey] = name
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user