Files
airz-uniapp/packages/content/services/search.uts
T
hujun dcf12a4471 refactor: 重构存储与接口适配,统一金额单位与会话管理
- 新增配置、存储、金额转换工具服务
- 按userId隔离本地缓存,替换旧设备级存储
- 统一移动端金额单位为万元,新增转换适配逻辑
- 重构认证与请求逻辑,支持真实后端会话
- 调整搜索、帮助、融资模块的模拟开关与数据适配
- 更新文档说明开发状态与配置规则
2026-09-22 17:25:52 +08:00

49 lines
2.7 KiB
Plaintext

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<ContentSearchResult[]> {
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
}