废话不多说,直接上代码。
实现与政务钉钉的人员集成,待办推送以及单点登录.
将次配置文件由模版路径拷贝到config目录下.
配置文件名: zhengwuDingding.json
模版路径:o2server/config/sample/zhengwuDingding.json
工作路径:o2server/config/zhengwuDingding.json
zhengwuDingding.json:
{
"appId": "",
"appSecret": "",
"agentId":"",
"syncOrganizationDirection": "disabe",
"syncOrganizationCallbackAddress": "http://127.0.0.1:20030/x_program_center/jaxrs/zhengwudingding/sync/organization/callback",
"pullCron": "10 0/5 * * * ?",
"forcePullCron": "10 45 12,18 * * ?",
"oapiAddress": "https://oapi.dingtalk.com",
"corpId": "",
"corpSecret": "",
"titleSplit": [
",",
"、",
",",
" ",
" "
],
"personAttributeTitleName": "职务",
"nonce": "o2oa",
"taskToMessage": true,
"taskCompletedToMessage": true,
"readToMessage": true,
"readCompletedToMessage": true
}
JSON参数说明:
appId: String 政务钉钉id
appSecret: String 政务钉钉密钥
agentId: 应用id
syncOrganizationDirection: String人员同步方向,pull,push,disable
syncOrganizationCallbackAddress: String 政务钉钉人员修改回调地址 http://127.0.0.1:20030/x_program_center/jaxrs/zhengwudingding/sync/organization/callback"
pullCron: String 拉入同步定时cron表达式
forcePullCron: String 强制拉入同步cron表达式
oapiAddress: String 政务钉钉api服务器地址
corpId: String 钉钉企业id
corpSecret: String 钉钉企业密钥
titleSplit: [String] 职务分隔符
personAttrubteTitleName: String 政务钉钉职务同步到个人属性名
nonce: String 单点登录(免登)使用的噪音字串
taskToMessage: Boolean 待办推送到政务钉钉消息
taskCompletedToMessage: Boolean 已办推送到政务钉钉消息
readToMessage: Boolean 待阅推送到政务钉钉消息
readCompletedToMessage: Boolean 已阅推送到政务钉钉消息
钉钉消息推送脚本代码:
/********************
resources.getEntityManagerContainer(); //实体管理器
resources.getContext(); //上下文根
resources.getOrganization(); //组织访问
resources.getWebservicesClient();//webSerivces客户端
********************/
var type = 'zhengwuDingdingMessage';
var resp = resources.getContext().applications().getQuery(com.x.base.core.project.x_message_assemble_communicate.class,
'consume/list/' + type + '/count/100');
var messages = resp.getDataAsList(com.x.message.core.entity.Message.class);
for (var i in messages) {
var message = messages[i];
switch (message.getType()) {
case 'task_create':
if (com.x.base.core.project.config.Config.zhengwuDingding().getTaskToMessage()) {
var person = resources.getOrganization().person().getObject(message.getPerson());
var body = JSON.parse(message.getBody());
if (body) {
if (person) {
if (person.getZhengwuDingdingId()) {
var txt = '您有新的待办需要处理:' + body['title'] + '.';
send('184707353', person.getZhengwuDingdingId(), txt);
print('发送政务钉钉待办消息,通知对象: ' + message.getPerson() + '(' + person.getZhengwuDingdingId() + '), 消息:' + txt + '.');
} else {
print('发送政务钉钉待办消息失败,通知对象' + message.getPerson() + ' 无法获取政务钉钉id.');
}
} else {
print('发送政务钉钉待办消息失败,通知对象' + message.getPerson() + ' 不存在.');
}
} else {
print('发送政务钉钉待办消息失败,通知对象' + message.getPerson() + ' 无法获取消息对象.');
}
}
break;
case 'taskCompleted_create':
if (com.x.base.core.project.config.Config.zhengwuDingding().getTaskCompletedToMessage()) {
var person = resources.getOrganization().person().getObject(message.getPerson());
var body = JSON.parse(message.getBody());
if (body) {
if (person) {
if (person.getZhengwuDingdingId()) {
var txt = '您的待办已经处理完成:' + body['title'] + '.';
send('184707353', person.getZhengwuDingdingId(), txt);
print('发送政务钉钉已办消息,通知对象: ' + message.getPerson() + '(' + person.getZhengwuDingdingId() + '), 消息:' + txt + '.');
} else {
print('发送政务钉钉已办消息失败,通知对象' + message.getPerson() + ' 无法获取政务钉钉id.');
}
} else {
print('发送政务钉钉已办消息失败,通知对象' + message.getPerson() + ' 不存在.');
}
} else {
print('发送政务钉钉待办消息失败,通知对象' + message.getPerson() + ' 无法获取消息对象.');
}
}
break;
case 'read_create':
if (com.x.base.core.project.config.Config.zhengwuDingding().getReadToMessage()) {
var person = resources.getOrganization().person().getObject(message.getPerson());
var body = JSON.parse(message.getBody());
if (body) {
if (person) {
if (person.getZhengwuDingdingId()) {
var txt = '您有新的待阅需要处理:' + body['title'] + '.';
send('184707353', person.getZhengwuDingdingId(), txt);
print('发送政务钉钉待阅消息,通知对象: ' + message.getPerson() + '(' + person.getZhengwuDingdingId() + '), 消息:' + txt + '.');
} else {
print('发送政务钉钉待阅消息失败,通知对象' + message.getPerson() + ' 无法获取政务钉钉id.');
}
} else {
print('发送政务钉钉待阅消息失败,通知对象' + message.getPerson() + ' 不存在.');
}
} else {
print('发送政务钉钉待办消息失败,通知对象' + message.getPerson() + ' 无法获取消息对象.');
}
}
break;
case 'readCompleted_create':
if (com.x.base.core.project.config.Config.zhengwuDingding().getReadCompletedToMessage()) {
var person = resources.getOrganization().person().getObject(message.getPerson());
var body = JSON.parse(message.getBody());
if (body) {
if (person) {
if (person.getZhengwuDingdingId()) {
var txt = '您的待阅已经处理完成:' + body['title'] + '.';
send('184707353', person.getZhengwuDingdingId(), txt);
print('发送政务钉钉已阅消息,通知对象: ' + message.getPerson() + '(' + person.getZhengwuDingdingId() + '), 消息:' + txt + '.');
} else {
print('发送政务钉钉已阅消息失败,通知对象' + message.getPerson() + ' 无法获取政务钉钉id.');
}
} else {
print('发送政务钉钉已阅消息失败,通知对象' + message.getPerson() + ' 不存在.');
}
} else {
print('发送政务钉钉待办消息失败,通知对象' + message.getPerson() + ' 无法获取消息对象.');
}
}
break;
default:
system.print('未知的消息类型: ' + message.getType() + ' , title: ' + message.getTitle() + '.');
break;
}
consume(message.getId(), type);
}
function send(agentId, user, message) {
var body = '{"agentId":"184707353","touser":"' + '10001461928' + '","toparty": "","msgtype":"text","context":"' + message + '"}';
var address = com.x.base.core.project.config.Config.zhengwuDingding().getOapiAddress() + "/ent_message/send?access_token=" + com.x.base.core.project.config.Config.zhengwuDingding().appAccessToken();
com.x.base.core.project.connection.HttpConnection.postAsString(address, null, body);
}
function consume(id, type) {
resources.getContext().applications().getQuery(com.x.base.core.project.x_message_assemble_communicate.class,
'consume/' + id + '/type/' + type);
}