背景
获取浙政钉组织架构、可以根据上架浙政钉应用的使用范围权限去对应获取组织结构。今天我以湖州市吴兴区为例来获取组织结构。
目录
?一、准备工作
?二、创建组织结构表
三、代码开发
1.yml配置文件
2.获取通讯录信息
3.根据组织code获取组织详情
4.获取下一级组织 Code 列表
5.批量根据组织Code查询详情
6.开始入库
7.数据库数据
?一、准备工作
1.【浙政钉】第一篇:企业内应用免登_郭老师的小迷弟雅思莫了的博客-CSDN博客
?二、创建组织结构表
mysql创建表脚本
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_zzd_organization
-- ----------------------------
DROP TABLE IF EXISTS `t_zzd_organization`;
CREATE TABLE `t_zzd_organization` (
`uuid` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`organization_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '组织机构名称',
`organization_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '组织机构code',
`parent_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '上一级组织机构名称',
`parent_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '上一级组织机构code',
`status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '状态 A:有效 F:无效',
PRIMARY KEY (`uuid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '浙政钉组织架构' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
三、代码开发
1.yml配置文件
#专有钉钉参数
zzdingTalk :
appKey : x-sBDVZNzCxxxxxxxx
appSecret : xxxxxC01z99LU6S0ypc0x
domainName : openplatform-pro.ding.zj.gov.cn
protocal : https
tenantId : 111111
domainName:调用服务的域名
服务端 | 域名 | 浙政钉 | openplatform-pro.ding.zj.gov.cn | 专有钉钉 | ? openplatform.dg-work.cn |
专有钉钉是浙政钉测试环境,所以后期上线只需替换yml文件里参数即可。
2.获取通讯录信息
tenantId :为租户id,简单点说就是你登陆的账号id
如何知道你的tenantId呢?你可以在【浙政钉】第一篇:企业内应用免登_郭老师的小迷弟雅思莫了的博客-CSDN博客查看。
/***
* 获取通讯录权限(组织架构)
* @return
* "content": {
* "stripLineAddressEmployeeScopes": [],
* "stripLineAddressScopes": [],
* "userVisibleScopes": [],
* "deptVisibleScopes": [
* "GO_24b8c3d44dfxxxxxxxxx"
* ]
* }
*/
public JSONObject getBookPermissions() {
ExecutableClient executableClient = ExecutableClient.getInstance();
executableClient.setDomainName(domainName);
executableClient.setProtocal(protocal);
executableClient.setAccessKey(appKey);
executableClient.setSecretKey(appSecret);
executableClient.init();
String api = "/auth/scopesV2";
PostClient postClient = executableClient.newPostClient(api);
postClient.addParameter("tenantId", tenantId);
String apiResult = postClient.post();
log.info("获取浙政钉获取通讯录权限:{}", apiResult);
JSONObject jsonObject = JSONObject.parseObject(apiResult);
if (jsonObject.getBoolean("success")) {
return jsonObject.getJSONObject("content");
}
return null;
}
log.info("获取浙政钉获取通讯录权限:{}", apiResult);
{
success: true,
content:{
"userVisibleScopes": ["人员编码1", "人员编码2"],
"deptVisibleScopes": ["组织编码1", "组织编码2"],
"stripLineAddressScopes": [
{
"domainCode": "条线通讯录code",
"stripLineAddressCodes": ["条线通讯录节点编码1", "条线通讯录节点编码2"]
}
],
"stripLineAddressEmployeeScopes": [
{
"domainCode": "条线通讯录code",
"employeeCodes": ["条线通讯录人员编码1", "条线通讯录人员编码2"]
}
]
},
errorLevel:"",
errorCode:"0",
errorMsg:"success"
}
返回参数:?
参数 | 类型 | 说明 |
---|
success | Boolean | 是否调用成功 | responseCode | String | 响应码。- 0:表示成功- 其他:表示失败 | responseMessage | String | 响应信息 | deptVisibleScopes | List | 企业授权的部门编码列表。返回值为授权部门编码的并集 (设置“全部员工”时,返回授权的部门编码为根部门ID,员工userid列表为空) | userVisibleScopes | List | 企业授权的员工编码列表。返回值为授权人员编码的并集。 | stripLineAddressScopes | List | 授予的条线通讯录节点列表 | stripLineAddressEmployeeScopes | List | 授予的条线通讯录人员列表 |
3.根据组织code获取组织详情
/***
* 根据组织code获取组织详情
* @param code
* @return
*/
public JSONObject getOrganizationByCode(String code) {
ExecutableClient executableClient = ExecutableClient.getInstance();
executableClient.setDomainName(domainName);
executableClient.setProtocal(protocal);
executableClient.setAccessKey(appKey);
executableClient.setSecretKey(appSecret);
executableClient.init();
String api = "/mozi/organization/getOrganizationByCode";
PostClient postClient = executableClient.newPostClient(api);
postClient.addParameter("organizationCode", code);
postClient.addParameter("tenantId", tenantId);
String apiResult = postClient.post();
log.info("获取浙政钉根据组织code获取组织详情:{}", apiResult);
JSONObject jsonObject = JSONObject.parseObject(apiResult);
if (jsonObject.getBoolean("success")) {
return jsonObject.getJSONObject("content").getJSONObject("data");
}
return null;
}
返回信息:
成功返回案例
{
"success": true,
"content": {
"data": {
"typeName": "虚节点",
"organizationName": "余杭区",
"typeCode": "GOV_HOLLOW_NODE",
"gmtCreate": "2019-10-21 19:15:13",
"parentName": "杭州市",
"parentCode": "75",
"organizationCode": "76",
"status": "A", //A - 有效的数据, F - 无效的数据
"responsibleEmployeeCodes":"GE_12j2hj33333ffff3e4r5tyhg5432wdv5|GE_1234hj33333ffff3e4r5tyhg5432wdv5",
"displayOrder":"1",
"businessStripCodes":"100|101",
"address":"浙江省杭州市xxx",
"contactEmployeeCode":"",
"contactNumber":"",
"divisionCode":"30000000000",
"shortName":"",
"institutionCode":"",
"otherName":"",
"postalCode":"",
"remarks":"",
"unifiedSocialCreditCode":"",
"institutionLevelCode":"",
"organizationExtProperties":[
{
"code":"ext_GOV_UNIT_fe789104",
"type":"3",
"value":[
"1"
]
},
{
"code":"ext_GOV_UNIT_ae789144",
"type":"2",
"value":[
"1"
]
},
{
"code":"ext_GOV_UNIT_3kr87wj3",
"type":"0",
"value":[
"aa"
]
}
]
},
"success": true,
"requestId": "44c76830-8df1-4a42-8d4d-8f53d54387e3",
"responseMessage": "OK",
"responseCode": "0"
}
}
4.获取下一级组织 Code 列表
/***
* 获取下一级组织 Code 列表
* @param code
* @return
*/
public JSONObject getNextLevelOrganization(String code) {
ExecutableClient executableClient = ExecutableClient.getInstance();
executableClient.setDomainName(domainName);
executableClient.setProtocal(protocal);
executableClient.setAccessKey(appKey);
executableClient.setSecretKey(appSecret);
executableClient.init();
String api = "/mozi/organization/pageSubOrganizationCodes";
PostClient postClient = executableClient.newPostClient(api);
postClient.addParameter("returnTotalSize", "true");
postClient.addParameter("pageSize", "100");
postClient.addParameter("organizationCode", code);
postClient.addParameter("pageNo", "1");
postClient.addParameter("status", "A");
postClient.addParameter("tenantId", tenantId);
String apiResult = postClient.post();
log.info("获取浙政钉获取下一级组织 Code 列表:{}", apiResult);
JSONObject jsonObject = JSONObject.parseObject(apiResult);
if (jsonObject.getBoolean("success")) {
return jsonObject.getJSONObject("content");
}
return null;
}
返回信息:
成功返回案例
{
"success": true,
"content": {
"totalSize": 2,
"data": ["GE_f6757f0fe6c048dc9dd8f62fdf156aef",
"GE_b5ec29b8c869419db1ac3fbba71983fb"],
"success": true,
"requestId": "f04d59de-ea7c-43fc-87b6-12b139cf64f3",
"pageSize": 10,
"responseMessage": "OK",
"currentPage": 1,
"responseCode": "0"
}
}
5.批量根据组织Code查询详情
/***
* 批量根据组织Code查询详情
* @param codes
* @return
*/
public JSONArray getListOrganizationsByCodes(List<String> codes) {
ExecutableClient executableClient = ExecutableClient.getInstance();
executableClient.setDomainName(domainName);
executableClient.setProtocal(protocal);
executableClient.setAccessKey(appKey);
executableClient.setSecretKey(appSecret);
executableClient.init();
IntelligentPostClient intelligentPostClient = executableClient.newIntelligentPostClient("/mozi/organization/listOrganizationsByCodes");
OapiMoziOrganizationListOrganizationsByCodesRequest oapiMoziOrganizationListOrganizationsByCodesRequest = new OapiMoziOrganizationListOrganizationsByCodesRequest();
oapiMoziOrganizationListOrganizationsByCodesRequest.setOrganizationCodes(codes);
oapiMoziOrganizationListOrganizationsByCodesRequest.setTenantId(Long.valueOf(tenantId));
//获取结果
OapiMoziOrganizationListOrganizationsByCodesResponse apiResult = intelligentPostClient.post(oapiMoziOrganizationListOrganizationsByCodesRequest);
log.info("获取浙政钉获取通讯录权限:{}", apiResult.getContent().getData());
if (apiResult.getContent().getSuccess()) {
return JSONArray.parseArray(apiResult.getContent().getData());
}
return null;
}
返回信息:
成功返回案例
{
"success": true,
"content": {
"data": [{
"creator": "jianqin.ljq",
"gmtModified": "2019-10-24 11:15:12",
"typeName": "单位",
"organizationName": "教育局",
"modifier": "jianqin.ljq",
"typeCode": "GOV_UNIT",
"displayOrder": 0,
"gmtCreate": "2019-10-24 11:15:12",
"parentName": "杭州市",
"parentCode": "75",
"organizationCode": "75-1",
"status": "A", //A - 有效的数据, F - 无效的数据
"responsibleEmployeeCodes":"GE_12j2hj33333ffff3e4r5tyhg5432wdv5|GE_1234hj33333ffff3e4r5tyhg5432wdv5",
"businessStripCodes":"100|101",
"address":"浙江省杭州市xxx",
"contactEmployeeCode":"",
"contactNumber":"",
"divisionCode":"30000000000",
"shortName":"",
"institutionCode":"",
"otherName":"",
"postalCode":"",
"remarks":"",
"unifiedSocialCreditCode":"",
"institutionLevelCode":"",
"institutionLevelCode":"",
"organizationExtProperties":[
{
"code":"ext_GOV_UNIT_fe789104",
"type":"3",
"value":[
"1"
]
},
{
"code":"ext_GOV_UNIT_ae789144",
"type":"2",
"value":[
"1"
]
},
{
"code":"ext_GOV_UNIT_3kr87wj3",
"type":"0",
"value":[
"aa"
]
}
]
}, {
"creator": "jianqin.ljq",
"gmtModified": "2019-10-24 11:17:41",
"typeName": "单位",
"organizationName": "民政局",
"modifier": "jianqin.ljq",
"typeCode": "GOV_UNIT",
"displayOrder": 1,
"gmtCreate": "2019-10-24 11:17:41",
"parentName": "杭州市",
"parentCode": "75",
"organizationCode": "75-2",
"status": "A",
"responsibleEmployeeCodes":"GE_12j2hj33333ffff3e4r5tyhg5432wdv5|GE_1234hj33333ffff3e4r5tyhg5432wdv5",
"businessStripCodes":"100|101",
"address":"浙江省杭州市xxx",
"contactEmployeeCode":"",
"contactNumber":"",
"divisionCode":"30000000000",
"shortName":"",
"institutionCode":"",
"otherName":"",
"postalCode":"",
"remarks":"",
"unifiedSocialCreditCode":"",
"institutionLevelCode":"",
"institutionLevelCode":"",
"institutionNature":"",
"institutionCategory":"",
"organizationExtProperties":[
{
"code":"ext_GOV_UNIT_fe789104",
"type":"3",
"value":[
"1"
]
},
{
"code":"ext_GOV_UNIT_ae789144",
"type":"2",
"value":[
"1"
]
},
{
"code":"ext_GOV_UNIT_3kr87wj3",
"type":"0",
"value":[
"aa"
]
}
]
}],
"success": true,
"requestId": "44c76830-8df1-4a42-8d4d-8f53d54387e3",
"responseMessage": "OK",
"responseCode": "0"
}
}
6.开始入库
/***
* 获取下一级组织 Code 列表
* deptVisibleScopes可能存在多个
*/
public R getNextLevelInfo() {
JSONObject jsonObject = getBookPermissions();
if (jsonObject != null) {
//获取组织code列表
List<String> codes = (List<String>) jsonObject.get("deptVisibleScopes");
if (!CollectionUtils.isEmpty(codes)) {
codes.forEach(x -> {
//获取本组织架构信息
JSONObject data = getOrganizationByCode(x);
if (data != null) {
//保存父类组织架构
ZzdOrganizationEntity entity = new ZzdOrganizationEntity();
entity.setOrganizationCode(data.getString("organizationCode"));
entity.setOrganizationName(data.getString("organizationName"));
entity.setParentCode(data.getString("parentCode"));
entity.setParentName(data.getString("parentName"));
entity.setStatus(data.getString("status"));
zzdOrganizationService.insertOrganizationInfo(entity);
}
//获取下一级组织架构信息
JSONObject nextData = getNextLevelOrganization(x);
if (nextData != null) {
//获取到了下一级组织code
List<String> nextCodes = (List<String>) nextData.get("data");
JSONArray arrays = getListOrganizationsByCodes(nextCodes);
if (arrays != null && arrays.size() > 0) {
arrays.forEach(y -> {
JSONObject jsInfo = (JSONObject) y;
//保存组织架构
ZzdOrganizationEntity entity = new ZzdOrganizationEntity();
entity.setOrganizationCode(jsInfo.getString("organizationCode"));
entity.setOrganizationName(jsInfo.getString("organizationName"));
entity.setParentCode(jsInfo.getString("parentCode"));
entity.setParentName(jsInfo.getString("parentName"));
entity.setStatus(jsInfo.getString("status"));
zzdOrganizationService.insertOrganizationInfo(entity);
});
}
}
});
}
}
return R.ok();
}
附赠R实体类
import java.util.HashMap;
import java.util.Map;
/**
*
* @author zsb
* @date 2017-8-10
*/
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public R() {
put("code", 0);
}
public R(Integer code) {
put("code", code);
put("data", new HashMap<String, Object>());
}
public R(Integer code, String msg) {
put("code", code);
put("msg", msg);
put("data", new HashMap<String, Object>());
}
public static R error() {
return error(500, "未知异常,请联系管理员");
}
public static R errorDebug(String message) {
return error(500, "未知异常 " + message + ",请联系管理员");
}
public static R error(String msg) {
return error(500, msg);
}
public static R error(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
public R errorInfo(String msg) {
this.put("errorMsg", msg);
return this;
}
public static R ok(String msg) {
R r = new R();
r.put("msg", msg);
r.put("data", new HashMap<String, Object>());
return r;
}
public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
r.put("data", new HashMap<String, Object>());
return r;
}
public static R ok() {
return new R().put("msg", "success").put("data", new HashMap<String, Object>());
}
public static R ok(Integer size) {
return new R().put("data", new HashMap<String, Object>((int)Math.round(size / 0.75)));
}
@Override
public R put(String key, Object value) {
super.put(key, value);
return this;
}
/**
* 添加返回结果数据
*
* @param key
* @param value
* @return
*/
public R putData(String key, Object value) {
Map<String, Object> map = (HashMap<String, Object>)this.get("data");
map.put(key, value);
return this;
}
}
controller层:
/***
* 获取吴兴区浙政钉组织架构
* @return
*/
@ApiOperation(value = "获取吴兴区浙政钉组织架构")
@RequestMapping(value = "/dingTalk/getNextLevelInfo", method = RequestMethod.GET)
public ResponseEntity<?> getNextLevelInfo() {
return ResponseEntity.ok(dingTalkService.getNextLevelInfo());
}
7.数据库数据
|