创建成员
若该成员需要直接关联的相关部门下,则创建成员前,请先同步相关部门组织架构
| 接口名 | POST: /login-register/open-api/employees | |||
| 入参:(body传值) | application/json | |||
| 是否必传 | 默认值 | 描述 | 备注 | |
| integrationId | 是 | - | 成员唯一ID | 该成员在外部平台的唯一ID |
| mobile | 是 | - | 手机号 | 15555555555 |
| name | 否 | 随机生成 | 姓名 | 成员姓名 |
| password | 否 | 6位随机数 | 密码 | 账号密码,可用于手机号-密码登录 |
| roleCode | 否 | ROLE_GENERAL | 角色code 仅允许创建3个管理员账号 | 管理员->ROLE_ENT_CHILD_ADMIN 普通用户->ROLE_GENERAL |
| deptIntegrationIds | 否 | null | 该成员所属部门的唯一标识 | 可以使用部门ID等公司系统内统一的ID,多个用英文逗号隔开 |
| remark | 否 | null | 成员备注 | 成员备注 |
| 入参:(Header传值) | ||||
| Authorization | 是 | - | 传递AccessToken | 格式:Bearer {AccessToken} |
| 入参示例:(application/json) | ||||
|
{
"name": "张三",
"mobile": "13000000000",
"integrationId": "zhangsan",
"password": "",
"roleCode": "ROLE_GENERAL",
"deptIntegrationIds": "1",
"remark": "企微同步"
}
|
||||
| 回参示例:(application/json) | ||||
|
{
"code": 0,
"msg": null,
"data": {
"userId": "1da2df95cc034fa28900704640752ba2"
},
"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/employees
* <p>
* 成功判定:HTTP 200 且 code=0,返回 data.userId
* 失败:HTTP 401 / 非 2xx / code!=0 / 网络异常,返回 null
*/
@Slf4j
public class CreateEmployeeDemo {
/** 接口地址(按实际环境替换 host) */
private static final String CREATE_EMPLOYEE_URL =
"http://your-host/login-register/open-api/employees";
/** AccessToken(按实际值替换,可通过 /auth/oauth2/token 接口预先获取) */
private static final String ACCESS_TOKEN = "your_access_token";
public static void main(String[] args) {
String userId = createEmployee(
"zhangsan", // integrationId(必填)
"13000000000", // mobile(必填)
"张三", // name
"", // password
"ROLE_GENERAL", // roleCode
"1", // deptIntegrationIds
"企微同步" // remark
);
if (userId == null) {
log.warn("创建成员失败");
return;
}
log.info("创建成员成功,userId={}", userId);
}
/**
* 创建成员
*
* @param integrationId 成员唯一ID(必填)
* @param mobile 手机号(必填)
* @param name 姓名(可空)
* @param password 密码(可空)
* @param roleCode 角色code(可空)
* @param deptIntegrationIds 部门唯一标识,多个英文逗号分隔(可空)
* @param remark 成员备注(可空)
* @return 成功返回 data.userId;失败返回 null
*/
public static String createEmployee(String integrationId,
String mobile,
String name,
String password,
String roleCode,
String deptIntegrationIds,
String remark) {
// 1. 参数校验
if (StrUtil.isBlank(integrationId) || StrUtil.isBlank(mobile)) {
log.warn("创建成员必填参数缺失:integrationId={}, mobile={}", integrationId, mobile);
return null;
}
// 2. 组装请求体
Map<String, Object> requestMap = new HashMap<>();
requestMap.put("integrationId", integrationId);
requestMap.put("mobile", mobile);
requestMap.put("name", name);
requestMap.put("password", password);
requestMap.put("roleCode", roleCode);
requestMap.put("deptIntegrationIds", deptIntegrationIds);
requestMap.put("remark", remark);
String requestBody = JSONUtil.toJsonStr(requestMap);
log.info("创建成员请求报文:{}", requestBody);
// 3. 发起请求
try (HttpResponse response = HttpRequest.post(CREATE_EMPLOYEE_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.userId
JSONObject data = json.getJSONObject("data");
return data == null ? null : data.getStr("userId");
} catch (Exception e) {
log.error("调用创建成员接口异常", e);
return null;
}
}
}