3.5 KiB
3.5 KiB
接口优化说明:/finance/user-register-detail
优化内容
优化了 /finance/user-register-detail 接口,将 team 字段从简单的字符串(团队名称)改为返回完整的团队信息对象。
返回数据结构
优化前
{
"code": 0,
"message": "查询成功",
"total": 10,
"data": [
{
"id": 1,
"username": "张三",
"account": "zhangsan",
"team": "团队A", // 只返回团队名称(字符串)
...
}
]
}
优化后
{
"code": 0,
"message": "查询成功",
"total": 10,
"data": [
{
"id": 1,
"username": "张三",
"account": "zhangsan",
"team": { // 返回完整的团队信息对象
"id": 1,
"name": "团队A",
"team_type": "team", // "personal" 或 "team"
"team_type_display": "团队", // "个人团队" 或 "团队"
"description": "团队描述信息",
"approvers": [ // 审核人列表
{
"id": 2,
"username": "李四"
},
{
"id": 3,
"username": "王五"
}
]
},
...
}
]
}
团队信息字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
id |
Integer | 团队ID |
name |
String | 团队名称 |
team_type |
String | 团队类型:"personal"(个人团队)或 "team"(团队) |
team_type_display |
String | 团队类型显示名称:"个人团队" 或 "团队" |
description |
String | 团队描述(可为空) |
approvers |
Array | 审核人列表,每个审核人包含 id 和 username |
特殊情况处理
1. 团队不存在
如果用户的团队名称在数据库中不存在,返回:
{
"team": {
"name": "团队A", // 保留原始团队名称
"team_type": null,
"team_type_display": null,
"description": null,
"approvers": []
}
}
2. 用户没有团队
如果用户没有分配团队(user.team 为空),返回:
{
"team": null
}
性能优化
为了优化查询性能,使用了批量查询:
- 批量获取团队信息:一次性查询所有用户的团队信息,避免 N+1 查询问题
- 批量获取待审核记录:一次性查询所有用户的待审核入职登记
代码位置
- 文件:
jyls_django/finance/views.py - 类:
UnregisteredUserList - 方法:
post - 行数:391-462
使用示例
前端调用示例
// 调用接口
const response = await fetch('/api2/finance/user-register-detail', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
page: 1,
per_page: 10,
username: '', // 可选
department: '' // 可选
})
});
const data = await response.json();
// 使用团队信息
data.data.forEach(user => {
if (user.team) {
console.log('团队名称:', user.team.name);
console.log('团队类型:', user.team.team_type_display);
console.log('审核人数量:', user.team.approvers.length);
// 判断是否需要审核人
if (user.team.team_type === 'team') {
console.log('需要审核人:', user.team.approvers);
}
}
});
兼容性说明
- ✅ 向后兼容:如果前端代码期望
team是字符串,可以通过user.team.name获取团队名称 - ✅ 新功能:前端可以直接使用完整的团队信息,无需额外查询
优化时间:2024年 最后更新:2024年