You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
10 KiB

2 years ago
package com.dsideal.FengHuang.DingTalk;
2 years ago
import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;
import cn.hutool.core.util.PhoneUtil;
import com.alibaba.fastjson.JSONArray;
2 years ago
import com.alibaba.fastjson.JSONObject;
2 years ago
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
2 years ago
import com.dsideal.FengHuang.Util.CommonUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
2 years ago
import com.taobao.api.ApiException;
2 years ago
import java.util.*;
2 years ago
2 years ago
public class RolePerson {
//暂未改修改,因为我们的角色与测试架构的角色存在冲突,不能在人家的环境中实现全部测试功能,需要搭建自己的专用服务器+架构
2 years ago
/*
--
2 years ago
DROP TABLE IF EXISTS `t_im_role`;
CREATE TABLE `t_im_role` (
`role_id` int NOT NULL COMMENT 'ID',
`role_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '',
`dingtalk_role_id` bigint NULL DEFAULT NULL COMMENT 'ID',
PRIMARY KEY (`role_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of t_im_role
-- ----------------------------
INSERT INTO `t_im_role` VALUES (174, '', NULL);
INSERT INTO `t_im_role` VALUES (323, '', NULL);
insert into `t_im_role` VALUES (4,'',NULL);
TODO
//让产品经理想好加入到此表中表示需要与IM钉钉系统对接的角色
2 years ago
--
2 years ago
select * from t_sys_person_role where bureau_id=? and role_id in (select role_id from t_im_role);
select t1.person_id,t1.role_id,t2.dingtalk_role_id from t_sys_person_role as t1
inner join t_im_role as t2 on t1.role_id=t2.role_id
where t1.bureau_id=2002543
*/
//创建用户组,这个概念似乎不重要,可以手动创建即可
2 years ago
public static void createRoleGroup(String access_token, String group_name) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/add_role_group");
OapiRoleAddrolegroupRequest req = new OapiRoleAddrolegroupRequest();
req.setName(group_name);
OapiRoleAddrolegroupResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
//创建成功后需要将角色组信息记录到自己的表中钉钉未提供查询有哪些角色组的API
//groupId --> write to --->database
}
2 years ago
/**
2 years ago
*
2 years ago
*
2 years ago
* @param access_token
* @param group_id
2 years ago
* @throws ApiException
*/
2 years ago
public static List<Record> getRoleList(String access_token, long group_id) throws ApiException {
2 years ago
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/getrolegroup");
OapiRoleGetrolegroupRequest req = new OapiRoleGetrolegroupRequest();
req.setGroupId(group_id);
OapiRoleGetrolegroupResponse rsp = client.execute(req, access_token);
2 years ago
JSONObject jo = JSONObject.parseObject(rsp.getBody());
JSONArray ja = jo.getJSONObject("role_group").getJSONArray("roles");
List<Record> list = new ArrayList();
for (int i = 0; i < ja.size(); i++) {
Record record = new Record();
record.set("role_id", ((JSONObject) ja.get(i)).getLong("role_id"));
record.set("role_name", ((JSONObject) ja.get(i)).getString("role_name"));
list.add(record);
}
return list;
2 years ago
}
2 years ago
public static long createRole(String access_token, String role_name, long group_id) throws ApiException {
2 years ago
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/add_role");
OapiRoleAddRoleRequest req = new OapiRoleAddRoleRequest();
req.setRoleName(role_name);
req.setGroupId(group_id);
OapiRoleAddRoleResponse rsp = client.execute(req, access_token);
2 years ago
JSONObject jo = JSONObject.parseObject(rsp.getBody());
return jo.getLong("roleId");
}
public static void delRole(String access_token, long role_id) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/deleterole");
OapiRoleDeleteroleRequest req = new OapiRoleDeleteroleRequest();
req.setRoleId(role_id);
OapiRoleDeleteroleResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
public static void updateRole(String access_token, long role_id, String role_name) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/update_role");
OapiRoleUpdateRoleRequest req = new OapiRoleUpdateRoleRequest();
req.setRoleId(role_id);
req.setRoleName(role_name);
OapiRoleUpdateRoleResponse rsp = client.execute(req, access_token);
2 years ago
System.out.println(rsp.getBody());
}
2 years ago
public static void delGroupRole(String access_token, long group_id) throws ApiException {
List<Record> list = getRoleList(access_token, group_id);
for (int i = 0; i < list.size(); i++) {
delRole(access_token, list.get(i).getLong("role_id"));
}
}
public static void syncRole(String access_token, long group_id) throws ApiException {
//1、钉钉侧有哪些角色
List<Record> listA = getRoleList(access_token, group_id);
//2、数据库有哪些角色
List<Record> listB = Model.getRoleList();
boolean flag;
//3、在A不在B删除之
for (int i = 0; i < listA.size(); i++) {
flag = false;
for (int j = 0; j < listB.size(); j++) {
if (listB.get(j).get("dingtalk_role_id") != null && listA.get(i).getLong("role_id") == listB.get(j).getLong("dingtalk_role_id")) {
flag = true;
break;
}
}
if (!flag) {
delRole(access_token, listA.get(i).getLong("role_id"));
CommonUtil.Print("成功删除钉钉侧角色:" + listA.get(i).getStr("role_name"));
}
}
//4、在B不在A增加之
for (int i = 0; i < listB.size(); i++) {
flag = false;
for (int j = 0; j < listA.size(); j++) {
if (listB.get(j).get("dingtalk_role_id") != null && listA.get(i).getLong("role_id") == listB.get(j).getLong("dingtalk_role_id")) {
flag = true;
break;
}
}
if (!flag) {
long dt_role_id = createRole(access_token, listB.get(i).getStr("role_name"), group_id);
//回写
Model.writeDingTalkRoleId(listB.get(i).getInt("role_id"), dt_role_id);
CommonUtil.Print("成功创建角色:"+listB.get(i).getStr("role_name"));
}
}
//5、在A也在B看看是不是角色名称需要修改
}
2 years ago
/**
2 years ago
*
2 years ago
*
2 years ago
* @param access_token
* @param role_id
* @throws ApiException
2 years ago
*/
2 years ago
public static List<String> getRolePersonList(String access_token, long role_id) throws ApiException {
2 years ago
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/simplelist");
2 years ago
List<String> res = new ArrayList<>();
long start = 0;
while (true) {
OapiRoleSimplelistRequest req = new OapiRoleSimplelistRequest();
req.setRoleId(role_id);
req.setSize(20L);
req.setOffset(start);
OapiRoleSimplelistResponse rsp = client.execute(req, access_token);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
System.out.println(jo);
break;
}
return res;
}
/**
*
*
* @param accessToken
* @throws ApiException
*/
public static void getRoleList(String accessToken) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/list");
OapiRoleListRequest req = new OapiRoleListRequest();
2 years ago
req.setSize(20L);
req.setOffset(0L);
2 years ago
OapiRoleListResponse rsp = client.execute(req, accessToken);
2 years ago
System.out.println(rsp.getBody());
}
2 years ago
2 years ago
public static void updateRole(String accessToken) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/update_role");
OapiRoleUpdateRoleRequest req = new OapiRoleUpdateRoleRequest();
req.setRoleId(1560985325L);
req.setRoleName("服装制造");
OapiRoleUpdateRoleResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
2 years ago
public static void addRolePerson(String accessToken, String role_id, String ids) throws ApiException {
//一次最多20个人员
2 years ago
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/addrolesforemps");
OapiRoleAddrolesforempsRequest req = new OapiRoleAddrolesforempsRequest();
2 years ago
req.setRoleIds(role_id);
req.setUserIds(ids);
2 years ago
OapiRoleAddrolesforempsResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
2 years ago
public static void delRolePerson(String accessToken, String role_id, String ids) throws ApiException {
2 years ago
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/removerolesforemps");
OapiRoleRemoverolesforempsRequest req = new OapiRoleRemoverolesforempsRequest();
2 years ago
req.setRoleIds(role_id);
req.setUserIds(ids);
2 years ago
OapiRoleRemoverolesforempsResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
}