Files
jyls_django/接口优化说明-user-register-detail.md
2026-01-13 15:25:37 +08:00

3.5 KiB
Raw Blame History

接口优化说明:/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 审核人列表,每个审核人包含 idusername

特殊情况处理

1. 团队不存在

如果用户的团队名称在数据库中不存在,返回:

{
  "team": {
    "name": "团队A",  // 保留原始团队名称
    "team_type": null,
    "team_type_display": null,
    "description": null,
    "approvers": []
  }
}

2. 用户没有团队

如果用户没有分配团队(user.team 为空),返回:

{
  "team": null
}

性能优化

为了优化查询性能,使用了批量查询:

  1. 批量获取团队信息:一次性查询所有用户的团队信息,避免 N+1 查询问题
  2. 批量获取待审核记录:一次性查询所有用户的待审核入职登记

代码位置

  • 文件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年