2026-03-20 17:05:49 +08:00

49 lines
1.5 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.

# user.ts
**路径**`src/api/user.ts`
## 功能用途
用户相关接口获取用户信息、USDC 余额、修改自身用户名。对接 `/user/getUserInfo``/user/getUsdcBalance``/user/setSelfUsername`,均需鉴权。
## 核心能力
- `getUserInfo`:获取当前用户信息(头像、昵称等)
- `getUsdcBalance`:查询 USDC 余额amount、available、locked 需除以 1_000_000
- `setSelfUsername`修改自身用户名PUT `/user/setSelfUsername`
- `formatUsdcBalance`:将原始数值转为显示用字符串(如 "0.00"
## 类型说明
| 类型 | 说明 |
|------|------|
| `UserInfoData` | 用户信息结构 |
| `UsdcBalanceData` | 余额结构amount/available/locked 为原始值 |
## 使用方式
```typescript
import { getUserInfo, getUsdcBalance, setSelfUsername, formatUsdcBalance } from '@/api/user'
const headers = { 'x-token': token, 'x-user-id': userId }
const userRes = await getUserInfo(headers)
const balanceRes = await getUsdcBalance(headers)
if (balanceRes.data) {
const display = formatUsdcBalance(balanceRes.data.available)
}
// 修改自身用户名
const setRes = await setSelfUsername(headers, { username: 'new_username' })
if (setRes.code === 0 || setRes.code === 200) {
// 建议:成功后调用 fetchUserInfo() 刷新页面展示
}
```
## 扩展方式
1. **更新个人信息**:新增 `updateUserInfo` 接口
2. **交易记录**:新增 `getUserTrades` 等接口
3. **多币种**:扩展 `UsdcBalanceData` 支持其他资产