From 220efe669c7629fec98df97fdc2250f655cf65c3 Mon Sep 17 00:00:00 2001 From: "459816669@qq.com" <459816669@qq.com> Date: Sat, 6 Mar 2021 18:52:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=E5=AE=8C=E7=BB=93=E9=92=89=E9=92=89=E5=92=8C=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-druid.yml | 6 +- .../com/snow/common/constant/Constants.java | 2 + .../common/constant/MessageConstants.java | 29 ++++ .../common/enums/DingTalkListenerType.java | 6 + .../common/enums/DingTalkMessageType.java | 33 ++++ .../com/snow/common/utils/PatternUtils.java | 3 + .../com/snow/common/utils/ServletUtils.java | 3 + .../snow/dingtalk/aspectj/SyncLogAspect.java | 8 +- .../snow/dingtalk/common/BaseConstantUrl.java | 5 +- .../dingtalk/listener/WorkRecodeService.java | 8 + .../model/SendCommonMessageRequest.java | 56 +++++++ .../dingtalk/service/WorkRecodeService.java | 21 +-- .../service/impl/WorkRecodeServiceImpl.java | 93 +++++++++-- .../common/SendMessageEventLister.java | 153 +++++++++++++++++- .../web/domain/common/SysSendMessageDTO.java | 15 +- .../framework/web/service/MailService.java | 5 - 16 files changed, 402 insertions(+), 44 deletions(-) create mode 100644 snow-common/src/main/java/com/snow/common/constant/MessageConstants.java create mode 100644 snow-common/src/main/java/com/snow/common/enums/DingTalkMessageType.java create mode 100644 snow-dingtalk/src/main/java/com/snow/dingtalk/model/SendCommonMessageRequest.java diff --git a/snow-admin/src/main/resources/application-druid.yml b/snow-admin/src/main/resources/application-druid.yml index 199facc..33e163d 100644 --- a/snow-admin/src/main/resources/application-druid.yml +++ b/snow-admin/src/main/resources/application-druid.yml @@ -6,9 +6,9 @@ spring: druid: # 主库数据源 master: - url: jdbc:mysql://localhost:3306/snow?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 - username: root - password: root + url: jdbc:mysql://rm-bp1j1554xv1qs04295o.mysql.rds.aliyuncs.com:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: cloud_root + password: Jin!152377 # 从库数据源 slave: # 从数据源开关/默认关闭 diff --git a/snow-common/src/main/java/com/snow/common/constant/Constants.java b/snow-common/src/main/java/com/snow/common/constant/Constants.java index e920c43..a7780ae 100644 --- a/snow-common/src/main/java/com/snow/common/constant/Constants.java +++ b/snow-common/src/main/java/com/snow/common/constant/Constants.java @@ -136,4 +136,6 @@ public class Constants * 邮件消息类型 */ public static final Integer NEWS_EMAIL_TYPE=2; + + } diff --git a/snow-common/src/main/java/com/snow/common/constant/MessageConstants.java b/snow-common/src/main/java/com/snow/common/constant/MessageConstants.java new file mode 100644 index 0000000..48926b8 --- /dev/null +++ b/snow-common/src/main/java/com/snow/common/constant/MessageConstants.java @@ -0,0 +1,29 @@ +package com.snow.common.constant; + +/** + * @program: snow + * @description + * @author: 没用的阿吉 + * @create: 2021-03-06 16:27 + **/ +public class MessageConstants { + + + + /** + *发起流程通知 + */ + public static final String PROCESS_STARTED_CODE="1367475870205353984"; + + + /** + * 待办消息通知 + */ + public static final String TASK_CREATED_CODE="1365961987292536832"; + + + /** + * 流程完结通知 + */ + public static final String PROCESS_COMPLETED_CODE="1368128438703104000"; +} diff --git a/snow-common/src/main/java/com/snow/common/enums/DingTalkListenerType.java b/snow-common/src/main/java/com/snow/common/enums/DingTalkListenerType.java index c866bc1..f32b7cf 100644 --- a/snow-common/src/main/java/com/snow/common/enums/DingTalkListenerType.java +++ b/snow-common/src/main/java/com/snow/common/enums/DingTalkListenerType.java @@ -26,6 +26,12 @@ public enum DingTalkListenerType { CALL_BACK_FAILED_RESULT(23,10, "获取回调失败结果"), WORK_RECODE_CREATE(1,20, "创建待办"), + + GET_WORK_RECORD_USER(2,20,"获取用户待办"), + + WORK_RECORD_UPDATE(3,20,"更新待办"), + + ASYNCSEND_V2(10,20,"发送消息") ; diff --git a/snow-common/src/main/java/com/snow/common/enums/DingTalkMessageType.java b/snow-common/src/main/java/com/snow/common/enums/DingTalkMessageType.java new file mode 100644 index 0000000..7bebf13 --- /dev/null +++ b/snow-common/src/main/java/com/snow/common/enums/DingTalkMessageType.java @@ -0,0 +1,33 @@ +package com.snow.common.enums; + +/** + * @author qimingjin + * @Title: 交易类型枚举 + * @Description: + * @date 2021/2/26 11:25 + */ +public enum DingTalkMessageType { + TEXT(1, "text"), + LINK(2, "link"); + + private final Integer code; + private final String info; + + DingTalkMessageType(Integer code, String info) + { + this.code = code; + this.info = info; + } + + public Integer getCode() + { + return code; + } + + public String getInfo() + { + return info; + } + + +} diff --git a/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java b/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java index 6e1ce6f..ca24275 100644 --- a/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java +++ b/snow-common/src/main/java/com/snow/common/utils/PatternUtils.java @@ -1,6 +1,7 @@ package com.snow.common.utils; import java.util.Map; +import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -27,6 +28,8 @@ public class PatternUtils { StringBuffer sb = new StringBuffer(); while (m.find()) { + if(null==m) + continue; String group = m.group(1); m.appendReplacement(sb, map.get(group)); } diff --git a/snow-common/src/main/java/com/snow/common/utils/ServletUtils.java b/snow-common/src/main/java/com/snow/common/utils/ServletUtils.java index 1c18158..31bfc56 100644 --- a/snow-common/src/main/java/com/snow/common/utils/ServletUtils.java +++ b/snow-common/src/main/java/com/snow/common/utils/ServletUtils.java @@ -57,6 +57,9 @@ public class ServletUtils */ public static HttpServletRequest getRequest() { + if(getRequestAttributes()==null){ + return null; + } return getRequestAttributes().getRequest(); } diff --git a/snow-dingtalk/src/main/java/com/snow/dingtalk/aspectj/SyncLogAspect.java b/snow-dingtalk/src/main/java/com/snow/dingtalk/aspectj/SyncLogAspect.java index 56cd981..78ce61a 100644 --- a/snow-dingtalk/src/main/java/com/snow/dingtalk/aspectj/SyncLogAspect.java +++ b/snow-dingtalk/src/main/java/com/snow/dingtalk/aspectj/SyncLogAspect.java @@ -23,7 +23,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; +import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; +import java.util.Optional; /** * 操作日志记录处理 @@ -99,8 +101,7 @@ public class SyncLogAspect // 返回参数 sysDingtalkSyncLog.setJsonResult(StringUtils.substring(JSON.marshal(jsonResult), 0, 2000)); - - sysDingtalkSyncLog.setOperUrl(ServletUtils.getRequest().getRequestURI()); + Optional.ofNullable(ServletUtils.getRequest()).ifPresent(t->sysDingtalkSyncLog.setOperUrl(ServletUtils.getRequest().getRequestURI())); if (e != null) { if(e instanceof SyncDataException){ @@ -123,7 +124,8 @@ public class SyncLogAspect sysDingtalkSyncLog.setMethod(className + "." + methodName + "()"); // 设置请求方式 - sysDingtalkSyncLog.setRequestMethod(ServletUtils.getRequest().getMethod()); + Optional.ofNullable(ServletUtils.getRequest()).ifPresent(t->sysDingtalkSyncLog.setRequestMethod(ServletUtils.getRequest().getMethod())); + // 处理设置注解上的参数 getControllerMethodDescription(controllerLog,joinPoint, sysDingtalkSyncLog); // 保存数据库 diff --git a/snow-dingtalk/src/main/java/com/snow/dingtalk/common/BaseConstantUrl.java b/snow-dingtalk/src/main/java/com/snow/dingtalk/common/BaseConstantUrl.java index 64dd5b7..0c9c552 100644 --- a/snow-dingtalk/src/main/java/com/snow/dingtalk/common/BaseConstantUrl.java +++ b/snow-dingtalk/src/main/java/com/snow/dingtalk/common/BaseConstantUrl.java @@ -82,6 +82,9 @@ public class BaseConstantUrl { public static final String CALL_BACK_FAILED_RESULT = "https://oapi.dingtalk.com/call_back/get_call_back_failed_result"; - + /** + * 发送普通消息 + */ + public static final String ASYNCSEND_V2="https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"; } diff --git a/snow-dingtalk/src/main/java/com/snow/dingtalk/listener/WorkRecodeService.java b/snow-dingtalk/src/main/java/com/snow/dingtalk/listener/WorkRecodeService.java index 0491d32..173ccd9 100644 --- a/snow-dingtalk/src/main/java/com/snow/dingtalk/listener/WorkRecodeService.java +++ b/snow-dingtalk/src/main/java/com/snow/dingtalk/listener/WorkRecodeService.java @@ -5,6 +5,7 @@ import com.snow.common.enums.DingTalkListenerType; import com.snow.common.utils.spring.SpringUtils; import com.snow.dingtalk.model.WorkrecordAddRequest; import com.snow.dingtalk.service.impl.WorkRecodeServiceImpl; +import com.snow.framework.web.domain.common.SysSendMessageDTO; import com.snow.system.event.SyncEvent; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -26,9 +27,16 @@ public class WorkRecodeService implements ISyncDingTalkInfo { log.info("调用工作通知传入的原始参数:{}",JSON.toJSONString(syncEvent)); DingTalkListenerType eventType =(DingTalkListenerType) syncEvent.getT(); Integer code = eventType.getCode(); + //钉钉创建待办 if(code.equals(DingTalkListenerType.WORK_RECODE_CREATE.getCode())){ WorkrecordAddRequest workrecordAddRequest=(WorkrecordAddRequest)syncEvent.getSource(); workRecodeService.create(workrecordAddRequest); } + //钉钉发送普通消息 + else if(code.equals(DingTalkListenerType.ASYNCSEND_V2.getCode())){ + + SysSendMessageDTO sysSendMessageDTO=(SysSendMessageDTO)syncEvent.getSource(); + workRecodeService.sendCommonMessage(sysSendMessageDTO); + } } } diff --git a/snow-dingtalk/src/main/java/com/snow/dingtalk/model/SendCommonMessageRequest.java b/snow-dingtalk/src/main/java/com/snow/dingtalk/model/SendCommonMessageRequest.java new file mode 100644 index 0000000..cc0b90d --- /dev/null +++ b/snow-dingtalk/src/main/java/com/snow/dingtalk/model/SendCommonMessageRequest.java @@ -0,0 +1,56 @@ +package com.snow.dingtalk.model; + +import com.snow.common.enums.DingTalkMessageType; +import lombok.Data; + +import java.io.Serializable; + +/** + * @program: snow + * @description 发送消息请求类 + * @author: 没用的阿吉 + * @create: 2021-03-06 15:54 + **/ +@Data +public class SendCommonMessageRequest implements Serializable { + + + /** + * 模板Code + */ + private String templateByCode; + /** + * 消息 + */ + private String msg; + /** + * 是否发给所有人 + */ + private Boolean toAllUser=false; + + /** + * 钉钉用户id + */ + private String userIdList; + + /** + * 消息类型 + */ + private DingTalkMessageType dingTalkMessageType; + + /** + * 消息URL + */ + private String messageUrl; + + /** + * 图片url + */ + private String picUrl; + + + /** + * 标题 + */ + private String title; +} diff --git a/snow-dingtalk/src/main/java/com/snow/dingtalk/service/WorkRecodeService.java b/snow-dingtalk/src/main/java/com/snow/dingtalk/service/WorkRecodeService.java index 6fa0762..622d05d 100644 --- a/snow-dingtalk/src/main/java/com/snow/dingtalk/service/WorkRecodeService.java +++ b/snow-dingtalk/src/main/java/com/snow/dingtalk/service/WorkRecodeService.java @@ -1,22 +1,9 @@ package com.snow.dingtalk.service; -import com.alibaba.fastjson.JSON; -import com.dingtalk.api.DefaultDingTalkClient; -import com.dingtalk.api.DingTalkClient; -import com.dingtalk.api.request.OapiWorkrecordAddRequest; -import com.dingtalk.api.request.OapiWorkrecordGetbyuseridRequest; -import com.dingtalk.api.request.OapiWorkrecordUpdateRequest; -import com.dingtalk.api.response.OapiWorkrecordAddResponse; import com.dingtalk.api.response.OapiWorkrecordGetbyuseridResponse; -import com.dingtalk.api.response.OapiWorkrecordUpdateResponse; -import com.snow.dingtalk.common.BaseConstantUrl; -import com.snow.dingtalk.common.BaseService; import com.snow.dingtalk.model.WorkrecordAddRequest; import com.snow.dingtalk.model.WorkrecordGetbyuseridRequest; -import com.snow.system.service.ISysConfigService; -import com.taobao.api.ApiException; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; +import com.snow.framework.web.domain.common.SysSendMessageDTO; import org.springframework.stereotype.Service; /** @@ -49,4 +36,10 @@ public interface WorkRecodeService { * @return */ Boolean update(String userId,String recordId); + + /** + * 钉钉发送消息 + * @param sysSendMessageDTO + */ + Long sendCommonMessage(SysSendMessageDTO sysSendMessageDTO); } diff --git a/snow-dingtalk/src/main/java/com/snow/dingtalk/service/impl/WorkRecodeServiceImpl.java b/snow-dingtalk/src/main/java/com/snow/dingtalk/service/impl/WorkRecodeServiceImpl.java index db94a10..e9e4562 100644 --- a/snow-dingtalk/src/main/java/com/snow/dingtalk/service/impl/WorkRecodeServiceImpl.java +++ b/snow-dingtalk/src/main/java/com/snow/dingtalk/service/impl/WorkRecodeServiceImpl.java @@ -3,24 +3,34 @@ package com.snow.dingtalk.service.impl; import com.alibaba.fastjson.JSON; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; +import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request; import com.dingtalk.api.request.OapiWorkrecordAddRequest; import com.dingtalk.api.request.OapiWorkrecordGetbyuseridRequest; import com.dingtalk.api.request.OapiWorkrecordUpdateRequest; +import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; import com.dingtalk.api.response.OapiWorkrecordAddResponse; import com.dingtalk.api.response.OapiWorkrecordGetbyuseridResponse; import com.dingtalk.api.response.OapiWorkrecordUpdateResponse; import com.snow.common.annotation.SyncLog; +import com.snow.common.constant.Constants; import com.snow.common.enums.DingTalkListenerType; +import com.snow.common.enums.DingTalkMessageType; import com.snow.common.exception.SyncDataException; +import com.snow.common.utils.PatternUtils; +import com.snow.common.utils.spring.SpringUtils; import com.snow.dingtalk.common.BaseConstantUrl; import com.snow.dingtalk.common.BaseService; import com.snow.dingtalk.model.WorkrecordAddRequest; import com.snow.dingtalk.model.WorkrecordGetbyuseridRequest; -import com.snow.system.service.ISysConfigService; +import com.snow.dingtalk.service.WorkRecodeService; +import com.snow.framework.web.domain.common.SysSendMessageDTO; +import com.snow.system.domain.SysMessageTemplate; +import com.snow.system.service.ISysMessageTemplateService; +import com.snow.system.service.impl.SysConfigServiceImpl; import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @@ -31,13 +41,19 @@ import org.springframework.stereotype.Service; */ @Slf4j @Service -public class WorkRecodeServiceImpl extends BaseService { +public class WorkRecodeServiceImpl extends BaseService implements WorkRecodeService { + private SysConfigServiceImpl isysConfigService=SpringUtils.getBean(SysConfigServiceImpl.class); + + + private ISysMessageTemplateService sysMessageTemplateService=SpringUtils.getBean(ISysMessageTemplateService.class); /** * 创建工作待办 * @param workrecordAddRequest * @return */ + @SyncLog(dingTalkListenerType = DingTalkListenerType.WORK_RECODE_CREATE,dingTalkUrl=BaseConstantUrl.WORK_RECORD_CREATE) + @Override public String create(WorkrecordAddRequest workrecordAddRequest){ DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.WORK_RECORD_CREATE); OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest(); @@ -62,6 +78,8 @@ public class WorkRecodeServiceImpl extends BaseService { * @param workrecordGetbyuseridRequest * @return */ + @Override + @SyncLog(dingTalkListenerType = DingTalkListenerType.GET_WORK_RECORD_USER,dingTalkUrl=BaseConstantUrl.GET_WORK_RECORD_USER_ID_) public OapiWorkrecordGetbyuseridResponse.PageResult getWorkRecordByUserId(WorkrecordGetbyuseridRequest workrecordGetbyuseridRequest){ DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.GET_WORK_RECORD_USER_ID_); OapiWorkrecordGetbyuseridRequest req = new OapiWorkrecordGetbyuseridRequest(); @@ -72,16 +90,14 @@ public class WorkRecodeServiceImpl extends BaseService { try { OapiWorkrecordGetbyuseridResponse rsp = client.execute(req, getDingTalkToken()); if(rsp.getErrcode()==0){ - syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,rsp.getMessage(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req)); return rsp.getRecords(); }else { - syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,rsp.getErrmsg(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req)); + throw new SyncDataException(JSON.toJSONString(req),rsp.getErrmsg()); } } catch (ApiException e) { - e.printStackTrace(); - syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,e.getMessage(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req)); + log.error("钉钉workRecordAddRequest异常:{}",e.getErrMsg()); + throw new SyncDataException(JSON.toJSONString(req),e.getErrMsg()); } - return null; } /** @@ -90,6 +106,8 @@ public class WorkRecodeServiceImpl extends BaseService { * @param recordId * @return */ + @Override + @SyncLog(dingTalkListenerType = DingTalkListenerType.WORK_RECORD_UPDATE,dingTalkUrl=BaseConstantUrl.WORK_RECORD_UPDATE) public Boolean update(String userId,String recordId){ DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.WORK_RECORD_UPDATE); OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest(); @@ -99,15 +117,64 @@ public class WorkRecodeServiceImpl extends BaseService { try { rsp = client.execute(req, getDingTalkToken()); if(rsp.getErrcode()==0){ - syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,rsp.getMessage(),"WorkrecordUpdateRequest",JSON.toJSONString(req)); return rsp.getResult(); }else { - syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,rsp.getErrmsg(),"WorkrecordUpdateRequest",JSON.toJSONString(req)); + throw new SyncDataException(JSON.toJSONString(req),rsp.getErrmsg()); } } catch (ApiException e) { - e.printStackTrace(); - syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,e.getMessage(),"WorkrecordUpdateRequest",JSON.toJSONString(req)); + log.error("钉钉workRecordAddRequest异常:{}",e.getErrMsg()); + throw new SyncDataException(JSON.toJSONString(req),e.getErrMsg()); + } + } + + + @Override + @SyncLog(dingTalkListenerType = DingTalkListenerType.ASYNCSEND_V2,dingTalkUrl=BaseConstantUrl.ASYNCSEND_V2) + public Long sendCommonMessage(SysSendMessageDTO sysSendMessageDTO) { + SysMessageTemplate sysMessageTemplate= sysMessageTemplateService.getSysMessageTemplateByCode(sysSendMessageDTO.getTemplateByCode()); + DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.ASYNCSEND_V2); + OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); + + request.setAgentId(Long.parseLong(isysConfigService.selectConfigByKey(Constants.AGENT_ID))); + + String userIdList = StringUtils.join(sysSendMessageDTO.getReceiverSet(), ","); + request.setUseridList(userIdList); + request.setToAllUser(sysSendMessageDTO.getToAllUser()); + + OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); + + if(sysSendMessageDTO.getDingTalkMessageType().equals(DingTalkMessageType.LINK)){ + msg.setMsgtype(DingTalkMessageType.LINK.getInfo()); + msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link()); + msg.getLink().setTitle(sysMessageTemplate.getTemplateName()); + String content= PatternUtils.builderTemplateBody(sysSendMessageDTO.getParamMap(), sysMessageTemplate.getTemplateBody()); + msg.getLink().setText(content); + msg.getLink().setMessageUrl(sysSendMessageDTO.getFilePath()); + msg.getLink().setPicUrl(sysSendMessageDTO.getFilePath()); + } + + + if(sysSendMessageDTO.getDingTalkMessageType().equals(DingTalkMessageType.TEXT)){ + msg.setMsgtype(DingTalkMessageType.TEXT.getInfo()); + msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text()); + String content= PatternUtils.builderTemplateBody(sysSendMessageDTO.getParamMap(), sysMessageTemplate.getTemplateBody()); + msg.getText().setContent(content); + + } + + request.setMsg(msg); + OapiMessageCorpconversationAsyncsendV2Response rsp = null; + try { + rsp = client.execute(request, getDingTalkToken()); + if(rsp.getErrcode()==0){ + return rsp.getTaskId(); + }else { + throw new SyncDataException(JSON.toJSONString(request),rsp.getErrmsg()); + } + + } catch (ApiException e) { + log.error("钉钉workRecordAddRequest异常:{}",e.getErrMsg()); + throw new SyncDataException(JSON.toJSONString(request),e.getErrMsg()); } - return false; } } diff --git a/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java b/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java index abdfeae..586ff24 100644 --- a/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java +++ b/snow-flowable/src/main/java/com/snow/flowable/listener/common/SendMessageEventLister.java @@ -1,10 +1,14 @@ package com.snow.flowable.listener.common; +import cn.hutool.core.date.BetweenFormater; import cn.hutool.core.date.DateUtil; import cn.hutool.core.thread.ExecutorBuilder; import com.alibaba.fastjson.JSON; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.snow.common.constant.MessageConstants; import com.snow.common.enums.DingTalkListenerType; +import com.snow.common.enums.DingTalkMessageType; import com.snow.common.utils.PatternUtils; import com.snow.dingtalk.model.WorkrecordAddRequest; import com.snow.flowable.common.SpringContextUtil; @@ -22,9 +26,11 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType; +import org.flowable.engine.HistoryService; import org.flowable.engine.RepositoryService; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.delegate.event.FlowableProcessStartedEvent; +import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.impl.persistence.entity.ExecutionEntity; import org.flowable.engine.repository.ProcessDefinition; import org.flowable.engine.runtime.Execution; @@ -61,7 +67,9 @@ public class SendMessageEventLister extends AbstractEventListener { super( new HashSet<>(Arrays.asList( FlowableEngineEventType.TASK_CREATED, - FlowableEngineEventType.PROCESS_STARTED + FlowableEngineEventType.PROCESS_STARTED, + FlowableEngineEventType.PROCESS_COMPLETED, + FlowableEngineEventType.TASK_OWNER_CHANGED )), new HashSet<>(Arrays.asList( FlowDefEnum.SNOW_OA_LEAVE, @@ -84,7 +92,7 @@ public class SendMessageEventLister extends AbstractEventListener { boolean emailOnOff = newsTriggerService.getEmailOnOff(execution.getProcessDefinitionKey(),FlowableEngineEventType.TASK_CREATED.name()); //钉钉通知 if(dingTalkOnOff){ - // sendDingTalkMessage(event); + sendProcessStartedDingTalkMessage(event); } // 邮件通知 if(emailOnOff){ @@ -92,6 +100,28 @@ public class SendMessageEventLister extends AbstractEventListener { } } + /** + * 流程已结束 + * @param event + */ + protected void processCompleted(FlowableEngineEntityEvent event) { + log.info("ManagerTaskEventListener----processCompleted流程创建监听:{}",JSON.toJSONString(event)); + HistoricProcessInstance hisProcessInstance = getHisProcessInstance(event.getProcessInstanceId()); + NewsTriggerService newsTriggerService = (NewsTriggerService)SpringContextUtil.getBean(NewsTriggerService.class); + boolean dingTalkOnOff = newsTriggerService.getDingTalkOnOff(hisProcessInstance.getProcessDefinitionKey(),FlowableEngineEventType.PROCESS_COMPLETED.name()); + boolean emailOnOff = newsTriggerService.getEmailOnOff(hisProcessInstance.getProcessDefinitionKey(),FlowableEngineEventType.PROCESS_COMPLETED.name()); + //钉钉通知 + if(dingTalkOnOff){ + sendProcessCompletedDingTalkMessage(event); + } + // 邮件通知 + if(emailOnOff){ + sendProcessCompletedEmailMessage(event); + } + } + + + /** * 任务创建(待办) * @param event @@ -165,7 +195,7 @@ public class SendMessageEventLister extends AbstractEventListener { map.put("processInstance",processInstance.getProcessDefinitionName()); map.put("url","http://localhost/flow/getMyHistoricProcessInstance"); map.put("datetime",DateUtil.formatDateTime(new Date())); - SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode("1367475870205353984") + SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode(MessageConstants.PROCESS_STARTED_CODE) .receiver(getUserInfo(processInstance.getStartUserId()).getEmail()) .paramMap(map) .build(); @@ -203,7 +233,7 @@ public class SendMessageEventLister extends AbstractEventListener { map.put("processInstance",processDefinition.getName()); map.put("url","http://localhost/flow/findTasksByUserId"); map.put("datetime",DateUtil.formatDateTime(new Date())); - SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode("1365961987292536832") + SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode(MessageConstants.TASK_CREATED_CODE) .receiver(t.getEmail()) .paramMap(map) .build(); @@ -215,6 +245,109 @@ public class SendMessageEventLister extends AbstractEventListener { } } + + + /** + * 发送钉钉通知 + * @param event + */ + public void sendProcessStartedDingTalkMessage(FlowableProcessStartedEvent event){ + + ThreadPoolExecutor executor = ExecutorBuilder.create().setCorePoolSize(5) + .setMaxPoolSize(10) + .setWorkQueue(new LinkedBlockingQueue<>(100)) + .build(); + + executor.execute(() -> { + + ExecutionEntity execution = (ExecutionEntity)event.getEntity(); + ProcessInstance processInstance = execution.getProcessInstance(); + Map map=new HashMap<>(); + map.put("toUser",getUserInfo(processInstance.getStartUserId()).getUserName()); + map.put("starttime",DateUtil.formatDateTime(processInstance.getStartTime())); + map.put("processInstance",processInstance.getProcessDefinitionName()); + map.put("url","http://localhost/flow/getMyHistoricProcessInstance"); + map.put("datetime",DateUtil.formatDateTime(new Date())); + + SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode(MessageConstants.PROCESS_STARTED_CODE) + .receiverSet(Sets.newHashSet(getUserInfo(processInstance.getStartUserId()).getDingUserId())) + .paramMap(map) + .dingTalkMessageType(DingTalkMessageType.TEXT) + .build(); + SyncEvent syncEventGroup = new SyncEvent(sysSendMessageDTO, DingTalkListenerType.ASYNCSEND_V2); + applicationContext.publishEvent(syncEventGroup); + }); + executor.shutdown(); + + } + + /** + * 流程完结发送邮件通知 + * @param event + */ + public void sendProcessCompletedEmailMessage(FlowableEngineEntityEvent event){ + MailService mailService = (MailService) SpringContextUtil.getBean(MailService.class); + ThreadPoolExecutor executor = ExecutorBuilder.create().setCorePoolSize(5) + .setMaxPoolSize(10) + .setWorkQueue(new LinkedBlockingQueue<>(100)) + .build(); + + executor.execute(() -> { + HistoricProcessInstance hisProcessInstance = getHisProcessInstance(event.getProcessInstanceId()); + Map map = buildSendProcessCompletedParam(hisProcessInstance); + SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode(MessageConstants.PROCESS_COMPLETED_CODE) + .receiver(getUserInfo(hisProcessInstance.getStartUserId()).getEmail()) + .paramMap(map) + .build(); + mailService.sendSimpleMail(sysSendMessageDTO); + }); + executor.shutdown(); + + } + + /** + * 流程完结发送钉钉通知 + * @param event + */ + public void sendProcessCompletedDingTalkMessage(FlowableEngineEntityEvent event){ + + ThreadPoolExecutor executor = ExecutorBuilder.create().setCorePoolSize(5) + .setMaxPoolSize(10) + .setWorkQueue(new LinkedBlockingQueue<>(100)) + .build(); + + executor.execute(() -> { + HistoricProcessInstance hisProcessInstance = getHisProcessInstance(event.getProcessInstanceId()); + Map map = buildSendProcessCompletedParam(hisProcessInstance); + + SysSendMessageDTO sysSendMessageDTO = SysSendMessageDTO.builder().templateByCode(MessageConstants.PROCESS_COMPLETED_CODE) + .receiverSet(Sets.newHashSet(getUserInfo(hisProcessInstance.getStartUserId()).getDingUserId())) + .paramMap(map) + .dingTalkMessageType(DingTalkMessageType.TEXT) + .build(); + SyncEvent syncEventGroup = new SyncEvent(sysSendMessageDTO, DingTalkListenerType.ASYNCSEND_V2); + applicationContext.publishEvent(syncEventGroup); + }); + executor.shutdown(); + + } + + /** + * 组建流程完结参数 + * @param hisProcessInstance + */ + private Map buildSendProcessCompletedParam(HistoricProcessInstance hisProcessInstance){ + Map map=new HashMap<>(); + map.put("toUser",getUserInfo(hisProcessInstance.getStartUserId()).getUserName()); + map.put("starttime",DateUtil.formatDateTime(hisProcessInstance.getStartTime())); + map.put("orderNo",hisProcessInstance.getBusinessKey()); + map.put("processInstance",hisProcessInstance.getProcessDefinitionName()); + String spendTime = DateUtil.formatBetween(hisProcessInstance.getStartTime(), new Date(), BetweenFormater.Level.SECOND); + map.put("time",spendTime); + map.put("url","http://localhost/flow/getMyHistoricProcessInstance"); + map.put("datetime",DateUtil.formatDateTime(new Date())); + return map; + } /** * 组装参数 * @param userId 需要发送通知的人 @@ -271,6 +404,18 @@ public class SendMessageEventLister extends AbstractEventListener { .singleResult(); } + /** + * 获取历史流程实例 + * @param processInstanceId + * @return + */ + protected HistoricProcessInstance getHisProcessInstance(String processInstanceId) { + + HistoryService historyService = (HistoryService)SpringContextUtil.getBean(HistoryService.class); + return historyService.createHistoricProcessInstanceQuery() + .processInstanceId(processInstanceId) + .singleResult(); + } /** * 获取用户信息 * @param userId diff --git a/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java b/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java index fbee625..802c41a 100644 --- a/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java +++ b/snow-framework/src/main/java/com/snow/framework/web/domain/common/SysSendMessageDTO.java @@ -1,5 +1,6 @@ package com.snow.framework.web.domain.common; +import com.snow.common.enums.DingTalkMessageType; import lombok.*; import java.io.Serializable; @@ -52,8 +53,20 @@ public class SysSendMessageDTO implements Serializable { private String filePath; /** - * 邮件发送时间 + * 发送时间 */ private Date sentDate; + + /** + * 是否发给所有人 + */ + private Boolean toAllUser=false; + + + /** + * 消息类型 + */ + private DingTalkMessageType dingTalkMessageType; + } diff --git a/snow-framework/src/main/java/com/snow/framework/web/service/MailService.java b/snow-framework/src/main/java/com/snow/framework/web/service/MailService.java index f9f6a53..5359d5a 100644 --- a/snow-framework/src/main/java/com/snow/framework/web/service/MailService.java +++ b/snow-framework/src/main/java/com/snow/framework/web/service/MailService.java @@ -1,6 +1,5 @@ package com.snow.framework.web.service; -import cn.hutool.core.thread.ExecutorBuilder; import com.snow.common.utils.PatternUtils; import com.snow.common.utils.StringUtils; import com.snow.framework.web.domain.common.SysSendMessageDTO; @@ -8,10 +7,8 @@ import com.snow.system.domain.SysMessageTemplate; import com.snow.system.service.ISysMessageTemplateService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; -import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; @@ -22,8 +19,6 @@ import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Set; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; /** * @program: snow