重构监听事件对象

This commit is contained in:
jinqiming 2020-11-06 14:14:01 +08:00
parent f1cc8ef841
commit 7e8209502e
16 changed files with 203 additions and 29 deletions

View File

@ -127,4 +127,12 @@ public class DingtalkCallBackController extends BaseController
{
return toAjax(dingtalkCallBackService.deleteDingtalkCallBackByIds(ids));
}
@RequiresPermissions("system:back:detail")
@GetMapping("/detail/{id}")
public String detail(@PathVariable("id") Long operId, ModelMap mmap)
{
mmap.put("operLog", "");
return prefix + "/detail";
}
}

View File

@ -30,9 +30,9 @@
<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:back:edit">
<i class="fa fa-edit"></i> 修改
</a>
<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:back:remove">
<!-- <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:back:remove">
<i class="fa fa-remove"></i> 删除
</a>
</a>-->
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:back:export">
<i class="fa fa-download"></i> 导出
</a>
@ -55,6 +55,7 @@
updateUrl: prefix + "/edit/{id}",
removeUrl: prefix + "/remove",
exportUrl: prefix + "/export",
detailUrl: prefix + "/detail/{id}",
modalName: "回调事件",
columns: [{
checkbox: true
@ -62,7 +63,7 @@
{
field: 'id',
title: 'ID',
visible: true
visible: false
},
/* {
field: 'token',
@ -88,6 +89,7 @@
var actions = [];
actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
actions.push('<a class="btn btn-warning btn-xs ' + detailFlag + '" href="javascript:void(0)" onclick="$.operate.detail(\'' + row.id + '\')"><i class="fa fa-search"></i>获取失败结果</a>');
return actions.join('');
}
}]

View File

@ -35,7 +35,7 @@
<label class="col-sm-3 control-label">通讯录事件回调:</label>
<div class="col-sm-8" th:with="type=${@dict.getType('address_book')}">
<label th:each="dict : ${type}" class="check-box">
<input name="eventNameList" type="checkbox" th:value="${dict.dictValue}" th:checked="${dict.dictValue}" th:text="${dict.dictLabel}" required>
<input name="eventNameList" type="checkbox" th:value="${dict.dictValue}" th:attr="checked=${dingtalkCallBack.eventNameList.contains(dict.dictValue)?true:false}" th:text="${dict.dictLabel}" required>
</label>
</div>
</div>

View File

@ -108,4 +108,9 @@ public class Constants
* 钉钉企业内部 AGENT_ID
*/
public static final String AGENT_ID="agent.id";
/**
* 通讯录事件type_key
*/
public static final String ADDRESS_BOOK="address_book";
}

View File

@ -7,24 +7,40 @@ package com.snow.common.enums;
* @date 2020/9/18 10:18
*/
public enum DingTalkListenerType {
DEPARTMENT_CREATE(1, "部门创建"),
DEPARTMENT_UPDATE(2, "部门更新"),
DEPARTMENT_DELETED(3, "部门删除"),
DEPARTMENT_CREATE(1, 2,"部门创建"),
USER_CREATED(4,"用户创建"),
DEPARTMENT_UPDATE(2, 2,"部门更新"),
CALL_BACK_REGISTER(10, "回调注册"),
DEPARTMENT_DELETED(3,2,"部门删除"),
USER_CREATED(5,2,"用户创建"),
CALL_BACK_REGISTER(20,10, "回调注册"),
CALL_BACK_UPDATE(21,10, "回调更新"),
CALL_BACK_DELETE(22,10, "回调删除"),
;
/**
* 一级code
*/
private final Integer code;
/**
* 二级code
*/
private final Integer type;
/**
* 描述
*/
private final String info;
DingTalkListenerType(Integer code, String info)
DingTalkListenerType(Integer code, Integer type,String info)
{
this.code = code;
this.info = info;
this.type=type;
}
public Integer getCode()
@ -36,4 +52,9 @@ public enum DingTalkListenerType {
{
return info;
}
public Integer getType() {
return type;
}
}

View File

@ -51,4 +51,18 @@ public class BaseConstantUrl {
* 注册回调
*/
public static final String REGISTER_CALL_BACK="https://oapi.dingtalk.com/call_back/register_call_back";
/**
* 更新回调
*/
public static final String UPDATE_CALL_BACK = "https://oapi.dingtalk.com/call_back/update_call_back";
/**
* 更新回调
*/
public static final String DELETE_CALL_BACK = "https://oapi.dingtalk.com/call_back/delete_call_back";
/**
* 获取回调失败
*/
public static final String CALL_BACK_FAILED_RESULT = "https://oapi.dingtalk.com/call_back/get_call_back_failed_result";
}

View File

@ -1,6 +1,7 @@
package com.snow.dingtalk.listener;
import com.alibaba.fastjson.JSON;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.common.utils.spring.SpringUtils;
import com.snow.dingtalk.service.impl.CallBackServiceImpl;
import com.snow.system.domain.DingtalkCallBack;
@ -17,11 +18,19 @@ import org.springframework.stereotype.Component;
@Component
@Slf4j
public class CallBackService implements ISyncDingTalkInfo {
private CallBackServiceImpl departmentService=SpringUtils.getBean("callBackServiceImpl");
private CallBackServiceImpl callBackServiceImpl=SpringUtils.getBean("callBackServiceImpl");
@Override
public void syncDingTalkInfoEvent(SyncEvent syncEvent) {
log.info("调用创建钉钉注册回调传入的原始参数:{}"+JSON.toJSONString(syncEvent));
departmentService.registerCallBack((DingtalkCallBack) syncEvent.getT());
Enum eventType =(Enum) syncEvent.getT();
if(eventType.equals(DingTalkListenerType.CALL_BACK_REGISTER.getCode())){
callBackServiceImpl.registerCallBack((DingtalkCallBack) syncEvent.getT());
}
else if( eventType.equals(DingTalkListenerType.CALL_BACK_UPDATE.getCode())){
callBackServiceImpl.updateCallBack((DingtalkCallBack) syncEvent.getT());
}
}
}

View File

@ -17,15 +17,15 @@ public class SyncDingTalkInfoFactory {
public ISyncDingTalkInfo getSyncDingTalkService(SyncEvent syncEvent){
Integer eventType = syncEvent.getEventType();
Enum dingTalkEnum = (Enum) syncEvent.getT();
if(eventType.equals(DingTalkListenerType.DEPARTMENT_CREATE.getCode())){
if(dingTalkEnum.equals(DingTalkListenerType.DEPARTMENT_CREATE.getType())){
return new DepartmentCreateEventService();
}
else if(eventType.equals(DingTalkListenerType.USER_CREATED.getCode())){
else if(dingTalkEnum.equals(DingTalkListenerType.USER_CREATED.getType())){
return new UserCreateEventService();
}
else if(eventType.equals(DingTalkListenerType.CALL_BACK_REGISTER.getCode())){
else if(dingTalkEnum.equals(DingTalkListenerType.CALL_BACK_REGISTER.getType())){
return new CallBackService();
}

View File

@ -13,4 +13,21 @@ public interface CallBackService {
* 注册事件
*/
void registerCallBack(DingtalkCallBack dingtalkCallBack);
/**
* 更新事件
* @param dingtalkCallBack
*/
void updateCallBack(DingtalkCallBack dingtalkCallBack);
/**
* 删除事件
* @param dingtalkCallBack
*/
void deleteCallBack(DingtalkCallBack dingtalkCallBack);
/**
* 获取回调失败结果
*/
void getCallBackFailedResult();
}

View File

@ -5,10 +5,8 @@ import cn.hutool.cache.impl.TimedCache;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiCallBackRegisterCallBackRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiCallBackRegisterCallBackResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.snow.common.constant.Constants;
import com.snow.common.utils.StringUtils;
import com.snow.dingtalk.common.BaseConstantUrl;
@ -53,7 +51,65 @@ public class CallBackServiceImpl extends BaseService implements CallBackService
}
}
@Override
public void updateCallBack(DingtalkCallBack dingtalkCallBack) {
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.UPDATE_CALL_BACK);
OapiCallBackUpdateCallBackRequest request = new OapiCallBackUpdateCallBackRequest();
request.setUrl(dingtalkCallBack.getUrl());
request.setAesKey(dingtalkCallBack.getAesKey());
request.setToken(dingtalkCallBack.getToken());
request.setCallBackTag(dingtalkCallBack.getEventNameList());
try {
OapiCallBackUpdateCallBackResponse response = client.execute(request,getCallBackDingTalkToken(dingtalkCallBack));
if(response.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.UPDATE_CALL_BACK,response.getMessage(),"updateCallBack()", JSON.toJSONString(request));
}else {
//记录获取token失败日志
syncDingTalkErrorOperLog(BaseConstantUrl.UPDATE_CALL_BACK,response.getErrmsg(),"updateCallBack()", JSON.toJSONString(request));
}
} catch (ApiException e) {
syncDingTalkErrorOperLog(BaseConstantUrl.UPDATE_CALL_BACK,e.getMessage(),"updateCallBack()", JSON.toJSONString(request));
e.printStackTrace();
}
}
@Override
public void deleteCallBack(DingtalkCallBack dingtalkCallBack) {
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.DELETE_CALL_BACK);
OapiCallBackDeleteCallBackRequest request = new OapiCallBackDeleteCallBackRequest();
request.setHttpMethod("GET");
try {
OapiCallBackDeleteCallBackResponse response = client.execute(request, getDingTalkToken());
if(response.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.DELETE_CALL_BACK,response.getMessage(),"deleteCallBack()", JSON.toJSONString(request));
}else {
//记录获取token失败日志
syncDingTalkErrorOperLog(BaseConstantUrl.DELETE_CALL_BACK,response.getErrmsg(),"deleteCallBack()", JSON.toJSONString(request));
}
} catch (ApiException e) {
syncDingTalkErrorOperLog(BaseConstantUrl.DELETE_CALL_BACK,e.getMessage(),"deleteCallBack()", JSON.toJSONString(request));
e.printStackTrace();
}
}
@Override
public void getCallBackFailedResult() {
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.CALL_BACK_FAILED_RESULT);
OapiCallBackGetCallBackFailedResultRequest request = new OapiCallBackGetCallBackFailedResultRequest();
request.setHttpMethod("GET");
try {
OapiCallBackGetCallBackFailedResultResponse response = client.execute(request, getDingTalkToken());
if(response.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.DELETE_CALL_BACK,response.getMessage(),"getCallBackFailedResult()", JSON.toJSONString(request));
}else {
//记录获取token失败日志
syncDingTalkErrorOperLog(BaseConstantUrl.DELETE_CALL_BACK,response.getErrmsg(),"getCallBackFailedResult()", JSON.toJSONString(request));
}
} catch (ApiException e) {
syncDingTalkErrorOperLog(BaseConstantUrl.DELETE_CALL_BACK,e.getMessage(),"getCallBackFailedResult()", JSON.toJSONString(request));
e.printStackTrace();
}
}
/**
* 获取token
* @return

View File

@ -45,8 +45,13 @@ public class DingtalkCallBack extends BaseEntity
@Excel(name = "回调名称")
private String callBackName;
/**
* 部门事件集合
*/
private List<String> eventNameList;
private Boolean flag=false;
public void setId(Long id)
{
this.id = id;

View File

@ -52,6 +52,8 @@ public class SysDictData extends BaseEntity
@Excel(name = "状态", readConverterExp = "0=正常,1=停用")
private String status;
private Boolean extfalg=true;
public Long getDictCode()
{
return dictCode;

View File

@ -15,11 +15,25 @@ public class SyncEvent<T> extends ApplicationEvent {
private T t;
public SyncEvent(Object source) {
super(source);
}
public SyncEvent(Object source,Integer eventType,T t) {
public SyncEvent(Object source,T t) {
super(source);
this.t=t;
}
public SyncEvent(Object source,Integer eventType) {
super(source);
this.eventType = eventType;
}
public SyncEvent(Integer eventType,Object source,T t) {
super(source);
this.eventType = eventType;
this.t=t;
}

View File

@ -1,12 +1,15 @@
package com.snow.system.service.impl;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.snow.common.constant.Constants;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.common.utils.DateUtils;
import com.snow.common.utils.StringUtils;
import com.snow.system.domain.DingtalkCallBackEvent;
import com.snow.system.domain.SysDictData;
import com.snow.system.event.SyncEvent;
import com.snow.system.mapper.DingtalkCallBackEventMapper;
import org.springframework.beans.BeanUtils;
@ -38,6 +41,7 @@ public class DingtalkCallBackServiceImpl implements IDingtalkCallBackService
private SysDictDataServiceImpl sysDictDataServiceImpl;
@Resource
private ApplicationContext applicationContext;
/**
* 查询回调事件
*
@ -89,13 +93,13 @@ public class DingtalkCallBackServiceImpl implements IDingtalkCallBackService
int i = dingtalkCallBackMapper.insertDingtalkCallBack(dingtalkCallBack);
eventNameList.forEach(t->{
dingtalkCallBackEvent.setEventName(t);
String addressBook = sysDictDataServiceImpl.selectDictLabel("address_book", t);
String addressBook = sysDictDataServiceImpl.selectDictLabel(Constants.ADDRESS_BOOK, t);
dingtalkCallBackEvent.setEventDesc(addressBook);
dingtalkCallBackEvent.setCallBanckId((long)i);
dingtalkCallBackEventMapper.insertDingtalkCallBackEvent(dingtalkCallBackEvent);
});
// 同步到dingding
SyncEvent syncEvent = new SyncEvent(dingtalkCallBack, DingTalkListenerType.CALL_BACK_REGISTER.getCode(), dingtalkCallBack);
SyncEvent syncEvent = new SyncEvent(dingtalkCallBack, DingTalkListenerType.CALL_BACK_REGISTER);
applicationContext.publishEvent(syncEvent);
return 1;
}
@ -107,6 +111,7 @@ public class DingtalkCallBackServiceImpl implements IDingtalkCallBackService
* @return 结果
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int updateDingtalkCallBack(DingtalkCallBack dingtalkCallBack)
{
dingtalkCallBack.setUpdateTime(DateUtils.getNowDate());
@ -116,14 +121,18 @@ public class DingtalkCallBackServiceImpl implements IDingtalkCallBackService
List<String> eventNameList = dingtalkCallBack.getEventNameList();
eventNameList.forEach(t->{
dingtalkCallBackEvent.setEventName(t);
String addressBook = sysDictDataServiceImpl.selectDictLabel("address_book", t);
String addressBook = sysDictDataServiceImpl.selectDictLabel(Constants.ADDRESS_BOOK, t);
dingtalkCallBackEvent.setEventDesc(addressBook);
dingtalkCallBackEvent.setCallBanckId(dingtalkCallBack.getId());
dingtalkCallBackEvent.setCreateTime(DateUtils.getNowDate());
dingtalkCallBackEvent.setCreateBy(dingtalkCallBack.getUpdateBy());
dingtalkCallBackEventMapper.insertDingtalkCallBackEvent(dingtalkCallBackEvent);
});
return dingtalkCallBackMapper.updateDingtalkCallBack(dingtalkCallBack);
int i = dingtalkCallBackMapper.updateDingtalkCallBack(dingtalkCallBack);
// 同步到dingding
SyncEvent syncEvent = new SyncEvent(dingtalkCallBack, DingTalkListenerType.CALL_BACK_UPDATE);
applicationContext.publishEvent(syncEvent);
return i;
}
/**
@ -135,7 +144,15 @@ public class DingtalkCallBackServiceImpl implements IDingtalkCallBackService
@Override
public int deleteDingtalkCallBackByIds(String ids)
{
return dingtalkCallBackMapper.deleteDingtalkCallBackByIds(Convert.toStrArray(ids));
List<String> idList = Arrays.asList(Convert.toStrArray(ids));
idList.forEach(t->{
dingtalkCallBackEventMapper.deleteDingtalkCallBackEventByCallBanckId(Integer.parseInt(t));
});
int i = dingtalkCallBackMapper.deleteDingtalkCallBackByIds(Convert.toStrArray(ids));
// 同步到dingding
SyncEvent syncEvent = new SyncEvent(idList, DingTalkListenerType.CALL_BACK_DELETE);
applicationContext.publishEvent(syncEvent);
return i;
}
/**
@ -149,4 +166,5 @@ public class DingtalkCallBackServiceImpl implements IDingtalkCallBackService
{
return dingtalkCallBackMapper.deleteDingtalkCallBackById(id);
}
}

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.system.domain.SysDept;
import com.snow.system.event.SyncEvent;
import org.apache.commons.lang3.ArrayUtils;
@ -211,7 +212,8 @@ public class SysDeptServiceImpl implements ISysDeptService
throw new BusinessException("部门停用,不允许新增");
}
dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
SyncEvent syncEvent = new SyncEvent(dept, 1, dept);
//同步钉钉数据
SyncEvent syncEvent = new SyncEvent(dept, DingTalkListenerType.DEPARTMENT_CREATE);
applicationContext.publishEvent(syncEvent);
return deptMapper.insertDept(dept);
}

View File

@ -3,6 +3,7 @@ package com.snow.system.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.system.domain.SysUserPost;
import com.snow.system.domain.SysUserRole;
import com.snow.system.event.SyncEvent;
@ -209,7 +210,7 @@ public class SysUserServiceImpl implements ISysUserService
// 新增用户与角色管理
insertUserRole(user.getUserId(), user.getRoleIds());
//同步用户数据
SyncEvent syncEvent = new SyncEvent(user, 4, user);
SyncEvent syncEvent = new SyncEvent(user, DingTalkListenerType.USER_CREATED);
applicationContext.publishEvent(syncEvent);
return rows;
}