fix(common-tools): 关键词共享按后端真实返回接统计四段与日志字段
/stats-grouped 是四段各自 GROUP BY(对齐桌面版弹窗),前端原先按 「国家×一级类目」交叉表建模 ⇒ 弹窗恒为暂无数据;/logs 返回 userName/time 而表格读 operator/createdAt ⇒ 两列恒为占位符。改为 API 层映射 + 四段渲染, 日志去掉后端不产出的「说明」列。
This commit is contained in:
@@ -172,7 +172,7 @@ export function fetchKeywordShareFilters() {
|
||||
|
||||
export function fetchKeywordShareStatsGrouped() {
|
||||
return unwrapJavaResponse(
|
||||
get<JavaApiResponse<unknown>>(`${JAVA_API_PREFIX}/keyword-share/stats-grouped`),
|
||||
get<JavaApiResponse<KeywordShareStatsGroupedVo>>(`${JAVA_API_PREFIX}/keyword-share/stats-grouped`),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ export function unmarkKeywordShareUsage(items: KeywordShareUsageItem[]) {
|
||||
|
||||
export function fetchKeywordShareLogs(query: KeywordShareLogsQuery) {
|
||||
return unwrapJavaResponse(
|
||||
get<JavaApiResponse<KeywordShareLogsVo>>(`${JAVA_API_PREFIX}/keyword-share/logs`, {
|
||||
get<JavaApiResponse<KeywordShareLogRawVo>>(`${JAVA_API_PREFIX}/keyword-share/logs`, {
|
||||
params: {
|
||||
type: query.type,
|
||||
country: query.country || undefined,
|
||||
@@ -227,7 +227,7 @@ export function fetchKeywordShareLogs(query: KeywordShareLogsQuery) {
|
||||
size: query.size ?? 20,
|
||||
},
|
||||
}),
|
||||
)
|
||||
).then((raw) => mapKeywordShareLogs(raw, query.type))
|
||||
}
|
||||
|
||||
export function fetchKeywordShareRandom(count = 10) {
|
||||
@@ -238,53 +238,105 @@ export function fetchKeywordShareRandom(count = 10) {
|
||||
)
|
||||
}
|
||||
|
||||
export interface KeywordStatsGroupedRow {
|
||||
country: string
|
||||
category: string
|
||||
/** 后端 /stats-grouped 的四段分布(对齐桌面版词库统计弹窗:国家 / 一级 / 二级 / 三级) */
|
||||
export interface KeywordShareStatsGroupedVo {
|
||||
byCountry?: Array<{ name?: string | null; count?: number | string | null }> | null
|
||||
byCategory?: Array<{ name?: string | null; count?: number | string | null }> | null
|
||||
bySubCategory?: Array<{ category?: string | null; name?: string | null; count?: number | string | null }> | null
|
||||
byCategoryL3?: Array<{
|
||||
category?: string | null
|
||||
subCategory?: string | null
|
||||
name?: string | null
|
||||
count?: number | string | null
|
||||
}> | null
|
||||
}
|
||||
|
||||
/** 弹窗一行:group 是所属上级(二级/三级段用),label 是本段名称 */
|
||||
export interface KeywordStatsItem {
|
||||
group: string
|
||||
label: string
|
||||
count: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 词库统计弹窗的兼容映射:/stats-grouped 的返回结构按后端实现演进,
|
||||
* 这里把已知形状统一拍平成「国家 × 一级类目 → 计数」行:
|
||||
* ① { rows: [{country, category, count}] };
|
||||
* ② [{country, category, count}] 数组;
|
||||
* ③ { 国家: { 类目: 数值 } } 二层映射。
|
||||
* 其余形状返回空数组(弹窗显示「暂无数据」,不报错)。
|
||||
*/
|
||||
export function normalizeKeywordStatsGrouped(raw: unknown): KeywordStatsGroupedRow[] {
|
||||
const rows: KeywordStatsGroupedRow[] = []
|
||||
|
||||
const pushRow = (country: unknown, category: unknown, count: unknown) => {
|
||||
const c = String(country ?? '').trim()
|
||||
const cat = String(category ?? '').trim()
|
||||
const n = Number(count)
|
||||
if (!c || !cat || !Number.isFinite(n)) return
|
||||
rows.push({ country: c, category: cat, count: n })
|
||||
}
|
||||
|
||||
if (Array.isArray(raw)) {
|
||||
for (const item of raw) {
|
||||
if (item && typeof item === 'object') {
|
||||
const record = item as Record<string, unknown>
|
||||
pushRow(record.country, record.category, record.count)
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
if (raw && typeof raw === 'object') {
|
||||
const record = raw as Record<string, unknown>
|
||||
if (Array.isArray(record.rows)) {
|
||||
return normalizeKeywordStatsGrouped(record.rows)
|
||||
}
|
||||
for (const [country, value] of Object.entries(record)) {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) continue
|
||||
for (const [category, count] of Object.entries(value as Record<string, unknown>)) {
|
||||
pushRow(country, category, count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rows
|
||||
export interface KeywordStatsSections {
|
||||
byCountry: KeywordStatsItem[]
|
||||
byCategory: KeywordStatsItem[]
|
||||
bySubCategory: KeywordStatsItem[]
|
||||
byCategoryL3: KeywordStatsItem[]
|
||||
}
|
||||
|
||||
function statsCount(value: unknown): number | null {
|
||||
// Number(null) 与 Number('') 都是 0——缺计数的脏行必须判无效丢弃,不能冒充「0 个词」
|
||||
if (value === null || value === undefined || value === '') return null
|
||||
const n = Number(value)
|
||||
return Number.isFinite(n) ? n : null
|
||||
}
|
||||
|
||||
/**
|
||||
* /stats-grouped → 四段分布。后端返回的是四份各自 GROUP BY 的清单(不是国家×类目交叉表),
|
||||
* 缺段/脏行按空处理,弹窗对应段显示「暂无数据」而不是整窗报错。
|
||||
*/
|
||||
export function buildKeywordStatsSections(raw: unknown): KeywordStatsSections {
|
||||
const source = (raw && typeof raw === 'object' ? raw : {}) as KeywordShareStatsGroupedVo
|
||||
const flat = (
|
||||
rows: Array<{ name?: string | null; count?: number | string | null }> | null | undefined,
|
||||
groupOf: (row: { name?: string | null; count?: number | string | null }) => string,
|
||||
): KeywordStatsItem[] =>
|
||||
(Array.isArray(rows) ? rows : []).flatMap((row) => {
|
||||
const label = String(row?.name ?? '').trim()
|
||||
const count = statsCount(row?.count)
|
||||
if (!label || count === null) return []
|
||||
return [{ group: groupOf(row), label, count }]
|
||||
})
|
||||
|
||||
return {
|
||||
byCountry: flat(source.byCountry, () => ''),
|
||||
byCategory: flat(source.byCategory, () => ''),
|
||||
bySubCategory: flat(source.bySubCategory, (row) => String((row as { category?: string | null }).category ?? '').trim()),
|
||||
byCategoryL3: flat(source.byCategoryL3, (row) => {
|
||||
const item = row as { category?: string | null; subCategory?: string | null }
|
||||
const category = String(item.category ?? '').trim()
|
||||
const sub = String(item.subCategory ?? '').trim()
|
||||
return [category, sub].filter(Boolean).join(' / ')
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/** 后端日志行字段名与展示口径不同(userName/time),映射到表格用的 operator/createdAt */
|
||||
export interface KeywordShareLogRawRow {
|
||||
id?: number | string
|
||||
userName?: string | null
|
||||
country?: string | null
|
||||
keyword?: string | null
|
||||
/** Jackson 默认 ISO-8601(2026-10-23T21:12:34),也可能带毫秒 */
|
||||
time?: string | null
|
||||
}
|
||||
|
||||
export interface KeywordShareLogRawVo {
|
||||
total?: number | string
|
||||
rows?: KeywordShareLogRawRow[] | null
|
||||
}
|
||||
|
||||
/** ISO 时间 → 'YYYY-MM-DD HH:mm:ss';非 ISO 形态原样透出,空值给空串 */
|
||||
export function formatLogTime(raw?: string | null): string {
|
||||
const value = String(raw ?? '').trim()
|
||||
if (!value) return ''
|
||||
const matched = /^(\d{4}-\d{2}-\d{2})[T ](\d{2}:\d{2}:\d{2})/.exec(value)
|
||||
return matched ? `${matched[1]} ${matched[2]}` : value
|
||||
}
|
||||
|
||||
export function mapKeywordShareLogs(raw: unknown, type?: KeywordShareLogType): KeywordShareLogsVo {
|
||||
const source = (raw && typeof raw === 'object' ? raw : {}) as KeywordShareLogRawVo
|
||||
const rows = Array.isArray(source.rows) ? source.rows : []
|
||||
return {
|
||||
total: Number.isFinite(Number(source.total)) ? Number(source.total) : rows.length,
|
||||
rows: rows.map((row) => ({
|
||||
id: row?.id,
|
||||
type,
|
||||
country: row?.country ?? null,
|
||||
keyword: row?.keyword ?? null,
|
||||
operator: row?.userName ?? null,
|
||||
createdAt: formatLogTime(row?.time),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user