You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.5 KiB

  1. package com.example.demo.RabbitMQ;
  2. import com.example.demo.Util.SecurityUtils;
  3. import com.example.demo.config.RabbitMQConfig;
  4. import com.example.demo.config.interfac.Message;
  5. import com.example.demo.domain.DTO.MessageDTO;
  6. import com.example.demo.mapper.coin.OperationLogMapper;
  7. import com.example.demo.mapper.coin.UserMapper;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.aspectj.lang.ProceedingJoinPoint;
  11. import org.aspectj.lang.annotation.Around;
  12. import org.aspectj.lang.annotation.Aspect;
  13. import org.aspectj.lang.reflect.MethodSignature;
  14. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. /**
  18. * @program: GOLD
  19. * @ClassName CashCollectionAspect
  20. * @description:
  21. * @author: huangqizhen
  22. * @create: 202511-14 19:10
  23. * @Version 1.0
  24. **/
  25. @Aspect
  26. @Component
  27. @Slf4j
  28. public class CashCollectionAspect {
  29. @Autowired
  30. private RabbitTemplate rabbitTemplate;
  31. @Autowired
  32. private UserMapper userMapper;
  33. @Autowired
  34. private OperationLogMapper operationLogMapper;
  35. @Around("@annotation(com.example.demo.config.interfac.Message)")
  36. public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
  37. long startTime = System.currentTimeMillis();
  38. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  39. String methodName = signature.getName();
  40. String className = signature.getDeclaringTypeName();
  41. Object[] args = joinPoint.getArgs();
  42. Message logAnnotation = signature.getMethod().getAnnotation(Message.class);
  43. String action = logAnnotation.value();
  44. // ✅ 使用 Spring Security 获取真实用户
  45. String username = SecurityUtils.getCurrentUsername();
  46. Integer userId = SecurityUtils.getCurrentUserId();
  47. String name = userMapper.selectUserByJwcode(Integer.valueOf(username)).getName();
  48. // 添加空值检查
  49. if (userId == null) {
  50. log.warn("无法获取当前用户ID,使用默认值 -1");
  51. userId = -1; // 或者使用其他默认值
  52. }
  53. ObjectMapper mapper = new ObjectMapper();
  54. String argsJson = "[]";
  55. try {
  56. argsJson = mapper.writeValueAsString(args);
  57. } catch (Exception e) {
  58. argsJson = "serialize failed";
  59. }
  60. Object result;
  61. try {
  62. result = joinPoint.proceed();
  63. } catch (Exception e) {
  64. log.error("方法执行异常: {}", e.getMessage());
  65. throw e;
  66. }
  67. long duration = System.currentTimeMillis() - startTime;
  68. // ✅ 构造日志消息 DTO
  69. MessageDTO messageDTO = new MessageDTO();
  70. messageDTO.setJwcode(Integer.valueOf(username));
  71. messageDTO.setName(name);
  72. messageDTO.setTitle(action);
  73. messageDTO.setDesc(username+"有一条消息需要处理");
  74. // operationLogMapper.insertMessage(messageDTO);
  75. // ✅ 发送消息到 RabbitMQ(不等待)
  76. try {
  77. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_COLLECTION_EXCHANGE, "cash.collection.save", messageDTO);
  78. log.info("📩 日志消息已发送到 RabbitMQ: {}", action);
  79. } catch (Exception e) {
  80. log.error("发送日志消息到 RabbitMQ 失败", e);
  81. }
  82. log.info("✅ AOP 拦截完成: {} 执行 [{}] 耗时 {}ms", username, action, duration);
  83. return result;
  84. }
  85. }