Files
ai_academy/internal/handler/orgcenter/orgcenter.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

225 lines
7.0 KiB
Go

package orgcenter
import (
"encoding/json"
"net/http"
"strconv"
"academy/internal/errors"
"academy/internal/types"
"academy/internal/types/interfaces"
"github.com/gin-gonic/gin"
)
// Handler 组织中心 HTTP 处理器。权限模型:
// - 组织架构为租户级共享数据,读写由空间 RBAC 把关(路由层 Viewer 读 / Contributor 写);
// - 各业务模块的角色绑定(module_key)由模块自己的接口/handler 细控,
// 本层不感知培训等模块的业务角色语义。
type Handler struct {
svc interfaces.OrgCenterService
}
// NewHandler constructs the org center handler.
func NewHandler(svc interfaces.OrgCenterService) *Handler {
return &Handler{svc: svc}
}
// callerTenantID reads the tenant id injected by the auth middleware.
func callerTenantID(c *gin.Context) uint64 {
return c.GetUint64(types.TenantIDContextKey.String())
}
// orgListQueryFromRequest parses the common list filters from the request.
func orgListQueryFromRequest(c *gin.Context) *types.OrgListQuery {
q := &types.OrgListQuery{
DeptID: c.Query("dept_id"),
Keyword: c.Query("keyword"),
}
if v, err := strconv.Atoi(c.Query("page")); err == nil {
q.Page = v
}
if v, err := strconv.Atoi(c.Query("page_size")); err == nil {
q.PageSize = v
}
return q
}
// ---------------------------------------------------------------------------
// 部门
// ---------------------------------------------------------------------------
// ListDepts GET /org/depts
func (h *Handler) ListDepts(c *gin.Context) {
depts, err := h.svc.ListDepts(c.Request.Context(), callerTenantID(c))
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": depts})
}
// saveDeptRequest is the body of POST /org/depts.
type saveDeptRequest struct {
ID string `json:"id"`
ParentID string `json:"parent_id"`
Name string `json:"name" binding:"required"`
Code string `json:"code"`
Sort int `json:"sort"`
}
// SaveDept POST /org/depts (id 非空走更新)
func (h *Handler) SaveDept(c *gin.Context) {
ctx := c.Request.Context()
var req saveDeptRequest
if err := c.ShouldBindJSON(&req); err != nil {
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
dept, err := h.svc.SaveDept(ctx, callerTenantID(c), &types.OrgDept{
ID: req.ID, ParentID: req.ParentID, Name: req.Name, Code: req.Code, Sort: req.Sort,
})
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": dept})
}
// DeleteDept DELETE /org/depts/:id
func (h *Handler) DeleteDept(c *gin.Context) {
ctx := c.Request.Context()
if err := h.svc.DeleteDept(ctx, callerTenantID(c), c.Param("id")); err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true})
}
// ---------------------------------------------------------------------------
// 员工档案
// ---------------------------------------------------------------------------
// ListEmployees GET /org/employees?module_key=&dept_id=&keyword=&page=&page_size=
// module_key 可选:传入时附加该模块的绑定角色展示(如 training)。
func (h *Handler) ListEmployees(c *gin.Context) {
employees, total, err := h.svc.ListEmployees(
c.Request.Context(), callerTenantID(c), c.Query("module_key"), orgListQueryFromRequest(c))
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"items": employees, "total": total}})
}
// importEmployeeItem is one row of the employee import payload.
type importEmployeeItem struct {
UserID string `json:"user_id" binding:"required"`
EmployeeNo string `json:"employee_no"`
DeptID string `json:"dept_id" binding:"required"`
Post string `json:"post"`
WecomUserid string `json:"wecom_userid"`
Status string `json:"status"`
}
// ImportEmployeesRequest is the body of POST /org/employees/import.
type ImportEmployeesRequest struct {
Employees []importEmployeeItem `json:"employees" binding:"required,min=1"`
}
// ImportEmployees POST /org/employees/import (部门必填,服务层校验命中)
func (h *Handler) ImportEmployees(c *gin.Context) {
ctx := c.Request.Context()
var req ImportEmployeesRequest
if err := c.ShouldBindJSON(&req); err != nil {
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
employees := make([]*types.OrgEmployee, 0, len(req.Employees))
for _, item := range req.Employees {
employees = append(employees, &types.OrgEmployee{
UserID: item.UserID,
EmployeeNo: item.EmployeeNo,
DeptID: item.DeptID,
Post: item.Post,
WecomUserid: item.WecomUserid,
Status: item.Status,
})
}
count, err := h.svc.ImportEmployees(ctx, callerTenantID(c), employees)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"imported": count}})
}
// ListPlatformUsers GET /org/platform-users — 员工导入的用户选择器数据源。
func (h *Handler) ListPlatformUsers(c *gin.Context) {
users, err := h.svc.ListTenantUsers(c.Request.Context(), callerTenantID(c))
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": users})
}
// ---------------------------------------------------------------------------
// 模块角色绑定
// ---------------------------------------------------------------------------
// ListModuleMembers GET /org/modules/:module_key/members
func (h *Handler) ListModuleMembers(c *gin.Context) {
members, err := h.svc.ListModuleMembers(
c.Request.Context(), callerTenantID(c), c.Param("module_key"))
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": members})
}
// putModuleMemberRequest is the body of PUT /org/modules/:module_key/members/:employee_id.
type putModuleMemberRequest struct {
Roles []string `json:"roles"`
Extra map[string]any `json:"extra"`
}
// PutModuleMember PUT /org/modules/:module_key/members/:employee_id
func (h *Handler) PutModuleMember(c *gin.Context) {
ctx := c.Request.Context()
var req putModuleMemberRequest
if err := c.ShouldBindJSON(&req); err != nil {
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
member, err := h.svc.PutModuleMember(ctx, callerTenantID(c), &types.OrgModuleMember{
ModuleKey: c.Param("module_key"),
EmployeeID: c.Param("employee_id"),
Roles: orgJSONFromRoles(req.Roles),
Extra: orgJSONFromAny(req.Extra),
})
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": member})
}
// orgJSONFromRoles encodes a role list as the roles jsonb payload.
func orgJSONFromRoles(roles []string) types.JSON {
if roles == nil {
roles = []string{}
}
data, _ := json.Marshal(roles)
return types.JSON(data)
}
// orgJSONFromAny encodes an arbitrary map as the extra jsonb payload.
func orgJSONFromAny(extra map[string]any) types.JSON {
if extra == nil {
extra = map[string]any{}
}
data, _ := json.Marshal(extra)
return types.JSON(data)
}