219 lines
7.8 KiB
TypeScript
219 lines
7.8 KiB
TypeScript
import { get, post, put, del } from '@/utils/request'
|
||
import { unwrap } from '@/api/training'
|
||
import type { WecomSyncSummary } from '@/api/training'
|
||
|
||
// 组织中心(租户级共享组织架构)API 封装,对应后端 /api/v1/org/*(routes_org.go)。
|
||
// 部门/员工档案归租户所有;业务模块(企业培训/图纸标注/物料检核...)通过
|
||
// module_key 在 org_module_members 上绑定各自角色,组织数据只维护一份。
|
||
// 响应解包沿用 api/training 导出的 unwrap(调用方 import { unwrap } from '@/api/training')。
|
||
|
||
// 注意必须带 /api/v1 前缀(与其他 api 模块一致);axios baseURL 为 '/',
|
||
// 缺前缀时请求会落入前端 SPA 回退路由,返回 index.html 而非接口数据。
|
||
const base = '/api/v1/org'
|
||
|
||
export type OrgModuleKey = string
|
||
|
||
/** 企业培训模块的 module_key(组织中心首个接入模块)。 */
|
||
export const ORG_MODULE_TRAINING: OrgModuleKey = 'training'
|
||
|
||
export interface OrgDept {
|
||
id: string
|
||
tenant_id: number
|
||
parent_id: string
|
||
name: string
|
||
code: string
|
||
sort: number
|
||
}
|
||
|
||
/** 模块绑定行(org_module_members):某模块在某员工上的角色/扩展属性。 */
|
||
export interface OrgModuleMember {
|
||
id: string
|
||
tenant_id: number
|
||
module_key: OrgModuleKey
|
||
employee_id: string
|
||
roles: string[]
|
||
extra: Record<string, any>
|
||
}
|
||
|
||
export interface OrgEmployee {
|
||
id: string
|
||
tenant_id: number
|
||
user_id: string
|
||
employee_no: string
|
||
dept_id: string
|
||
post: string
|
||
lecturer_rate: number
|
||
status: string
|
||
/** 企微账号(OA 同步/绑定维护;列表 Select org_employees.* 随行返回)。 */
|
||
wecom_userid?: string
|
||
/** 联查展示字段:用户姓名/部门名称/指定 module_key 的绑定角色 */
|
||
user_name?: string
|
||
dept_name?: string
|
||
roles?: string[]
|
||
}
|
||
|
||
export interface OrgListQuery {
|
||
module_key?: string
|
||
dept_id?: string
|
||
keyword?: string
|
||
page?: number
|
||
page_size?: number
|
||
}
|
||
|
||
export interface OrgPagedData<T> {
|
||
items: T[]
|
||
total: number
|
||
}
|
||
|
||
// ---- 部门 ----
|
||
|
||
export const listDepts = () => get<{ success: boolean; data: OrgDept[] }>(`${base}/depts`)
|
||
|
||
/** 新增/修改部门:id 为空走新增,否则更新。 */
|
||
export const saveDept = (payload: Partial<OrgDept>) =>
|
||
post<{ success: boolean; data: OrgDept }>(`${base}/depts`, payload)
|
||
|
||
export const deleteDept = (id: string) => del<{ success: boolean }>(`${base}/depts/${enc(id)}`)
|
||
|
||
// ---- 员工档案 ----
|
||
|
||
export const listEmployees = (query?: OrgListQuery) =>
|
||
get<{ success: boolean; data: OrgPagedData<OrgEmployee> }>(`${base}/employees`, { params: query })
|
||
|
||
// listAllOrgEmployees 拉取全量员工档案(分页循环,直到取满 total 或空页)。
|
||
// [2026-11-30] 档案超过 500 条后,单页 page_size=500 会把最早建档的员工截断
|
||
// 在第一页之外,组织架构员工档案分页器误显「共500条」—— 与 listAllTrainingMembers
|
||
// 同根因(4e071f9 修了培训侧 7 处,组织中心此处漏网),选人/列表场景一律用本
|
||
// helper 拉全量,名册继续增长也不再复发。
|
||
export async function listAllOrgEmployees(
|
||
query?: Omit<OrgListQuery, 'page' | 'page_size'>,
|
||
pageSize = 500,
|
||
): Promise<OrgEmployee[]> {
|
||
const out: OrgEmployee[] = []
|
||
for (let page = 1; page <= 100; page++) {
|
||
const res = await listEmployees({ ...query, page, page_size: pageSize })
|
||
const pageData = unwrap<OrgPagedData<OrgEmployee>>(res)
|
||
const items = pageData?.items || []
|
||
out.push(...items)
|
||
const total = Number(pageData?.total || 0)
|
||
if (!items.length || (total > 0 && out.length >= total)) break
|
||
}
|
||
return out
|
||
}
|
||
|
||
export interface OrgPlatformUser {
|
||
id: string
|
||
username: string
|
||
email: string
|
||
/** 姓名(OIDC displayName claim),可能为空 */
|
||
display_name?: string
|
||
/** Casdoor 同步的部门编号/名称(users.dept_code/dept_name,000156 同步态字段)。
|
||
* 从平台用户添加时按 dept_code 自动预选组织架构部门;两者可为空。 */
|
||
dept_code?: string
|
||
dept_name?: string
|
||
in_roster: boolean
|
||
}
|
||
|
||
export const listPlatformUsers = () =>
|
||
get<{ success: boolean; data: OrgPlatformUser[] }>(`${base}/platform-users`)
|
||
|
||
export interface OrgImportEmployee {
|
||
user_id: string
|
||
employee_no?: string
|
||
dept_id: string
|
||
post?: string
|
||
status?: string
|
||
}
|
||
|
||
/** 批量导入员工档案(部门必填,须为已维护部门)。 */
|
||
export const importEmployees = (employees: OrgImportEmployee[]) =>
|
||
post<{ success: boolean; data: { imported: number } }>(`${base}/employees/import`, { employees })
|
||
|
||
export interface OrgUpdateEmployee {
|
||
dept_id: string
|
||
post?: string
|
||
/** 留空表示不变更 */
|
||
status?: string
|
||
}
|
||
|
||
/** 修改员工档案(部门必填;姓名/工号随平台账号,不在可编辑范围)。 */
|
||
export const updateEmployee = (id: string, payload: OrgUpdateEmployee) =>
|
||
put<{ success: boolean; data: OrgEmployee }>(`${base}/employees/${enc(id)}`, payload)
|
||
|
||
// ---- 职称角色 / 企微账号(自培训域迁入组织架构页;/org 域别名路由复用培训
|
||
// handler,角色绑定仍写 module_key='training';权限细控 HR/经理在 handler 内) ----
|
||
|
||
/** 设置员工职称角色(学员为全员隐式角色,由后端保留,前端保存时一并带上)。 */
|
||
export const updateOrgEmployeeRoles = (id: string, roles: string[]) =>
|
||
put<{ success: boolean; data: OrgEmployee }>(`${base}/employees/${enc(id)}/roles`, { roles })
|
||
|
||
/** 维护员工企微账号(培训提醒推送目标;留空即解绑)。 */
|
||
export const updateOrgEmployeeWecomUserid = (id: string, wecomUserid: string) =>
|
||
put<{ success: boolean; data: OrgEmployee }>(`${base}/employees/${enc(id)}/wecom-userid`, {
|
||
wecom_userid: wecomUserid,
|
||
})
|
||
|
||
/** 从 OA 目录按工号批量拉取企微 userid(预览→写入两段式,预览只读)。 */
|
||
export const previewOrgWecomSync = () =>
|
||
post<{ success: boolean; data: WecomSyncSummary }>(`${base}/employees/wecom-userid-sync/preview`)
|
||
|
||
/** OA 同步写入(无冲突时才落库,返回写入统计)。 */
|
||
export const applyOrgWecomSync = () =>
|
||
post<{ success: boolean; data: WecomSyncSummary }>(`${base}/employees/wecom-userid-sync/apply`)
|
||
|
||
// ---- 归属空间收编(登录默认落点;后端 Owner 限定) ----
|
||
// 员工首登自动开通个人空间后,归属指针(users.tenant_id)停在个人空间,登录后
|
||
// 看到的组织架构/培训是个人空间那份空数据;收编把默认落点改回本空间,
|
||
// 成员关系与个人空间数据不变(可经空间切换器改回,非破坏性)。
|
||
|
||
/** 可收编成员:归属空间不在本空间的活跃成员。 */
|
||
export interface OrgAdoptableUser {
|
||
id: string
|
||
username: string
|
||
display_name: string
|
||
email: string
|
||
home_tenant_id: number
|
||
home_tenant_name: string
|
||
in_roster: boolean
|
||
}
|
||
|
||
/** 单条收编跳过记录(非本空间成员/停用/跨空间账号等,原因逐条回传)。 */
|
||
export interface OrgAdoptSkip {
|
||
user_id: string
|
||
username: string
|
||
display_name: string
|
||
reason: string
|
||
}
|
||
|
||
export interface OrgAdoptResult {
|
||
adopted: number
|
||
skipped: OrgAdoptSkip[]
|
||
}
|
||
|
||
/** 收编预览:归属空间不在本空间的活跃成员列表。 */
|
||
export const listAdoptableUsers = () =>
|
||
get<{ success: boolean; data: OrgAdoptableUser[] }>(`${base}/employees/adoptable`)
|
||
|
||
/** 批量收编:把成员的归属空间(登录默认落点)改为本空间。 */
|
||
export const adoptHomeTenant = (userIds: string[]) =>
|
||
post<{ success: boolean; data: OrgAdoptResult }>(`${base}/employees/adopt-home`, { user_ids: userIds })
|
||
|
||
// ---- 模块角色绑定 ----
|
||
|
||
export const listModuleMembers = (moduleKey: OrgModuleKey) =>
|
||
get<{ success: boolean; data: OrgModuleMember[] }>(
|
||
`${base}/modules/${enc(moduleKey)}/members`,
|
||
)
|
||
|
||
export const putModuleMember = (moduleKey: OrgModuleKey, employeeId: string, roles: string[]) =>
|
||
put<{ success: boolean; data: OrgModuleMember }>(
|
||
`${base}/modules/${enc(moduleKey)}/members/${enc(employeeId)}`,
|
||
{ roles },
|
||
)
|
||
|
||
// ---- 内部小工具 ----
|
||
|
||
function enc(value: string) {
|
||
return encodeURIComponent(value)
|
||
}
|