main
黄海 2 years ago
parent 018d723ae8
commit 5fd816963c

@ -13,6 +13,8 @@ import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import java.util.List;
public class TestDingTalk {
public static DruidPlugin createDruidPlugin(String url, String username, String password, String driverClass) {
DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driverClass);
@ -68,37 +70,35 @@ public class TestDingTalk {
Record rOrg = Model.getOrgByOrgName(orgName);
//同步组织机构
OrgPerson.syncOrg(accessToken, rOrg);
//OrgPerson.syncOrg(accessToken, rOrg);
//从数据库中获取最新的部门列表(已与钉钉匹配完毕)
Model.fillDeptListByDataBase(rOrg);
//Model.fillDeptListByDataBase(rOrg);
//删除所有的组织机构
// OrgPerson.delAllDept(accessToken);
//OrgPerson.delAllDept(accessToken);
//同步人员
// OrgPerson.syncPerson(accessToken, rOrg);
//OrgPerson.syncPerson(accessToken, rOrg);
//删除单位下所有人员(开发测试时使用)
// OrgPerson.delBureauPerson(accessToken, rOrg);
//创建角色组
//创建角色组【执行一次即可】
//RolePerson.createRoleGroup(accessToken, "义务教育阶段角色组"); ---> 3779920123
long groupId = 3779920123L;
//获取指定角色组下有哪些角色
RolePerson.getRoleList(accessToken, groupId);
// RolePerson.createRole(accessToken, "校长", groupId);
// RolePerson.createRole(accessToken, "副校长", groupId);
// RolePerson.createRole(accessToken, "教导主任", groupId);
// RolePerson.createRole(accessToken, "后勤主任", groupId);
// RolePerson.createRole(accessToken, "班主任", groupId);
// RolePerson.createRole(accessToken, "普通教师", groupId);
//
// RolePerson.getRoleList(accessToken, groupId);
//System.out.println(RolePerson.getRolePersonList(accessToken,role_id));
//List<Record> list = RolePerson.getRoleList(accessToken, groupId);
//System.out.println(list);
//删除指定角色组下的角色
//RolePerson.delGroupRole(accessToken,groupId);
//同步角色
//RolePerson.syncRole(accessToken, groupId);
CommonUtil.Print("恭喜,所有操作成功完成!");
}

@ -52,4 +52,14 @@ public class Model {
String sql = "select org_id,org_name,sort_id,dingtalk_dept_id from t_base_organization where org_name = ?";
return Db.findFirst(sql, org_name);
}
public static List<Record> getRoleList() {
String sql = "select * from t_im_role";
return Db.find(sql);
}
public static void writeDingTalkRoleId(int role_id,long dt_role_id){
String sql = "update t_im_role set dingtalk_role_id=? where role_id=?";
Db.update(sql, dt_role_id, role_id);
}
}

@ -1,23 +1,52 @@
package com.dsideal.FengHuang.DingTalk;
import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;
import cn.hutool.core.util.PhoneUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.dsideal.FengHuang.Util.CommonUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.taobao.api.ApiException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class RolePerson {
//暂未改修改,因为我们的角色与测试架构的角色存在冲突,不能在人家的环境中实现全部测试功能,需要搭建自己的专用服务器+架构
/*
--
select * from t_sys_role;
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钉钉系统对接的角色
--
select * from t_sys_person_role where bureau_id=? and role_id in ();
*/
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
*/
//创建用户组,这个概念似乎不重要,可以手动创建即可
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();
@ -35,23 +64,97 @@ public class RolePerson {
* @param group_id
* @throws ApiException
*/
public static void getRoleList(String access_token, long group_id) throws ApiException {
public static List<Record> getRoleList(String access_token, long group_id) throws ApiException {
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);
System.out.println(rsp.getBody());
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;
}
public static void createRole(String access_token, String role_name, long group_id) throws ApiException {
public static long createRole(String access_token, String role_name, long group_id) throws ApiException {
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);
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);
System.out.println(rsp.getBody());
}
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看看是不是角色名称需要修改
}
/**
*
*
@ -92,15 +195,6 @@ public class RolePerson {
}
public static void createRole(String accessToken) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/add_role");
OapiRoleAddRoleRequest req = new OapiRoleAddRoleRequest();
req.setRoleName("测试1");
req.setGroupId(1507113595L);
OapiRoleAddRoleResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
public static void updateRole(String accessToken) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/role/update_role");
OapiRoleUpdateRoleRequest req = new OapiRoleUpdateRoleRequest();
@ -110,13 +204,6 @@ public class RolePerson {
System.out.println(rsp.getBody());
}
public static void delRole(String accessToken) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/deleterole");
OapiRoleDeleteroleRequest req = new OapiRoleDeleteroleRequest();
req.setRoleId(1581321999L);
OapiRoleDeleteroleResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
public static void addRolePerson(String accessToken, String role_id, String ids) throws ApiException {
//一次最多20个人员

Loading…
Cancel
Save