修改readme

This commit is contained in:
jinqiming 2020-09-17 18:04:40 +08:00
parent c33ac9b170
commit 6a940a0bb0
12 changed files with 290 additions and 16 deletions

View File

@ -100,6 +100,7 @@
<artifactId>hutool-all</artifactId>
<version>5.4.2</version>
</dependency>
</dependencies>
</project>

View File

@ -91,4 +91,18 @@ public class Constants
* 资源映射路径 前缀
*/
public static final String RESOURCE_PREFIX = "/profile";
public static final String POST="POST";
public static final String GET="GET";
/**
* 钉钉企业内部APPkey
*/
public static final String ENTERPRICE_APP_KEY="enterpriceAppKey";
/**
* 钉钉企业内部ENTERPRICE_APP_SECRET
*/
public static final String ENTERPRICE_APP_SECRET="enterpriceAppSecret";
}

View File

@ -56,4 +56,12 @@ public enum BusinessType
* 清空
*/
CLEAN,
/**
* 钉钉
*/
DING_TALK,
/**
* 同步
*/
SYNCHRONIZATION
}

View File

@ -24,5 +24,11 @@
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,32 @@
package com.snow.dingtalk;
import com.alibaba.fastjson.JSON;
import com.snow.dingtalk.model.DepartmentDTO;
import com.snow.dingtalk.service.DepartmentService;
import com.snow.system.domain.SysDept;
import com.snow.system.event.SyncEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/9/17 17:40
*/
@Component
public class SyncEventListener implements ApplicationListener<SyncEvent> {
@Autowired
private DepartmentService departmentService;
@Override
public void onApplicationEvent(SyncEvent syncEvent) {
Integer eventType = syncEvent.getEventType();
SysDept sysDept=(SysDept)syncEvent.getT();
DepartmentDTO departmentDTO = DepartmentDTO.builder().name(sysDept.getDeptName()).order(sysDept.getOrderNum())
.parentid(sysDept.getParentName()).build();
departmentService.createDepartment(departmentDTO);
System.out.println("监听到的事件类型:"+eventType+JSON.toJSONString(syncEvent));
}
}

View File

@ -7,6 +7,12 @@ package com.snow.dingtalk.common;
* @date 2020/9/16 11:02
*/
public class BaseConstantUrl {
/**
* 获取token url
*/
public static final String GET_TOKEN_URL="https://oapi.dingtalk.com/gettoken";
/**
* 创建部门
*/
public static final String DEPARTMENT_CREATE="https://oapi.dingtalk.com/department/create";
}

View File

@ -5,31 +5,45 @@ import cn.hutool.cache.impl.TimedCache;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.snow.common.constant.Constants;
import com.snow.common.enums.BusinessType;
import com.snow.common.utils.StringUtils;
import com.snow.system.domain.SysOperLog;
import com.snow.system.service.ISysConfigService;
import com.snow.system.service.ISysOperLogService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/9/16 10:46
*/
@Service
public class BaseService {
public final String TOKEN="token";
public static final String TOKEN="token";
@Autowired
private ISysConfigService isysConfigService;
@Autowired
private ISysOperLogService iSysOperLogService;
/**
* 获取token
* @return
*/
public String getDingTalkToken(){
//创建缓存缓存默认是7100S
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(7100);
if(StringUtils.isEmpty(timedCache.get(TOKEN))){
DefaultDingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.GET_TOKEN_URL);
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey("dingjidrzgnmznpofira");
request.setAppsecret("IuKf2e8Z0stvf8LPMfHy_Im6sTi3G1mh0Jn0k4xuEnOhOyiLfKogwYympDVcRGK4");
request.setHttpMethod("GET");
request.setAppkey(isysConfigService.selectConfigByKey(Constants.ENTERPRICE_APP_KEY));
request.setAppsecret(isysConfigService.selectConfigByKey(Constants.ENTERPRICE_APP_SECRET));
request.setHttpMethod(Constants.GET);
try {
OapiGettokenResponse response = client.execute(request);
if(response.getErrcode()==0){
@ -37,9 +51,11 @@ public class BaseService {
return response.getAccessToken();
}else {
//记录获取token失败日志
syncDingTalkErrorOperLog(BaseConstantUrl.GET_TOKEN_URL,response.getErrmsg(),"getDingTalkToken()");
return null;
}
} catch (ApiException e) {
syncDingTalkErrorOperLog(BaseConstantUrl.GET_TOKEN_URL,e.getMessage(),"getDingTalkToken()");
e.printStackTrace();
}
return null;
@ -49,4 +65,22 @@ public class BaseService {
}
/**
* 记录钉钉异常信息
* @param url
* @param errorMessage
* @param method
*/
public void syncDingTalkErrorOperLog(String url,String errorMessage,String method){
SysOperLog sysOperLog=new SysOperLog();
sysOperLog.setOperTime(new Date());
sysOperLog.setErrorMsg(errorMessage);
sysOperLog.setBusinessType(BusinessType.SYNCHRONIZATION.ordinal());
sysOperLog.setOperName("系统自动记录");
sysOperLog.setOperUrl(url);
sysOperLog.setMethod(method);
sysOperLog.setTitle("系统调用钉钉异常");
iSysOperLogService.insertOperlog(sysOperLog);
}
}

View File

@ -0,0 +1,86 @@
package com.snow.dingtalk.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/9/17 17:10
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DepartmentDTO {
/**
* 部门名称长度限制为1~64个字符不允许包含字符-以及,
*/
private String name;
/**
* 父部门id根部门id为1
*/
private String parentid;
/**
* 在父部门中的排序值order值小的排序靠前
*/
private String order;
/**
* 是否创建一个关联此部门的企业群默认为false
*/
private Boolean createDeptGroup;
/**
*
是否隐藏部门
true表示隐藏
false表示显示
*/
private Boolean deptHiding;
/**
* 可以查看指定隐藏部门的其他部门列表如果部门隐藏则此值生效
* 取值为其他的部门id组成的字符串使用\|符号进行分割总数不能超过200
*/
private String deptPermits;
/**
* 可以查看指定隐藏部门的其他人员列表如果部门隐藏则此值生效
* 取值为其他的人员userid组成的字符串使用\|符号进行分割总数不能超过200
*/
private String userPermits;
/**
* 限制本部门成员查看通讯录限制开启后本部门成员只能看到限定范围内的通讯录true表示限制开启
*/
private Boolean outerDept;
/**
* 部门标识字段开发者可用该字段来唯一标识一个部门并与钉钉外部通讯录里的部门做映射
*/
private String sourceIdentifier;
/**
* 部门自定义字段格式为文本类型的Json格式
*/
private String ext;
}

View File

@ -0,0 +1,50 @@
package com.snow.dingtalk.service;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentCreateRequest;
import com.dingtalk.api.response.OapiDepartmentCreateResponse;
import com.snow.dingtalk.common.BaseConstantUrl;
import com.snow.dingtalk.common.BaseService;
import com.snow.dingtalk.model.DepartmentDTO;
import com.taobao.api.ApiException;
import com.taobao.api.Constants;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/9/17 17:02
*/
@Service
public class DepartmentService extends BaseService {
/**
* 创建部门
* @param departmentDTO
* @return
*/
public Long createDepartment(DepartmentDTO departmentDTO){
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.DEPARTMENT_CREATE);
OapiDepartmentCreateRequest request = new OapiDepartmentCreateRequest();
request.setParentid(departmentDTO.getParentid());
request.setCreateDeptGroup(departmentDTO.getCreateDeptGroup());
request.setOrder(departmentDTO.getOrder());
request.setName(departmentDTO.getName());
request.setHttpMethod(Constants.METHOD_POST);
request.setSourceIdentifier(departmentDTO.getSourceIdentifier());
try {
OapiDepartmentCreateResponse response = client.execute(request,getDingTalkToken());
if(response.getErrcode()==0){
return response.getId();
}else {
syncDingTalkErrorOperLog(BaseConstantUrl.DEPARTMENT_CREATE,response.getErrmsg(),"createDepartment");
}
} catch (ApiException e) {
syncDingTalkErrorOperLog(BaseConstantUrl.DEPARTMENT_CREATE,e.getMessage(),"createDepartment");
e.printStackTrace();
}
return null;
}
}

View File

@ -1,10 +0,0 @@
package com.snow.dingtalk;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/9/16 10:45
*/
public class test {
}

View File

@ -0,0 +1,38 @@
package com.snow.system.event;
import org.springframework.context.ApplicationEvent;
/**
* @author qimingjin
* @Title: 同步事件监听器
* @Description:
* @date 2020/9/17 17:34
*/
public class SyncEvent<T> extends ApplicationEvent {
private Integer eventType;
private T t;
public SyncEvent(Object source,Integer eventType,T t) {
super(source);
this.eventType = eventType;
this.t=t;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public Integer getEventType() {
return eventType;
}
public void setEventType(Integer eventType) {
this.eventType = eventType;
}
}

View File

@ -5,8 +5,10 @@ import java.util.Iterator;
import java.util.List;
import com.snow.system.domain.SysDept;
import com.snow.system.event.SyncEvent;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.snow.common.annotation.DataScope;
@ -18,6 +20,8 @@ import com.snow.system.domain.SysRole;
import com.snow.system.mapper.SysDeptMapper;
import com.snow.system.service.ISysDeptService;
import javax.annotation.Resource;
/**
* 部门管理 服务实现
*
@ -29,6 +33,9 @@ public class SysDeptServiceImpl implements ISysDeptService
@Autowired
private SysDeptMapper deptMapper;
@Resource
private ApplicationContext applicationContext;
/**
* 查询部门管理数据
*
@ -204,6 +211,8 @@ public class SysDeptServiceImpl implements ISysDeptService
throw new BusinessException("部门停用,不允许新增");
}
dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
SyncEvent syncEvent = new SyncEvent(dept, 1, dept);
applicationContext.publishEvent(syncEvent);
return deptMapper.insertDept(dept);
}