提交 fb5969af 编写于 作者: zhouweidong's avatar zhouweidong

钉钉待办集成

上级 263a308e
...@@ -114,8 +114,14 @@ public class DevBootSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -114,8 +114,14 @@ public class DevBootSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/"+downloadpath+"/**").permitAll() .antMatchers("/"+downloadpath+"/**").permitAll()
.antMatchers("/"+uploadpath).permitAll() .antMatchers("/"+uploadpath).permitAll()
.antMatchers("/"+previewpath+"/**").permitAll() .antMatchers("/"+previewpath+"/**").permitAll()
//消息发送请求
.antMatchers("/SendMsg").permitAll() .antMatchers("/SendMsg").permitAll()
.antMatchers("/createMsgTemplate").permitAll() .antMatchers("/createMsgTemplate").permitAll()
.antMatchers("/trade/pagepay").permitAll()
.antMatchers("/dingtalk/sendlinkmsg").permitAll()
.antMatchers("/dingtalk/createworkrecord").permitAll()
.antMatchers("/dingtalk/finishworkrecord").permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
// 防止iframe 造成跨域 // 防止iframe 造成跨域
.and().headers().frameOptions().disable(); .and().headers().frameOptions().disable();
......
package cn.ibizlab.core.extensions.domain; package cn.ibizlab.core.extensions.domain;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
...@@ -12,6 +13,7 @@ import java.util.Map; ...@@ -12,6 +13,7 @@ import java.util.Map;
* 其余类型消息则以模板内容为准。 * 其余类型消息则以模板内容为准。
*/ */
@Data @Data
@Builder
public class Msg { public class Msg {
/** /**
...@@ -41,6 +43,10 @@ public class Msg { ...@@ -41,6 +43,10 @@ public class Msg {
* 消息链接 * 消息链接
*/ */
private String url; private String url;
/**
* 待办标识
*/
private String recordid;
/** /**
* 消息模板参数 * 消息模板参数
*/ */
......
...@@ -24,8 +24,9 @@ import com.dingtalk.api.DefaultDingTalkClient; ...@@ -24,8 +24,9 @@ import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest; import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request; import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiGettokenResponse; import com.dingtalk.api.request.OapiWorkrecordAddRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; import com.dingtalk.api.request.OapiWorkrecordUpdateRequest;
import com.dingtalk.api.response.*;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -46,11 +47,19 @@ public class NotifyCoreService { ...@@ -46,11 +47,19 @@ public class NotifyCoreService {
/** /**
* 钉钉获取token api * 钉钉获取token api
*/ */
private final String dingTalkTokenApi="https://oapi.dingtalk.com/gettoken"; private static final String dingTalkTokenApi="https://oapi.dingtalk.com/gettoken";
/** /**
* 钉钉发送消息api * 钉钉发送消息api
*/ */
private final String dingTalkSendMsgApi="https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"; private static final String dingTalkSendMsgApi="https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
/**
* 钉钉创建待办api
*/
private static final String dingTalkCreateWorkRecordApi="https://oapi.dingtalk.com/topapi/workrecord/add";
/**
* 钉钉完成待办api
*/
private static final String dingTalkFinishWorkRecordApi="https://oapi.dingtalk.com/topapi/workrecord/update";
/** /**
* 微信获取token api * 微信获取token api
*/ */
...@@ -62,15 +71,15 @@ public class NotifyCoreService { ...@@ -62,15 +71,15 @@ public class NotifyCoreService {
/** /**
* 阿里短信服务域名 * 阿里短信服务域名
*/ */
private final String aliSmsDomain="dysmsapi.aliyuncs.com"; private static final String aliSmsDomain="dysmsapi.aliyuncs.com";
/** /**
* 阿里短信服务单处理行为 * 阿里短信服务单处理行为
*/ */
private final String aliSendSmsAction="SendSms"; private static final String aliSendSmsAction="SendSms";
/** /**
* 阿里短信服务单处理行为 * 阿里短信服务单处理行为
*/ */
private final String aliSendSmsVersion="2017-05-25"; private static final String aliSendSmsVersion="2017-05-25";
/** /**
* 消息类型 * 消息类型
...@@ -91,6 +100,17 @@ public class NotifyCoreService { ...@@ -91,6 +100,17 @@ public class NotifyCoreService {
put(64,"dingtalk-msg"); put(64,"dingtalk-msg");
}}; }};
private static DingTalkClient sendMsgClient;
private static DingTalkClient createWorkRecordClient;
private static DingTalkClient finishWorkRecordClient;
private static DingTalkClient getTokenClient;
static {
getTokenClient = new DefaultDingTalkClient(dingTalkTokenApi);
sendMsgClient = new DefaultDingTalkClient(dingTalkSendMsgApi);
createWorkRecordClient=new DefaultDingTalkClient(dingTalkCreateWorkRecordApi);
finishWorkRecordClient = new DefaultDingTalkClient(dingTalkFinishWorkRecordApi);
}
@Autowired @Autowired
@Lazy @Lazy
RestTemplate restTemplate; RestTemplate restTemplate;
...@@ -113,12 +133,11 @@ public class NotifyCoreService { ...@@ -113,12 +133,11 @@ public class NotifyCoreService {
public String getDingTalkToken(MsgOpenAccess openAccess){ public String getDingTalkToken(MsgOpenAccess openAccess){
String accessKey=openAccess.getAccessKey(); String accessKey=openAccess.getAccessKey();
String accessSecret=openAccess.getSecretKey(); String accessSecret=openAccess.getSecretKey();
DingTalkClient client = new DefaultDingTalkClient(dingTalkTokenApi);
OapiGettokenRequest req = new OapiGettokenRequest(); OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey(accessKey); req.setAppkey(accessKey);
req.setAppsecret(accessSecret); req.setAppsecret(accessSecret);
req.setHttpMethod("GET"); req.setHttpMethod("GET");
OapiGettokenResponse rsp = client.execute(req); OapiGettokenResponse rsp = getTokenClient.execute(req);
getResult(rsp.getBody()); getResult(rsp.getBody());
return rsp.getBody(); return rsp.getBody();
} }
...@@ -144,33 +163,18 @@ public class NotifyCoreService { ...@@ -144,33 +163,18 @@ public class NotifyCoreService {
@SneakyThrows @SneakyThrows
private boolean sendDingTalkMsg(Msg msg,String tid){ private boolean sendDingTalkMsg(Msg msg,String tid){
String authUserId=getAuthUserId(msg.getUserids(),MsgType.dingtalk); String authUserId=getAuthUserId(msg.getUserids(),MsgType.dingtalk);
MsgTemplate template=templateService.get(tid); MsgTemplate template=templateService.getById(tid);
MsgOpenAccess openAccess =template.getOpenaccess(); MsgOpenAccess openAccess =template.getOpenaccess();
DingTalkClient client = new DefaultDingTalkClient(dingTalkSendMsgApi);
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request(); OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(Long.parseLong(template.getTemplateId())); req.setAgentId(Long.parseLong(template.getTemplateId()));
req.setUseridList(authUserId); req.setUseridList(authUserId);
OapiMessageCorpconversationAsyncsendV2Request.Msg dingtalkMsg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); OapiMessageCorpconversationAsyncsendV2Request.Msg dingtalkMsg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
if((!StringUtils.isEmpty(template.getTemplateUrl())) || (!StringUtils.isEmpty(msg.getUrl())) ){
//链接消息
dingtalkMsg.setMsgtype("link");
dingtalkMsg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
dingtalkMsg.getLink().setTitle(StringUtils.isEmpty(msg.getTitle())?template.getTemplateName():msg.getTitle());
dingtalkMsg.getLink().setText(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent());
dingtalkMsg.getLink().setMessageUrl(StringUtils.isEmpty(msg.getUrl())?template.getTemplateUrl():msg.getUrl());
dingtalkMsg.getLink().setPicUrl("test");
req.setMsg(dingtalkMsg);
}else{
//文本消息
dingtalkMsg.setMsgtype("text"); dingtalkMsg.setMsgtype("text");
OapiMessageCorpconversationAsyncsendV2Request.Text obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Text(); OapiMessageCorpconversationAsyncsendV2Request.Text obj2 = new OapiMessageCorpconversationAsyncsendV2Request.Text();
obj2.setContent(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent()); obj2.setContent(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent());
dingtalkMsg.setText(obj2); dingtalkMsg.setText(obj2);
req.setMsg(dingtalkMsg); req.setMsg(dingtalkMsg);
} OapiMessageCorpconversationAsyncsendV2Response rsp = sendMsgClient.execute(req, openAccess.getAccessToken());
OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, openAccess.getAccessToken());
getResult(rsp.getBody()); getResult(rsp.getBody());
return true; return true;
} }
...@@ -183,7 +187,7 @@ public class NotifyCoreService { ...@@ -183,7 +187,7 @@ public class NotifyCoreService {
@SneakyThrows @SneakyThrows
private boolean sendAliSms(String tid,String userId,Map templParams){ private boolean sendAliSms(String tid,String userId,Map templParams){
String authUserId=getAuthUserId(userId,MsgType.aliyun); String authUserId=getAuthUserId(userId,MsgType.aliyun);
MsgTemplate template=templateService.get(tid); MsgTemplate template=templateService.getById(tid);
MsgOpenAccess openAccess =template.getOpenaccess(); MsgOpenAccess openAccess =template.getOpenaccess();
String region=openAccess.getRegionId(); String region=openAccess.getRegionId();
String accessKey=openAccess.getAccessKey(); String accessKey=openAccess.getAccessKey();
...@@ -215,7 +219,7 @@ public class NotifyCoreService { ...@@ -215,7 +219,7 @@ public class NotifyCoreService {
private boolean sendAliEMail(String tid,String userId){ private boolean sendAliEMail(String tid,String userId){
String authUserId=getAuthUserId(userId,MsgType.aliyun); String authUserId=getAuthUserId(userId,MsgType.aliyun);
MsgTemplate template=templateService.get(tid); MsgTemplate template=templateService.getById(tid);
MsgOpenAccess openAccess =template.getOpenaccess(); MsgOpenAccess openAccess =template.getOpenaccess();
String region=openAccess.getRegionId(); String region=openAccess.getRegionId();
String accessKey=openAccess.getAccessKey(); String accessKey=openAccess.getAccessKey();
...@@ -243,7 +247,7 @@ public class NotifyCoreService { ...@@ -243,7 +247,7 @@ public class NotifyCoreService {
*/ */
private boolean sendWeChatMsg(String tid,String userId,Map templParams){ private boolean sendWeChatMsg(String tid,String userId,Map templParams){
String authUserId=getAuthUserId(userId,MsgType.wechat); String authUserId=getAuthUserId(userId,MsgType.wechat);
MsgTemplate template=templateService.get(tid); MsgTemplate template=templateService.getById(tid);
MsgOpenAccess openAccess =template.getOpenaccess(); MsgOpenAccess openAccess =template.getOpenaccess();
Map paramMap=new HashMap(); Map paramMap=new HashMap();
paramMap.put("template_id",template.getTemplateId()); paramMap.put("template_id",template.getTemplateId());
...@@ -491,4 +495,82 @@ public class NotifyCoreService { ...@@ -491,4 +495,82 @@ public class NotifyCoreService {
return useMsg; return useMsg;
} }
/**
* 发送钉钉链接消息
* @return
*/
@SneakyThrows
public Long sendLinkMsg(Msg msg){
String authUserId=getAuthUserId(msg.getUserids(),MsgType.dingtalk);
MsgTemplate template=templateService.getById(msg.getTemplateid());
if(template==null){
throw new BadRequestAlertException(String.format("发送链接消息失败,无法获取到[%s]对应的rt模板",msg.getTemplateid()),"","");
}
MsgOpenAccess openAccess =template.getOpenaccess();
OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
req.setAgentId(Long.parseLong(template.getTemplateId()));
req.setUseridList(authUserId);
OapiMessageCorpconversationAsyncsendV2Request.Msg dingtalkMsg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
dingtalkMsg.setMsgtype("link");
dingtalkMsg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
dingtalkMsg.getLink().setTitle(StringUtils.isEmpty(msg.getTitle())?template.getTemplateName():msg.getTitle());
dingtalkMsg.getLink().setText(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent());
dingtalkMsg.getLink().setMessageUrl(StringUtils.isEmpty(msg.getUrl())?template.getTemplateUrl():msg.getUrl());
dingtalkMsg.getLink().setPicUrl("test");
req.setMsg(dingtalkMsg);
OapiMessageCorpconversationAsyncsendV2Response rsp = sendMsgClient.execute(req, openAccess.getAccessToken());
getResult(rsp.getBody());
return rsp.getTaskId();
}
/**
* 建立待办
* @param msg
* @return
*/
@SneakyThrows
public String createWorkRecord(Msg msg){
String authUserId=getAuthUserId(msg.getUserids(),MsgType.dingtalk);
MsgTemplate template=templateService.getById(msg.getTemplateid());
if(template==null){
throw new BadRequestAlertException(String.format("创建待办失败,无法获取到[%s]对应的rt模板",msg.getTemplateid()),"","");
}
MsgOpenAccess openAccess =template.getOpenaccess();
OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
req.setUserid(authUserId);
req.setCreateTime(new Date().getTime());
req.setTitle(StringUtils.isEmpty(msg.getTitle())?template.getTemplateName():msg.getTitle());
req.setUrl(StringUtils.isEmpty(msg.getUrl())?template.getTemplateUrl():msg.getUrl());
List<OapiWorkrecordAddRequest.FormItemVo> list2 = new ArrayList<>();
OapiWorkrecordAddRequest.FormItemVo obj3 = new OapiWorkrecordAddRequest.FormItemVo();
list2.add(obj3);
obj3.setTitle(StringUtils.isEmpty(msg.getTitle())?template.getTemplateName():msg.getTitle());
obj3.setContent(StringUtils.isEmpty(msg.getContent())?template.getContent():msg.getContent());
req.setFormItemList(list2);
OapiWorkrecordAddResponse rsp = createWorkRecordClient.execute(req, openAccess.getAccessToken());
getResult(rsp.getBody());
return rsp.getRecordId();
}
/**
* 完成待办
* @param msg
* @return
*/
@SneakyThrows
public boolean finishWorkRecord(Msg msg){
String authUserId=getAuthUserId(msg.getUserids(),MsgType.dingtalk);
MsgTemplate template=templateService.getById(msg.getTemplateid());
if(template==null){
throw new BadRequestAlertException(String.format("完成待办失败,无法获取到[%s]对应的rt模板",msg.getTemplateid()),"","");
}
MsgOpenAccess openAccess =template.getOpenaccess();
OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest();
req.setUserid(authUserId);
req.setRecordId(msg.getRecordid());
OapiWorkrecordUpdateResponse rsp = finishWorkRecordClient.execute(req, openAccess.getAccessToken());
getResult(rsp.getBody());
return rsp.getResult();
}
} }
...@@ -119,8 +119,13 @@ public class apiSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -119,8 +119,13 @@ public class apiSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/"+downloadpath+"/**").permitAll() .antMatchers("/"+downloadpath+"/**").permitAll()
.antMatchers("/"+uploadpath).permitAll() .antMatchers("/"+uploadpath).permitAll()
.antMatchers("/"+previewpath+"/**").permitAll() .antMatchers("/"+previewpath+"/**").permitAll()
//消息发送请求
.antMatchers("/SendMsg").permitAll() .antMatchers("/SendMsg").permitAll()
.antMatchers("/createMsgTemplate").permitAll() .antMatchers("/createMsgTemplate").permitAll()
.antMatchers("/trade/pagepay").permitAll()
.antMatchers("/dingtalk/sendlinkmsg").permitAll()
.antMatchers("/dingtalk/createworkrecord").permitAll()
.antMatchers("/dingtalk/finishworkrecord").permitAll()
// 所有请求都需要认证 // 所有请求都需要认证
.anyRequest().authenticated() .anyRequest().authenticated()
// 防止iframe 造成跨域 // 防止iframe 造成跨域
......
...@@ -15,13 +15,50 @@ public class NotifyCoreResource { ...@@ -15,13 +15,50 @@ public class NotifyCoreResource {
@Autowired @Autowired
NotifyCoreService notifyCoreService; NotifyCoreService notifyCoreService;
/**
* 发送工作流消息
* @param msg
* @return
*/
@RequestMapping(method = RequestMethod.POST,value = "/SendMsg") @RequestMapping(method = RequestMethod.POST,value = "/SendMsg")
public ResponseEntity<Boolean> SendMsg(@Validated @RequestBody Msg msg){ public ResponseEntity<Boolean> SendMsg(@Validated @RequestBody Msg msg){
return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.sendMsg(msg)); return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.sendMsg(msg));
} }
/**
* 建立消息模板
* @param template
* @return
*/
@RequestMapping(method = RequestMethod.POST,value = "/createMsgTemplate") @RequestMapping(method = RequestMethod.POST,value = "/createMsgTemplate")
public ResponseEntity<Boolean> createMsgTemplate(@Validated @RequestBody Template template){ public ResponseEntity<Boolean> createMsgTemplate(@Validated @RequestBody Template template){
return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.createMsgTemplate(template)); return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.createMsgTemplate(template));
} }
/**
* 发送钉钉链接消息
* @param msg
* @return
*/
@RequestMapping(method = RequestMethod.POST,value = "/dingtalk/sendlinkmsg")
public ResponseEntity<Long> sendDingTalkLinkMsg(@RequestBody Msg msg){
return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.sendLinkMsg(msg));
}
/**
* 创建钉钉待办
* @param msg
* @return
*/
@RequestMapping(method = RequestMethod.POST,value = "/dingtalk/createworkrecord")
public ResponseEntity<String> createDingTalkWorkRecord(@RequestBody Msg msg){
return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.createWorkRecord(msg));
}
/**
* 完成钉钉待办
* @param msg
* @return
*/
@RequestMapping(method = RequestMethod.POST,value = "/dingtalk/finishworkrecord")
public ResponseEntity<Boolean> finishDingTalkWorkRecord(@RequestBody Msg msg){
return ResponseEntity.status(HttpStatus.OK).body(notifyCoreService.finishWorkRecord(msg));
}
} }
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册