/** * 体系模块API * 统一管理体系相关的所有API调用 */ import { API_CONFIG } from '../config/api.config.js' import { get } from '../utils/request.js' /** * 获取知识体系分类树 * @returns {Promise} 体系分类数据 */ export const getSystemTree = () => { return get(API_CONFIG.TREE.SYSTEM_TREE) } /** * 获取体系分类下的文章列表 * @param {number} cid 分类ID * @param {number} page 页码,从0开始 * @returns {Promise} 文章列表数据 */ export const getSystemArticles = (cid, page = 0) => { const url = API_CONFIG.TREE.SYSTEM_ARTICLES.replace('{page}', page) return get(url, { cid }) } /** * 获取体系模块完整数据(分类树 + 默认分类的文章列表) * @param {number} defaultCid 默认分类ID(可选) * @returns {Promise} 体系模块完整数据 */ export const getTreeModuleData = async (defaultCid = null) => { try { // 获取体系分类树 const treeData = await getSystemTree() const categories = treeData?.data || [] // 处理分类数据,提取一级分类和二级分类 const processedCategories = categories.map(category => ({ id: category.id, name: category.name, order: category.order, children: category.children || [], articleCount: 0, // 可以后续统计文章数量 expanded: false // 默认折叠状态 })) // 如果有默认分类,获取该分类的文章列表 let articleList = [] let selectedCategory = null if (defaultCid && processedCategories.length > 0) { // 查找默认分类 for (const category of processedCategories) { if (category.id === defaultCid) { selectedCategory = category break } // 在子分类中查找 const childCategory = category.children.find(child => child.id === defaultCid) if (childCategory) { selectedCategory = childCategory break } } if (selectedCategory) { const articleData = await getSystemArticles(selectedCategory.id, 0) articleList = articleData?.datas || [] } } return { categories: processedCategories, articleList, selectedCategory, total: articleList.length, currentPage: 0 } } catch (error) { console.error('获取体系模块数据失败:', error) throw error } } /** * 搜索体系分类 * @param {string} keyword 搜索关键词 * @returns {Promise} 搜索结果 */ export const searchSystemCategories = async (keyword) => { try { const treeData = await getSystemTree() const allCategories = treeData?.data || [] // 扁平化所有分类(包括一级和二级) const flatCategories = [] allCategories.forEach(category => { // 添加一级分类 flatCategories.push({ ...category, level: 1, parentName: '' }) // 添加二级分类 if (category.children && category.children.length > 0) { category.children.forEach(child => { flatCategories.push({ ...child, level: 2, parentName: category.name }) }) } }) // 搜索过滤 const filtered = flatCategories.filter(category => { const nameMatch = category.name.toLowerCase().includes(keyword.toLowerCase()) const parentMatch = category.parentName.toLowerCase().includes(keyword.toLowerCase()) return nameMatch || parentMatch }) return filtered } catch (error) { console.error('搜索体系分类失败:', error) throw error } } /** * 获取分类详情 * @param {number} cid 分类ID * @returns {Promise} 分类详情 */ export const getCategoryDetail = async (cid) => { try { const treeData = await getSystemTree() const allCategories = treeData?.data || [] // 查找分类 for (const category of allCategories) { if (category.id === cid) { return { ...category, level: 1 } } // 在子分类中查找 if (category.children && category.children.length > 0) { const childCategory = category.children.find(child => child.id === cid) if (childCategory) { return { ...childCategory, level: 2, parentCategory: { id: category.id, name: category.name } } } } } return null } catch (error) { console.error('获取分类详情失败:', error) throw error } } export default { getSystemTree, getSystemArticles, getTreeModuleData, searchSystemCategories, getCategoryDetail }