diff --git a/src/api/category.ts b/src/api/category.ts new file mode 100644 index 0000000..d4cf841 --- /dev/null +++ b/src/api/category.ts @@ -0,0 +1,110 @@ +import { get } from './request' + +/** 分类树节点(与后端返回结构一致) */ +export interface CategoryTreeNode { + id: string + label: string + slug: string + /** 第二层专用:MDI 图标名,如 mdi-view-grid-outline */ + icon?: string + /** 第三层展示时的区块标题,如「加密货币」 */ + sectionTitle?: string + forceShow?: boolean + forceHide?: boolean + publishedAt?: string + updatedBy?: number + createdAt?: string + updatedAt?: string + children?: CategoryTreeNode[] +} + +/** 模拟分类数据:一层(财经)、二层(体育/加密带图标)、三层(政治) */ +export const MOCK_CATEGORY_TREE: CategoryTreeNode[] = [ + { + id: '1', + label: '政治', + slug: 'politics', + children: [ + { + id: '11', + label: '政治1', + slug: 'politics1', + children: [ + { id: '111', label: '政治1-A', slug: 'politics1a', children: [] }, + { id: '112', label: '政治1-B', slug: 'politics1b', children: [] }, + ], + }, + { id: '12', label: '政治2', slug: 'politics2', children: [] }, + ], + }, + { + id: '2', + label: '体育', + slug: 'sports', + children: [ + { id: '21', label: '足球', slug: 'football', children: [] }, + { id: '22', label: '篮球', slug: 'basketball', children: [] }, + ], + }, + { + id: '3', + label: '加密', + slug: 'crypto', + sectionTitle: '加密货币', + children: [ + { + id: '31', + label: '全部', + slug: 'all', + icon: 'mdi-view-grid-outline', + children: [ + { id: '311', label: '全部', slug: 'all', children: [] }, + { id: '312', label: 'Above / Below', slug: 'above-below', children: [] }, + { id: '313', label: 'Up / Down', slug: 'up-down', children: [] }, + { id: '314', label: 'BTC', slug: 'btc', children: [] }, + { id: '315', label: 'ETH', slug: 'eth', children: [] }, + ], + }, + { id: '32', label: '5分钟', slug: '5m', icon: 'mdi-format-list-bulleted', children: [] }, + { id: '33', label: '15分钟', slug: '15m', icon: 'mdi-clock-outline', children: [] }, + { id: '34', label: '每小时', slug: '1h', icon: 'mdi-refresh', children: [] }, + { id: '35', label: '4小时', slug: '4h', icon: 'mdi-clock-outline', children: [] }, + { id: '36', label: '每天', slug: '1d', icon: 'mdi-calendar', children: [] }, + ], + }, + { id: '4', label: '财务', slug: 'finance', children: [] }, + { id: '5', label: '地缘政治', slug: 'geopolitics', children: [] }, + { id: '0', label: '最新', slug: 'latest', children: [] }, +] + +/** 分类树接口响应 */ +export interface CategoryTreeResponse { + code: number + data: CategoryTreeNode[] + msg: string +} + +/** + * 获取分类树(公开接口,不需要鉴权) + * GET /PmTag/getPmTagPublic + * + * 返回带 children 的树形结构,最多三层 + * data 可能为数组,或 { list: [] } 等格式,统一转为 CategoryTreeNode[] + */ +export async function getCategoryTree(): Promise { + const res = await get<{ code: number; data: CategoryTreeNode[] | { list?: CategoryTreeNode[] }; msg: string }>( + '/PmTag/getPmTagPublic' + ) + let data: CategoryTreeNode[] = [] + const raw = res.data + if (Array.isArray(raw)) { + data = raw + } else if (raw && typeof raw === 'object') { + if (Array.isArray((raw as { list?: CategoryTreeNode[] }).list)) { + data = (raw as { list: CategoryTreeNode[] }).list + } else if ((raw as CategoryTreeNode).id && (raw as CategoryTreeNode).label) { + data = [raw as CategoryTreeNode] + } + } + return { code: res.code, data, msg: res.msg } +} diff --git a/src/views/Home.vue b/src/views/Home.vue index 10cfe7b..d53e88b 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -1,13 +1,61 @@