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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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.*;
public class RolePerson {
//暂未改修改,因为我们的角色与测试架构的角色存在冲突,不能在人家的环境中实现全部测试功能,需要搭建自己的专用服务器+架构
/*
-- 需要产品人员整理出我们管理软件需要的角色有哪些,是现在云平台角色表的子集
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 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();
req.setName(group_name);
OapiRoleAddrolegroupResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
//创建成功后需要将角色组信息记录到自己的表中钉钉未提供查询有哪些角色组的API
//groupId --> write to --->database
}
/**
* 功能:获取指定角色组下有哪些角色
*
* @param access_token
* @param 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);
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 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看看是不是角色名称需要修改
}
/**
* 功能:角色下人员列表
*
* @param access_token
* @param role_id
* @throws ApiException
*/
public static List<String> getRolePersonList(String access_token, long role_id) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/simplelist");
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();
req.setSize(20L);
req.setOffset(0L);
OapiRoleListResponse 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();
req.setRoleId(1560985325L);
req.setRoleName("服装制造");
OapiRoleUpdateRoleResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
public static void addRolePerson(String accessToken, String role_id, String ids) throws ApiException {
//一次最多20个人员
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/addrolesforemps");
OapiRoleAddrolesforempsRequest req = new OapiRoleAddrolesforempsRequest();
req.setRoleIds(role_id);
req.setUserIds(ids);
OapiRoleAddrolesforempsResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
public static void delRolePerson(String accessToken, String role_id, String ids) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/role/removerolesforemps");
OapiRoleRemoverolesforempsRequest req = new OapiRoleRemoverolesforempsRequest();
req.setRoleIds(role_id);
req.setUserIds(ids);
OapiRoleRemoverolesforempsResponse rsp = client.execute(req, accessToken);
System.out.println(rsp.getBody());
}
}