SView Help Center
English (US)

创建部门

Author: sviewngts Last Updated: 2026-08-25 16:28
需按层级依次向下创建,父部门优先创建
接口名 POST: /login-register/open-api/departments
入参:(body传值) application/json
是否必传 默认值 描述 备注
integrationId 是 - 部门唯一ID 该部门在外部平台的唯一ID
deptName 是 - 部门名称  
sortOrder 是 0 部门排序值  
parentDeptIntegrationId 否 0 父部门唯一ID 该部门在外部平台的唯一ID "0"为根部门
入参:(Header传值)
Authorization 是 - 传递AccessToken 格式:Bearer {AccessToken}
入参示例:(application/json)

 

{
    "integrationId": "1",
    "deptName": "SView",
    "sortOrder": 0,
    "parentDeptIntegrationId": "0"
}
回参示例:(application/json)

 

 
{
    "code": 0,
    "msg": null,
    "data": {
        "deptId": "13fd6efd4b134e17ae0fd632b0d634c1"
    },
    "ok": true
}
package com.hoteam.sview.demo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建部门(部门同步,组织架构复制)Demo
 * 接口:POST /login-register/open-api/departments
 * <p>
 * 注意:需按层级依次向下创建,父部门优先创建
 * <p>
 * 成功判定:HTTP 200 且 code=0,返回 data.deptId
 * 失败:HTTP 401 / 非 2xx / code!=0 / 网络异常,返回 null
 */
@Slf4j
public class CreateDepartmentHutoolDemo {

    /** 接口地址(按实际环境替换 host) */
    private static final String CREATE_DEPARTMENT_URL =
            "http://your-host/login-register/open-api/departments";

    /** AccessToken(按实际值替换,可通过 /auth/oauth2/token 接口预先获取) */
    private static final String ACCESS_TOKEN = "your_access_token";

    public static void main(String[] args) {
        // 示例:创建根部门
        String rootDeptId = createDepartment(
                "1",         // integrationId(必填)
                "SView",     // deptName(必填)
                0,           // sortOrder(必填,默认 0)
                "0"          // parentDeptIntegrationId(可空,"0" 表示根部门)
        );
        if (rootDeptId == null) {
            log.warn("创建根部门失败");
            return;
        }
        log.info("创建根部门成功,deptId={}", rootDeptId);

        // 示例:在根部门下创建子部门(按层级依次创建,父部门优先)
        String subDeptId = createDepartment(
                "2",
                "研发中心",
                1,
                "1"          // 父部门的 integrationId
        );
        log.info("创建子部门结果:deptId={}", subDeptId);
    }

    /**
     * 创建部门
     *
     * @param integrationId           部门唯一ID(必填,外部平台唯一ID)
     * @param deptName                部门名称(必填)
     * @param sortOrder               部门排序值(必填,默认 0)
     * @param parentDeptIntegrationId 父部门唯一ID(可空,"0" 为根部门)
     * @return 成功返回 data.deptId;失败返回 null
     */
    public static String createDepartment(String integrationId,
                                          String deptName,
                                          Integer sortOrder,
                                          String parentDeptIntegrationId) {
        // 1. 参数校验
        if (StrUtil.isBlank(integrationId) || StrUtil.isBlank(deptName)) {
            log.warn("创建部门必填参数缺失:integrationId={}, deptName={}", integrationId, deptName);
            return null;
        }

        // 2. 组装请求体
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("integrationId", integrationId);
        requestMap.put("deptName", deptName);
        requestMap.put("sortOrder", sortOrder == null ? 0 : sortOrder);
        requestMap.put("parentDeptIntegrationId",
                StrUtil.isBlank(parentDeptIntegrationId) ? "0" : parentDeptIntegrationId);
        String requestBody = JSONUtil.toJsonStr(requestMap);
        log.info("创建部门请求报文:{}", requestBody);

        // 3. 发起请求
        try (HttpResponse response = HttpRequest.post(CREATE_DEPARTMENT_URL)
                // 公共 Header
                .header("tenant-id", "1")
                .header("deviceId", "web")
                .header("deviceModel", "web")
                .header("deviceType", "4")
                .header("lang", "zh-CN")
                // 认证 + Content-Type
                .header(Header.AUTHORIZATION, "Bearer " + ACCESS_TOKEN)
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
                .body(requestBody)
                .timeout(10_000)
                .execute()) {

            int status = response.getStatus();
            String body = response.body();
            log.info("创建部门响应:status={}, body={}", status, body);

            // 3.1 401 认证失败
            if (status == HttpStatus.HTTP_UNAUTHORIZED) {
                log.warn("创建部门认证失败(401),accessToken 可能失效,body={}", body);
                return null;
            }
            // 3.2 非 2xx 或响应体为空
            if (!response.isOk() || StrUtil.isBlank(body)) {
                log.warn("创建部门请求失败:status={}, body={}", status, body);
                return null;
            }

            // 4. 业务字段判定:仅以 code=0 为成功
            JSONObject json = JSONUtil.parseObj(body);
            Integer code = json.getInt("code");
            if (code == null || code != 0) {
                log.warn("创建部门业务失败:code={}, msg={}", code, json.getStr("msg"));
                return null;
            }

            // 5. 成功,返回 data.deptId
            JSONObject data = json.getJSONObject("data");
            return data == null ? null : data.getStr("deptId");
        } catch (Exception e) {
            log.error("调用创建部门接口异常", e);
            return null;
        }
    }
}