import { API_DEMO_ENABLED, API_BASE_URL, requestJson } from '@/services/api.uts' import { newsArticles, financeLessons, ContentSection } from './content.uts' export type ContentSearchResult = { id: string, type: string, title: string, summary: string, category: string, meta: string, image: string } const sectionText = (sections: ContentSection[]): string => sections.map((item: ContentSection): string => item.title + ' ' + item.body).join(' ') const matches = (text: string, words: string[]): boolean => words.every((word: string): boolean => text.toLowerCase().includes(word)) const readText = (item: UTSJSONObject, key: string): string => typeof item[key] == 'string' ? item[key] as string : '' // type: all / news / lesson。接入后端时返回相同类型的 items,详情沿用内容 ID。 export async function searchContent(keyword: string, type: string): Promise { const query = keyword.trim().replace(/\s+/g, ' ') const results: ContentSearchResult[] = [] if (query.length == 0) return results if (!API_DEMO_ENABLED) { const response = await requestJson('/content/search', 'GET', { keyword: query, type }) const items = response['items'] as UTSJSONObject[] | null if (items == null) throw new Error('搜索结果加载失败,请重试') items.forEach((item: UTSJSONObject) => { const kind = readText(item, 'type'); const id = readText(item, 'id'); const title = readText(item, 'title') if ((kind == 'news' || kind == 'lesson') && (type == 'all' || type == kind) && id.length > 0 && title.length > 0) { const result: ContentSearchResult = { id, type: kind, title, summary: readText(item, 'summary'), category: readText(item, 'category'), meta: readText(item, 'meta'), image: readText(item, 'image') } results.push(result) } }) return results } const words = query.toLowerCase().split(' ') if (type == 'all' || type == 'news') { newsArticles.forEach((item) => { if (matches(item.title + ' ' + item.summary + ' ' + item.category + ' ' + sectionText(item.sections), words)) { const result: ContentSearchResult = { id: item.id, type: 'news', title: item.title, summary: item.summary, category: item.category, meta: item.date, image: item.image } results.push(result) } }) } if (type == 'all' || type == 'lesson') { financeLessons.forEach((item) => { if (matches(item.title + ' ' + item.desc + ' ' + item.tag + ' ' + item.intro + ' ' + sectionText(item.chapters), words)) { const result: ContentSearchResult = { id: item.id, type: 'lesson', title: item.title, summary: item.desc, category: item.tag, meta: item.duration + ' · ' + item.chapters.length + ' 节', image: item.image } results.push(result) } }) } return results }