33 changed files with 952 additions and 84 deletions
@ -0,0 +1,51 @@ |
|||
package com.ccsens.tall.aspect; |
|||
|
|||
import com.ccsens.cloudutil.annotation.MustLogin; |
|||
import com.ccsens.tall.service.IAsyncService; |
|||
import com.ccsens.tall.service.IRobotService; |
|||
import com.ccsens.tall.service.RobotService; |
|||
import com.ccsens.tall.util.RobotUtil; |
|||
import com.ccsens.util.annotation.OperateType; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.aspectj.lang.JoinPoint; |
|||
import org.aspectj.lang.ProceedingJoinPoint; |
|||
import org.aspectj.lang.Signature; |
|||
import org.aspectj.lang.annotation.*; |
|||
import org.aspectj.lang.reflect.MethodSignature; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.context.annotation.Lazy; |
|||
import org.springframework.core.annotation.Order; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.lang.reflect.Method; |
|||
|
|||
@Order(2) |
|||
@Slf4j |
|||
@Aspect |
|||
@Component |
|||
public class RobotAspect { |
|||
@Autowired |
|||
private IRobotService robotService; |
|||
@Resource |
|||
private IAsyncService asyncService; |
|||
|
|||
@Pointcut("@annotation(com.ccsens.util.annotation.OperateType)") |
|||
public void robotAdvice(){ |
|||
|
|||
} |
|||
|
|||
@After("robotAdvice()") |
|||
public void robotMessageSend(JoinPoint joinPoint){ |
|||
//1.获取方法类型Code
|
|||
Signature signature = joinPoint.getSignature(); |
|||
MethodSignature methodSignature = (MethodSignature) signature; |
|||
Method targetMethod = methodSignature.getMethod(); |
|||
OperateType operateType = targetMethod.getAnnotation(OperateType.class); |
|||
//2.获取发送参数
|
|||
RobotUtil.Message message = RobotUtil.get(); |
|||
asyncService.sendRobotMessage(operateType,message); |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,8 @@ |
|||
package com.ccsens.tall.persist.dao; |
|||
|
|||
import com.ccsens.tall.persist.mapper.SysMessageTypeMapper; |
|||
import org.springframework.stereotype.Repository; |
|||
|
|||
@Repository |
|||
public interface SysMessageTypeDao extends SysMessageTypeMapper { |
|||
} |
@ -0,0 +1,8 @@ |
|||
package com.ccsens.tall.persist.dao; |
|||
|
|||
import com.ccsens.tall.persist.mapper.SysProjectRobotMapper; |
|||
import org.springframework.stereotype.Repository; |
|||
|
|||
@Repository |
|||
public interface SysProjectRobotDao extends SysProjectRobotMapper { |
|||
} |
@ -0,0 +1,8 @@ |
|||
package com.ccsens.tall.persist.dao; |
|||
|
|||
import com.ccsens.tall.persist.mapper.SysProjectRobotMessageMapper; |
|||
import org.springframework.stereotype.Repository; |
|||
|
|||
@Repository |
|||
public interface SysProjectRobotMessageDao extends SysProjectRobotMessageMapper { |
|||
} |
@ -0,0 +1,28 @@ |
|||
package com.ccsens.tall.service; |
|||
|
|||
import com.ccsens.tall.util.RobotUtil; |
|||
import com.ccsens.util.annotation.OperateType; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.scheduling.annotation.Async; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Propagation; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import javax.annotation.Resource; |
|||
|
|||
@Slf4j |
|||
@Async |
|||
@Service |
|||
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) |
|||
public class AsyncService implements IAsyncService{ |
|||
|
|||
@Resource |
|||
private IRobotService robotService; |
|||
|
|||
|
|||
@Override |
|||
public void sendRobotMessage(OperateType operateType,RobotUtil.Message message) { |
|||
robotService.robotMessage(operateType,message); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,11 @@ |
|||
package com.ccsens.tall.service; |
|||
|
|||
import com.ccsens.tall.util.RobotUtil; |
|||
import com.ccsens.util.annotation.OperateType; |
|||
|
|||
/** |
|||
* 异步方法 |
|||
*/ |
|||
public interface IAsyncService { |
|||
void sendRobotMessage(OperateType operateType, RobotUtil.Message message); |
|||
} |
@ -0,0 +1,72 @@ |
|||
package com.ccsens.tall.service; |
|||
|
|||
import com.ccsens.tall.bean.po.ProTaskDetail; |
|||
import com.ccsens.tall.bean.po.SysProject; |
|||
import com.ccsens.tall.bean.vo.TaskVo; |
|||
import com.ccsens.tall.util.RobotUtil; |
|||
import com.ccsens.util.annotation.OperateType; |
|||
|
|||
import java.util.Set; |
|||
|
|||
public interface IRobotService { |
|||
void robotMessage(OperateType operateType, RobotUtil.Message message); |
|||
|
|||
/** |
|||
* 完成任务消息 |
|||
*/ |
|||
void finishTaskRobotSend(Long currentUserId,Long projectId, String projectName, String name,Long executorRoleId ,int completedStatus)throws Exception; |
|||
|
|||
/** |
|||
* 添加任务消息 |
|||
* @param currentUserId |
|||
* @param project |
|||
* @param taskName |
|||
* @param executorId |
|||
*/ |
|||
void addTaskRobotSend(Long currentUserId, SysProject project, String taskName, Long executorId) throws Exception; |
|||
|
|||
/** |
|||
* 删除任务消息 |
|||
* @param currentUserId |
|||
* @param taskDetail |
|||
*/ |
|||
void deleteTaskRobotSend(Long currentUserId, ProTaskDetail taskDetail) throws Exception; |
|||
|
|||
/** |
|||
* 修改任务信息 |
|||
* @param currentUserId |
|||
* @param normalTask |
|||
*/ |
|||
void changeTaskRobotSend(Long currentUserId, TaskVo.NormalTask normalTask) throws Exception; |
|||
|
|||
/** |
|||
* 上传交付物信息 |
|||
* @param currentUserId |
|||
* @param subTimeId |
|||
*/ |
|||
void addDeliverRobotSend(Long currentUserId,String deliverName, Long subTimeId) throws Exception; |
|||
|
|||
/** |
|||
* 删除交付物信息 |
|||
* @param currentUserId |
|||
* @param name |
|||
* @param subTimeId |
|||
*/ |
|||
void deleteDeliverRobotSend(Long currentUserId, String name, Long subTimeId) throws Exception; |
|||
|
|||
/** |
|||
* 检查交付物信息 |
|||
* @param currentUserId |
|||
* @param task |
|||
* @param userIdSet |
|||
* @param name |
|||
*/ |
|||
void checkDeliverRobotSend(Long currentUserId, ProTaskDetail task, Long userIdSet, String name) throws Exception; |
|||
|
|||
/** |
|||
* 评论任务信息 |
|||
* @param userId |
|||
* @param proTaskDetail |
|||
*/ |
|||
void addCommentRobotSend(Long userId, ProTaskDetail proTaskDetail) throws Exception; |
|||
} |
@ -0,0 +1,316 @@ |
|||
package com.ccsens.tall.service; |
|||
|
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import cn.hutool.core.util.ObjectUtil; |
|||
import cn.hutool.core.util.StrUtil; |
|||
import com.ccsens.tall.bean.po.*; |
|||
import com.ccsens.tall.bean.vo.TaskVo; |
|||
import com.ccsens.tall.persist.dao.*; |
|||
import com.ccsens.tall.util.RobotUtil; |
|||
import com.ccsens.tall.util.TallConstant; |
|||
import com.ccsens.util.CodeEnum; |
|||
import com.ccsens.util.RedisUtil; |
|||
import com.ccsens.util.annotation.OperateType; |
|||
import com.ccsens.util.exception.BaseException; |
|||
import com.ccsens.util.wx.WxRobotUtil; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Propagation; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import java.util.List; |
|||
import java.util.concurrent.atomic.AtomicBoolean; |
|||
import java.util.concurrent.atomic.AtomicInteger; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) |
|||
public class RobotService implements IRobotService{ |
|||
@Autowired |
|||
private SysProjectDao sysProjectDao; |
|||
@Autowired |
|||
private SysRobotDao sysRobotDao; |
|||
@Autowired |
|||
private SysProjectRobotDao sysProjectRobotDao; |
|||
@Autowired |
|||
private SysMessageTypeDao sysMessageTypeDao; |
|||
@Autowired |
|||
private SysProjectRobotMessageDao projectRobotMessageDao; |
|||
@Autowired |
|||
private RedisUtil redisUtil; |
|||
@Autowired |
|||
private IUserService userService; |
|||
@Autowired |
|||
private IProRoleService proRoleService; |
|||
@Autowired |
|||
private IProMemberService proMemberService; |
|||
@Autowired |
|||
private TaskDetailDao taskDetailDao; |
|||
@Autowired |
|||
private TaskSubTimeDao taskSubTimeDao; |
|||
|
|||
@Override |
|||
public void robotMessage(OperateType operateType,RobotUtil.Message message){ |
|||
//获取机器人信息
|
|||
SysProjectRobotExample sysProjectRobotExample = new SysProjectRobotExample(); |
|||
sysProjectRobotExample.createCriteria().andProjectIdEqualTo( message.getProjectId()); |
|||
List<SysProjectRobot> sysProjectRobotList = sysProjectRobotDao.selectByExample(sysProjectRobotExample); |
|||
if(CollectionUtil.isNotEmpty(sysProjectRobotList)){ |
|||
sysProjectRobotList.forEach(sysProjectRobot -> { |
|||
//检查接口是否需要发送消息
|
|||
if(isSend(sysProjectRobot.getId(),operateType.value())){ |
|||
String content = getRobotTemplate(operateType.value()); |
|||
//获取发送参数
|
|||
StringBuilder builder = new StringBuilder(content); |
|||
String replace = "{{}}"; |
|||
message.getParams().forEach(param->{ |
|||
int start = builder.indexOf(replace); |
|||
builder.replace(start, start + replace.length(),param); |
|||
}); |
|||
|
|||
//获取机器人信息
|
|||
SysRobot sysRobot = sysRobotDao.selectByPrimaryKey(sysProjectRobot.getRobotId()); |
|||
//4.发送消息
|
|||
if(ObjectUtil.isNotNull(sysRobot)){ |
|||
switch (sysRobot.getClientType()){ |
|||
case 0://微信
|
|||
try { |
|||
WxRobotUtil.sendRobotInfo(sysRobot.getWebHook(),builder,message.getMsgType(),message.getMentionedList(),message.getMentionedMobileList()); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
//删除线程
|
|||
RobotUtil.del(); |
|||
break; |
|||
case 1://钉钉
|
|||
//删除线程
|
|||
RobotUtil.del(); |
|||
break; |
|||
default: |
|||
//删除线程
|
|||
RobotUtil.del(); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
private boolean isSend(Long projectRobotId,int operateType){ |
|||
AtomicBoolean flag = new AtomicBoolean(false); |
|||
SysProjectRobotMessageExample messageExample = new SysProjectRobotMessageExample(); |
|||
messageExample.createCriteria().andProjectRobotIdEqualTo(projectRobotId); |
|||
List<SysProjectRobotMessage> sysProjectRobotMessageList = projectRobotMessageDao.selectByExample(messageExample); |
|||
if(CollectionUtil.isNotEmpty(sysProjectRobotMessageList)){ |
|||
sysProjectRobotMessageList.forEach(projectRobotMessage->{ |
|||
if(projectRobotMessage.getAllMessage() == 1 || projectRobotMessage.getOperateType() == operateType){ |
|||
flag.set(true); |
|||
} |
|||
}); |
|||
} |
|||
return flag.get(); |
|||
} |
|||
|
|||
private String getRobotTemplate(int code){ |
|||
String robotKey = TallConstant.getRobotTemplateKey(code); |
|||
String template = ""; |
|||
template = (String)redisUtil.get(robotKey); |
|||
if(StrUtil.isEmpty(template)){ |
|||
SysMessageTypeExample sysMessageTypeExample = new SysMessageTypeExample(); |
|||
sysMessageTypeExample.createCriteria().andOperateTypeEqualTo((byte) code); |
|||
List<SysMessageType> sysMessageTypeList = sysMessageTypeDao.selectByExample(sysMessageTypeExample); |
|||
if(CollectionUtil.isNotEmpty(sysMessageTypeList)){ |
|||
template = sysMessageTypeList.get(0).getTemplate(); |
|||
redisUtil.set(robotKey,template); |
|||
} |
|||
} |
|||
return template; |
|||
} |
|||
|
|||
private String[] getMemberPhonesByRoleId(Long roleId) throws Exception { |
|||
String[] memberPhone = new String[]{}; |
|||
List<ProMember> memberList = proMemberService.selectByRole(roleId); |
|||
if(CollectionUtil.isNotEmpty(memberList)){ |
|||
String[] phones = new String[memberList.size()]; |
|||
AtomicInteger i = new AtomicInteger(0); |
|||
memberList.forEach(proMember -> { |
|||
if(StrUtil.isNotEmpty(proMember.getPhone())) { |
|||
phones[i.get()] = proMember.getPhone(); |
|||
i.set(i.get() + 1); |
|||
} |
|||
}); |
|||
memberPhone = phones; |
|||
} |
|||
return memberPhone; |
|||
} |
|||
|
|||
@Override |
|||
public void finishTaskRobotSend(Long currentUserId,Long projectId, String projectName, String taskName,Long executorRoleId , int completedStatus) throws Exception { |
|||
String userName = userService.getUserNameByUserId(currentUserId); |
|||
String isFinish = ""; |
|||
if(completedStatus == 2){ |
|||
isFinish = "完成"; |
|||
}else { |
|||
isFinish = "取消完成"; |
|||
} |
|||
|
|||
RobotUtil.Message message = new RobotUtil.Message(projectId); |
|||
message.appendParams(userName,projectName,isFinish,taskName); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(executorRoleId); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
|
|||
@Override |
|||
public void addTaskRobotSend(Long currentUserId, SysProject project, String taskName, Long executorId) throws Exception { |
|||
String userName = userService.getUserNameByUserId(currentUserId); |
|||
String projectName = ""; |
|||
String executorName = ""; |
|||
TaskVo.RoleCheckList role = proRoleService.selectRoleByCheckOrExecutor(executorId); |
|||
if(ObjectUtil.isNotNull(role)){ |
|||
executorName = role.getName(); |
|||
} |
|||
if(ObjectUtil.isNotNull(project)){ |
|||
projectName = project.getName(); |
|||
} |
|||
RobotUtil.Message message = new RobotUtil.Message(project.getId()); |
|||
message.appendParams(userName,projectName,taskName,executorName); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(executorId); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
|
|||
@Override |
|||
public void deleteTaskRobotSend(Long currentUserId, ProTaskDetail taskDetail) throws Exception { |
|||
SysProject sysProject = sysProjectDao.selectByPrimaryKey(taskDetail.getProjectId()); |
|||
String userName = userService.getUserNameByUserId(currentUserId); |
|||
String projectName = ""; |
|||
if(ObjectUtil.isNotNull(sysProject)){ |
|||
projectName = sysProject.getName(); |
|||
} |
|||
RobotUtil.Message message = new RobotUtil.Message(sysProject.getId()); |
|||
message.appendParams(userName,projectName,taskDetail.getName()); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(taskDetail.getExecutorRole()); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
|
|||
@Override |
|||
public void changeTaskRobotSend(Long currentUserId, TaskVo.NormalTask normalTask) throws Exception { |
|||
String userName = userService.getUserNameByUserId(currentUserId); |
|||
RobotUtil.Message message = new RobotUtil.Message(normalTask.getProjectId()); |
|||
message.appendParams(userName,normalTask.getProjectName(),normalTask.getName(),normalTask.getExecutorRoleName()); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(normalTask.getExecutorRole()); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
|
|||
@Override |
|||
public void addDeliverRobotSend(Long currentUserId, String deliverName, Long subTimeId) throws Exception { |
|||
String userName = userService.getUserNameByUserId(currentUserId); |
|||
String projectName = ""; |
|||
ProTaskSubTime proTaskSubTime = taskSubTimeDao.selectByPrimaryKey(subTimeId); |
|||
if(ObjectUtil.isNull(proTaskSubTime)){ |
|||
throw new BaseException(CodeEnum.NOT_TASK); |
|||
} |
|||
ProTaskDetail taskDetail = taskDetailDao.selectByPrimaryKey(proTaskSubTime.getTaskDetailId()); |
|||
if(ObjectUtil.isNull(taskDetail)){ |
|||
throw new BaseException(CodeEnum.NOT_TASK); |
|||
} |
|||
SysProject project = sysProjectDao.selectByPrimaryKey(taskDetail.getProjectId()); |
|||
if(ObjectUtil.isNotNull(project)){ |
|||
projectName = project.getName(); |
|||
} |
|||
//生成消息
|
|||
RobotUtil.Message message = new RobotUtil.Message(project.getId()); |
|||
message.appendParams(userName,projectName,taskDetail.getName(),deliverName); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(taskDetail.getExecutorRole()); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
|
|||
@Override |
|||
public void deleteDeliverRobotSend(Long currentUserId, String deliverName, Long subTimeId) throws Exception { |
|||
String userName = userService.getUserNameByUserId(currentUserId); |
|||
String projectName = ""; |
|||
ProTaskSubTime proTaskSubTime = taskSubTimeDao.selectByPrimaryKey(subTimeId); |
|||
if(ObjectUtil.isNull(proTaskSubTime)){ |
|||
throw new BaseException(CodeEnum.NOT_TASK); |
|||
} |
|||
ProTaskDetail taskDetail = taskDetailDao.selectByPrimaryKey(proTaskSubTime.getTaskDetailId()); |
|||
if(ObjectUtil.isNull(taskDetail)){ |
|||
throw new BaseException(CodeEnum.NOT_TASK); |
|||
} |
|||
SysProject project = sysProjectDao.selectByPrimaryKey(taskDetail.getProjectId()); |
|||
if(ObjectUtil.isNotNull(project)){ |
|||
projectName = project.getName(); |
|||
} |
|||
//生成消息
|
|||
RobotUtil.Message message = new RobotUtil.Message(project.getId()); |
|||
message.appendParams(userName,projectName,taskDetail.getName(),deliverName); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(taskDetail.getExecutorRole()); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
|
|||
@Override |
|||
public void checkDeliverRobotSend(Long currentUserId, ProTaskDetail task, Long uploadUserId, String deliverName) throws Exception { |
|||
String userName = userService.getUserNameByUserId(currentUserId); |
|||
String uploadUserName = ""; |
|||
if(ObjectUtil.isNotNull(uploadUserId)){ |
|||
uploadUserName = userService.getUserNameByUserId(uploadUserId); |
|||
} |
|||
String projectName = ""; |
|||
SysProject project = sysProjectDao.selectByPrimaryKey(task.getProjectId()); |
|||
if(ObjectUtil.isNotNull(project)){ |
|||
projectName = project.getName(); |
|||
} |
|||
//生成消息
|
|||
RobotUtil.Message message = new RobotUtil.Message(project.getId()); |
|||
message.appendParams(userName,uploadUserName,projectName,task.getName(),deliverName); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(task.getExecutorRole()); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
|
|||
@Override |
|||
public void addCommentRobotSend(Long userId, ProTaskDetail proTaskDetail) throws Exception { |
|||
String userName = userService.getUserNameByUserId(userId); |
|||
String projectName = ""; |
|||
SysProject project = sysProjectDao.selectByPrimaryKey(proTaskDetail.getProjectId()); |
|||
if(ObjectUtil.isNotNull(project)){ |
|||
projectName = project.getName(); |
|||
} |
|||
//生成消息
|
|||
RobotUtil.Message message = new RobotUtil.Message(project.getId()); |
|||
message.appendParams(userName,projectName,proTaskDetail.getName()); |
|||
//获取角色内成员的手机号
|
|||
String[] memberPhone = getMemberPhonesByRoleId(proTaskDetail.getExecutorRole()); |
|||
if(memberPhone.length != 0){ |
|||
message.appendMentionedMobileList(memberPhone); |
|||
} |
|||
RobotUtil.set(message); |
|||
} |
|||
} |
@ -0,0 +1,71 @@ |
|||
package com.ccsens.tall.util; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Arrays; |
|||
import java.util.List; |
|||
|
|||
public class RobotUtil { |
|||
|
|||
@Data |
|||
public static class Message{ |
|||
private String msgType = "text"; |
|||
private List<String> mentionedList = new ArrayList<>(); |
|||
private List<String> mentionedMobileList = new ArrayList<>(); |
|||
private boolean isAtAll = false; |
|||
private Long projectId; |
|||
private List<String> params = new ArrayList<>(); |
|||
|
|||
public Message(){ |
|||
} |
|||
|
|||
public Message(String msgType,Long projectId){ |
|||
this.msgType = msgType; |
|||
this.projectId = projectId; |
|||
} |
|||
|
|||
public Message(Long projectId){ |
|||
this.projectId = projectId; |
|||
} |
|||
|
|||
public void appendMentionedList(String... mentionedList){ |
|||
if(mentionedList == null || mentionedList.length == 0){ |
|||
return; |
|||
} |
|||
this.mentionedList.addAll(Arrays.asList(mentionedList)); |
|||
} |
|||
|
|||
public void appendMentionedMobileList(String... mentionedMobileList){ |
|||
if(mentionedMobileList == null || mentionedMobileList.length == 0){ |
|||
return; |
|||
} |
|||
this.mentionedMobileList.addAll(Arrays.asList(mentionedMobileList)); |
|||
} |
|||
|
|||
public void appendParams(String... params){ |
|||
if(params == null || params.length == 0){ |
|||
return; |
|||
} |
|||
this.params.addAll(Arrays.asList(params)); |
|||
} |
|||
|
|||
} |
|||
|
|||
static final ThreadLocal<Message> robotThreadLocal = new ThreadLocal<Message>(); |
|||
|
|||
public static Message get() { |
|||
return robotThreadLocal.get(); |
|||
} |
|||
|
|||
public static void set(Message message) { |
|||
robotThreadLocal.set(message); |
|||
} |
|||
|
|||
public static void del() { |
|||
robotThreadLocal.remove(); |
|||
} |
|||
|
|||
|
|||
|
|||
} |
@ -0,0 +1,17 @@ |
|||
package com.ccsens.tall.util; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
public class TallConstant { |
|||
|
|||
/** |
|||
* 接口发送的信息模板 |
|||
* @param operateType 接口code |
|||
* @return |
|||
*/ |
|||
public static String getRobotTemplateKey(int operateType) { |
|||
return "operate_type_" + operateType; |
|||
} |
|||
|
|||
} |
@ -1,4 +1,4 @@ |
|||
spring: |
|||
profiles: |
|||
active: test |
|||
include: util-test,common |
|||
active: dev |
|||
include: util-dev,common |
|||
|
@ -0,0 +1,16 @@ |
|||
package com.ccsens.util.annotation; |
|||
|
|||
import java.lang.annotation.*; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
@Documented |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Target(ElementType.METHOD) |
|||
public @interface OperateType { |
|||
public int value(); |
|||
|
|||
|
|||
} |
@ -0,0 +1,111 @@ |
|||
package com.ccsens.util.wx; |
|||
|
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import cn.hutool.core.date.DateUtil; |
|||
import cn.hutool.json.JSONUtil; |
|||
import com.ccsens.util.JacksonUtil; |
|||
import lombok.Data; |
|||
import org.json.JSONObject; |
|||
|
|||
import java.io.*; |
|||
import java.net.URL; |
|||
import java.net.URLConnection; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class WxRobotUtil { |
|||
@Data |
|||
public static class WxRobotVo{ |
|||
private String msgtype; |
|||
private WxRobotText text; |
|||
private WxRobotMarkdown markdown; |
|||
} |
|||
@Data |
|||
public static class WxRobotText{ |
|||
private StringBuilder content; |
|||
private List<String> mentioned_list; |
|||
private List<String> mentioned_mobile_list; |
|||
} |
|||
@Data |
|||
public static class WxRobotMarkdown{ |
|||
private StringBuilder content; |
|||
} |
|||
|
|||
|
|||
public static void sendPost(String url, String param){ |
|||
PrintWriter out = null; |
|||
BufferedReader in = null; |
|||
JSONObject jsonObject = null; |
|||
String result = ""; |
|||
try { |
|||
URL realUrl = new URL(url); |
|||
// 打开和URL之间的连接
|
|||
URLConnection conn = realUrl.openConnection(); |
|||
// 发送POST请求必须设置如下两行
|
|||
conn.setDoOutput(true); |
|||
conn.setDoInput(true); |
|||
// conn.addRequestProperty("Cookie", "stay_login=1 smid=DumpWzWQSaLmKlFY1PgAtURdV_u3W3beoei96zsXkdSABwjVCRrnnNBsnH1wGWI0-VIflgvMaZAfli9H2NGtJg id=EtEWf1XZRLIwk1770NZN047804");//设置获取的cookie
|
|||
// 获取URLConnection对象对应的输出流(设置请求编码为UTF-8)
|
|||
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); |
|||
// 发送请求参数
|
|||
out.print(param); |
|||
// flush输出流的缓冲
|
|||
out.flush(); |
|||
// 获取请求返回数据(设置返回数据编码为UTF-8)
|
|||
in = new BufferedReader( |
|||
new InputStreamReader(conn.getInputStream(), "UTF-8")); |
|||
String line; |
|||
while ((line = in.readLine()) != null) { |
|||
result += line; |
|||
} |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} finally { |
|||
try { |
|||
if (out != null) { |
|||
out.close(); |
|||
} |
|||
if (in != null) { |
|||
in.close(); |
|||
} |
|||
} catch (IOException ex) { |
|||
ex.printStackTrace(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static void sendRobotInfo(String webHook,StringBuilder content,String msgType,List<String> mentionedList,List<String> mentionedMobileList)throws Exception { |
|||
WxRobotVo wxRobotVo = new WxRobotVo(); |
|||
wxRobotVo.setMsgtype(msgType); |
|||
if("text".equalsIgnoreCase(msgType)){ |
|||
WxRobotText wxRobotText = new WxRobotText(); |
|||
wxRobotText.setContent(content); |
|||
if(CollectionUtil.isNotEmpty(mentionedList)){ |
|||
wxRobotText.setMentioned_list(mentionedList); |
|||
} |
|||
if(CollectionUtil.isNotEmpty(mentionedMobileList)){ |
|||
wxRobotText.setMentioned_mobile_list(mentionedMobileList); |
|||
} |
|||
wxRobotVo.setText(wxRobotText); |
|||
}else if("markdown".equalsIgnoreCase(msgType)){ |
|||
WxRobotMarkdown wxRobotMarkdown = new WxRobotMarkdown(); |
|||
wxRobotMarkdown.setContent(content); |
|||
wxRobotVo.setMarkdown(wxRobotMarkdown); |
|||
} |
|||
|
|||
String body = JacksonUtil.beanToJson(wxRobotVo); |
|||
System.out.println(body); |
|||
sendPost(webHook,body); |
|||
} |
|||
public static void main(String[] args) throws Exception { |
|||
// String path = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7fbbd546-1c9b-47aa-bcf8-39d272bac87b";
|
|||
// WxRobotVo wxRobotVo = new WxRobotVo();
|
|||
// WxRobotText wxRobotText = new WxRobotText();
|
|||
// wxRobotText.setContent(DateUtil.today());
|
|||
// wxRobotVo.setMsgtype("text");
|
|||
// wxRobotVo.setText(wxRobotText);
|
|||
// String s = JacksonUtil.beanToJson(wxRobotVo);
|
|||
// System.out.println(s);
|
|||
// sendPost(path,s);
|
|||
} |
|||
} |
Loading…
Reference in new issue