2026-02-28 00:03:37 +08:00

67 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# event.ts
**路径**`src/api/event.ts`
## 功能用途
Event预测市场事件相关接口与类型定义对接 XTrader API 的 `/PmEvent/*` 接口。提供事件列表、详情查询,以及将接口数据映射为首页卡片所需结构的工具函数。
## 核心能力
- `getPmEventPublic`:分页获取公开事件列表(无需鉴权)
- `findPmEvent`:按 id/slug 查询事件详情(需鉴权)
- `mapEventItemToCard`:将 `PmEventListItem` 转为 `EventCardItem`(供 MarketCard 使用)
- 内存缓存:`getEventListCache``setEventListCache``clearEventListCache`,用于列表切换页面时复用
## 类型说明
| 类型 | 说明 |
|------|------|
| `PmEventListItem` | 接口返回的事件项 |
| `PmEventMarketItem` | 市场项,含 outcomes、outcomePrices、clobTokenIds |
| `EventCardItem` | 首页卡片所需结构,含 displayTypesingle/multi |
| `EventCardOutcome` | 多选项卡片中单个选项 |
| `PageResult<T>` | 分页结果 |
## 使用方式
```typescript
import {
getPmEventPublic,
findPmEvent,
mapEventItemToCard,
clearEventListCache,
} from '@/api/event'
// 获取列表
const res = await getPmEventPublic({ page: 1, pageSize: 10, tagIds: [1, 2] })
const cards = res.data.list.map(mapEventItemToCard)
// 获取详情(需鉴权)
const detail = await findPmEvent({ id: 123 }, { headers: authHeaders })
// 下拉刷新时清空缓存
clearEventListCache()
```
## 扩展方式
1. **新增筛选参数**:在 `GetPmEventListParams` 中增加字段,并在 `getPmEventPublic` 的 query 中传入
2. **缓存策略**:可改为 sessionStorage 或带 TTL 的缓存
3. **多选项展示**`mapEventItemToCard` 已支持 multi 类型,可扩展 `EventCardOutcome` 字段
## 参数传递方式
### tagIds 参数(数组)
`tagIds` 使用传统数组方式传递,不再是逗号分隔的字符串:
```typescript
// 正确方式 - 直接传递数组
const res = await getPmEventPublic({
page: 1,
pageSize: 10,
tagIds: [1, 2, 3] // 会作为多个同名参数传递:?tagIds=1&tagIds=2&tagIds=3
})
```