From 5dd48e50a0895a364fab3f065ef5dbcd14f909e9 Mon Sep 17 00:00:00 2001 From: Loki <654612@qq.com> Date: Mon, 20 Jun 2022 15:08:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9farmmsg=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/module/farm/annotation/FarmMsg.java | 4 +- .../module/farm/annotation/FarmMsgAspect.java | 239 +++++++++++------- .../module/farm/annotation/FarmMsgType.java | 20 ++ .../admin/project/ProjectController.java | 7 +- .../controller/admin/task/TaskController.java | 7 +- .../service/WxCpServiceNoticeService.java | 9 + 6 files changed, 179 insertions(+), 107 deletions(-) create mode 100644 zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgType.java create mode 100644 zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/WxCpServiceNoticeService.java diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsg.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsg.java index 8aa2e0a7..c758a9b7 100644 --- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsg.java +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsg.java @@ -11,9 +11,9 @@ import java.lang.annotation.*; @Documented public @interface FarmMsg { - String type() default ""; + FarmMsgType type() default FarmMsgType.NONE; - String operation() default ""; + FarmMsgType operation() default FarmMsgType.NONE; String title() default ""; diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgAspect.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgAspect.java index d8af6c65..1a20159d 100644 --- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgAspect.java +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgAspect.java @@ -17,13 +17,15 @@ import cn.iocoder.yudao.module.farm.dal.dataobject.taskCate.TaskCateDO; import cn.iocoder.yudao.module.farm.dal.mysql.logMsg.LogMsgMapper; import cn.iocoder.yudao.module.farm.dal.mysql.project.ProjectMapper; import cn.iocoder.yudao.module.farm.dal.mysql.taskCate.TaskCateMapper; +import cn.iocoder.yudao.module.farm.service.WxCpServiceNoticeService; import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.bean.message.WxCpMessage; import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.annotation.AfterReturning; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.lang.reflect.Method; @@ -45,117 +47,158 @@ public class FarmMsgAspect { @Resource private ProjectMapper projectMapper; + @Resource + private WxCpServiceNoticeService wxCpServiceNoticeService; + + @Autowired + private WxCpService wxCpService; /** * 定义切入点 @PointCut * 使用了@FarmMsg注解的地方切入 * **/ - @Pointcut("@annotation(cn.iocoder.yudao.module.farm.annotation.FarmMsg)") - public void farmMsgPointCut(){ + @Pointcut("@annotation(farmMsg)") + public void farmMsgPointCut(FarmMsg farmMsg){} + + @Around("farmMsgPointCut(farmMsg)") + public Object doAround(ProceedingJoinPoint pjp,FarmMsg farmMsg) throws Throwable { + Object object = pjp.proceed(); + if (object instanceof CommonResult){ + // 只处理接口正常result + if (farmMsg.type() == FarmMsgType.NONE + || farmMsg.operation() == FarmMsgType.NONE + || ((CommonResult) object).getCode()!=0 ){ + return object; + } - } + // 构造一个企业推送消息 + WxCpMessage message = new WxCpMessage(); + + if (farmMsg.type() == FarmMsgType.PROJECT){ + if (farmMsg.operation() == FarmMsgType.CREATE){ + Long id = (Long) ((CommonResult) object).getData(); + // 发企业微信 + // 拼接全部的参与用户 + message.setToUser(""); + //把主题填充 + message.setTitle(""); + wxCpService.getMessageService().send(message); + } + // 跟新项目 删除项目 更新任务 删除任务。。。 - @Before(value = "farmMsgPointCut()") - public void beforeSaveFarmMsg(JoinPoint joinPoint){ - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - FarmMsg farmMsg = method.getAnnotation(FarmMsg.class); + // 构造一个卡片消息 消息名称 : 回乡农场任务管理更新 , 小标题: xxx 创建了项目,请查看处理。 + // appid 是小程序的appid + // touser 可以传多个。 + // 这里处理完删除注释 + } - if (method.getName().startsWith("get")){ - if (farmMsg.type().equals("任务")){ - }else if (farmMsg.type().equals("项目")){ - } } - + return object; } +// @Before(value = "farmMsgPointCut()") +// public void beforeSaveFarmMsg(JoinPoint joinPoint){ +// MethodSignature signature = (MethodSignature) joinPoint.getSignature(); +// Method method = signature.getMethod(); +// FarmMsg farmMsg = method.getAnnotation(FarmMsg.class); +// +// if (method.getName().startsWith("get")){ +// if (farmMsg.type().equals("任务")){ +// +// }else if (farmMsg.type().equals("项目")){ +// +// } +// } +// +// } + /** * 拦截用户操作日志, 连接点执行完成后记录, 如果连接点异常,则不会执行 * @param jointPoint 切入点 **/ - @AfterReturning(value = "farmMsgPointCut()", returning = "result") - public void afterSaveFarmMsg(JoinPoint jointPoint, Object result){ - //从切入点通过反射获取切入点方法 - MethodSignature signature = (MethodSignature) jointPoint.getSignature(); - //获取切入点的方法 - Method method = signature.getMethod(); - - FarmMsgDTO farmMsgDTO = this.assembleMsg(method, jointPoint.getArgs(), result); - - //插入农场日志信息 - LogMsgDO logMsg = LogMsgDO.builder() - .type(farmMsgDTO.getType()) - .operation(farmMsgDTO.getOperation()) - .title(farmMsgDTO.getTitle()) - .msg(farmMsgDTO.getMsg()) - .userId(getLoginUserId()) - .build(); - logMsgMapper.insert(logMsg); - } - - public FarmMsgDTO assembleMsg(Method method, Object[] objects, Object result){ - String title = ""; - String msg = ""; - - FarmMsg farmMsg = method.getAnnotation(FarmMsg.class); - if (method.getName().startsWith("get")){ - for (Object object : objects) { - log.info("after用户{},{},{},{}",getLoginUserId(), farmMsg.type(), farmMsg.title(), object); - if (farmMsg.type().equals("项目")){ - CommonResult projectDO = (CommonResult) result; - title = projectDO.getData().getName(); - } - if (farmMsg.type().equals("任务")){ - CommonResult taskDO = (CommonResult) result; - ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getData().getProjectId()); - TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getData().getTaskCateId()); - title = project.getName(); - msg = taskCate.getName(); - } - } - } - - if (method.getName().startsWith("update")){ - for (Object object : objects) { - if (farmMsg.type().equals("项目")){ - ProjectUpdateReqVO projectDO = (ProjectUpdateReqVO) object; - title = projectDO.getName(); - } - if (farmMsg.type().equals("任务")){ - TaskUpdateReqVO taskDO = (TaskUpdateReqVO) object; - ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId()); - title = project.getName(); - TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getTaskCateId()); - msg = taskCate.getName(); - } - } - } - - if (method.getName().startsWith("create")){ - for (Object object : objects){ - if (farmMsg.type().equals("项目")){ - ProjectCreateReqVO projectDO = (ProjectCreateReqVO) object; - title = projectDO.getName(); - } - if (farmMsg.type().equals("任务")){ - TaskCreateReqVO taskDO = (TaskCreateReqVO) object; - ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId()); - title = project.getName(); - TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getTaskCateId()); - msg = taskCate.getName(); - } - } - } - - FarmMsgDTO farmMsgDTO = FarmMsgDTO.builder() - .type(farmMsg.type()) - .operation(farmMsg.operation()) - .title(title) - .msg(msg).build(); - return farmMsgDTO; - } +// @AfterReturning(value = "farmMsgPointCut()", returning = "result") +// public void afterSaveFarmMsg(JoinPoint jointPoint, Object result){ +// //从切入点通过反射获取切入点方法 +// MethodSignature signature = (MethodSignature) jointPoint.getSignature(); +// //获取切入点的方法 +// Method method = signature.getMethod(); +// +// FarmMsgDTO farmMsgDTO = this.assembleMsg(method, jointPoint.getArgs(), result); +// +// //插入农场日志信息 +// LogMsgDO logMsg = LogMsgDO.builder() +// .type(farmMsgDTO.getType()) +// .operation(farmMsgDTO.getOperation()) +// .title(farmMsgDTO.getTitle()) +// .msg(farmMsgDTO.getMsg()) +// .userId(getLoginUserId()) +// .build(); +// logMsgMapper.insert(logMsg); +// } + +// public FarmMsgDTO assembleMsg(Method method, Object[] objects, Object result){ +// String title = ""; +// String msg = ""; +// +// FarmMsg farmMsg = method.getAnnotation(FarmMsg.class); +// if (method.getName().startsWith("get")){ +// for (Object object : objects) { +// log.info("after用户{},{},{},{}",getLoginUserId(), farmMsg.type(), farmMsg.title(), object); +// if (farmMsg.type().equals("项目")){ +// CommonResult projectDO = (CommonResult) result; +// title = projectDO.getData().getName(); +// } +// if (farmMsg.type().equals("任务")){ +// CommonResult taskDO = (CommonResult) result; +// ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getData().getProjectId()); +// TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getData().getTaskCateId()); +// title = project.getName(); +// msg = taskCate.getName(); +// } +// } +// } +// +// if (method.getName().startsWith("update")){ +// for (Object object : objects) { +// if (farmMsg.type().equals("项目")){ +// ProjectUpdateReqVO projectDO = (ProjectUpdateReqVO) object; +// title = projectDO.getName(); +// } +// if (farmMsg.type().equals("任务")){ +// TaskUpdateReqVO taskDO = (TaskUpdateReqVO) object; +// ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId()); +// title = project.getName(); +// TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getTaskCateId()); +// msg = taskCate.getName(); +// } +// } +// } +// +// if (method.getName().startsWith("create")){ +// for (Object object : objects){ +// if (farmMsg.type().equals("项目")){ +// ProjectCreateReqVO projectDO = (ProjectCreateReqVO) object; +// title = projectDO.getName(); +// } +// if (farmMsg.type().equals("任务")){ +// TaskCreateReqVO taskDO = (TaskCreateReqVO) object; +// ProjectDO project = projectMapper.selectOne(ProjectDO::getId, taskDO.getProjectId()); +// title = project.getName(); +// TaskCateDO taskCate = taskCateMapper.selectOne(TaskCateDO::getId, taskDO.getTaskCateId()); +// msg = taskCate.getName(); +// } +// } +// } +// +// FarmMsgDTO farmMsgDTO = FarmMsgDTO.builder() +// .type(farmMsg.type()) +// .operation(farmMsg.operation()) +// .title(title) +// .msg(msg).build(); +// return farmMsgDTO; +// } } diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgType.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgType.java new file mode 100644 index 00000000..b51ad2f7 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/annotation/FarmMsgType.java @@ -0,0 +1,20 @@ +package cn.iocoder.yudao.module.farm.annotation; + +import lombok.Getter; + +@Getter +public enum FarmMsgType { + + PROJECT, + TASK, + DISCUSS, + + + CREATE, + UPDATE, + SELECT, + EDIT, + DELETE, + + NONE; +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/project/ProjectController.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/project/ProjectController.java index 6ae46294..44dabb84 100644 --- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/project/ProjectController.java +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/project/ProjectController.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.farm.controller.admin.project; import cn.iocoder.yudao.framework.common.page.PageVO; import cn.iocoder.yudao.module.farm.annotation.FarmMsg; +import cn.iocoder.yudao.module.farm.annotation.FarmMsgType; import cn.iocoder.yudao.module.farm.controller.admin.project.dto.ProjectListDTO; import cn.iocoder.yudao.module.farm.controller.admin.project.dto.ProjectPageDTO; import com.zsw.base.R; @@ -39,7 +40,7 @@ public class ProjectController { @Resource private ProjectService projectService; - @FarmMsg(type = "项目", operation = "创建") + @FarmMsg(type = FarmMsgType.PROJECT, operation = FarmMsgType.CREATE) @PostMapping("/create") @ApiOperation("创建农场项目") @PreAuthorize("@ss.hasPermission('farm:project:create')") @@ -47,7 +48,7 @@ public class ProjectController { return success(projectService.createProject(createReqVO)); } - @FarmMsg(type = "项目", operation = "更新") + @FarmMsg(type = FarmMsgType.PROJECT, operation = FarmMsgType.UPDATE) @PutMapping("/update") @ApiOperation("更新农场项目") @PreAuthorize("@ss.hasPermission('farm:project:update')") @@ -56,6 +57,7 @@ public class ProjectController { return success(true); } + @FarmMsg(type = FarmMsgType.PROJECT,operation = FarmMsgType.DELETE) @DeleteMapping("/delete") @ApiOperation("删除农场项目") @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) @@ -65,7 +67,6 @@ public class ProjectController { return success(true); } - @FarmMsg(type = "项目", operation = "查看") @GetMapping("/get") @ApiOperation("获得农场项目") @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/TaskController.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/TaskController.java index ebb3a71c..6b4b580d 100644 --- a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/TaskController.java +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/task/TaskController.java @@ -42,7 +42,7 @@ public class TaskController { @Resource private TaskService taskService; - @FarmMsg(type = "任务", operation = "创建") +// @FarmMsg(type = "任务", operation = "创建") @PostMapping("/create") @ApiOperation("创建农场项目") @PreAuthorize("@ss.hasPermission('farm:task:create')") @@ -50,7 +50,7 @@ public class TaskController { return success(taskService.createTask(createReqVO)); } - @FarmMsg(type = "任务", operation = "更新") +// @FarmMsg(type = "任务", operation = "更新") @PutMapping("/update") @ApiOperation("更新农场项目") @PreAuthorize("@ss.hasPermission('farm:task:update')") @@ -68,7 +68,7 @@ public class TaskController { return success(true); } - @FarmMsg(type = "任务", operation = "查看") +// @FarmMsg(type = "任务", operation = "查看") @GetMapping("/get") @ApiOperation("获得农场项目") @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) @@ -124,7 +124,6 @@ public class TaskController { } - @FarmMsg(type = "任务", title = "查看任务") @GetMapping("/appGet") @ApiOperation("APP查看农场任务详情") @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/WxCpServiceNoticeService.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/WxCpServiceNoticeService.java new file mode 100644 index 00000000..84431a52 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/WxCpServiceNoticeService.java @@ -0,0 +1,9 @@ +package cn.iocoder.yudao.module.farm.service; + +import org.springframework.stereotype.Service; + +@Service +public class WxCpServiceNoticeService { + + +}