qa-prevention-gwj/src/main/java/com/zcloud/aspect/DockingAspect.java

226 lines
10 KiB
Java
Raw Normal View History

2023-12-17 15:44:38 +08:00
package com.zcloud.aspect;
import com.alibaba.fastjson.JSON;
import com.zcloud.dto.TenCorpDto;
import com.zcloud.entity.PageData;
2023-12-22 08:42:29 +08:00
import com.zcloud.entity.system.User;
2023-12-17 15:44:38 +08:00
import com.zcloud.service.docking.DockingRelationService;
2023-12-18 16:58:08 +08:00
import com.zcloud.service.mq.DockSendMessageService;
2023-12-17 15:44:38 +08:00
import com.zcloud.service.mq.SendMessageService;
2023-12-22 08:42:29 +08:00
import com.zcloud.util.Const;
2023-12-17 15:44:38 +08:00
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
2023-12-22 08:42:29 +08:00
import org.apache.shiro.session.Session;
2023-12-17 15:44:38 +08:00
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
2023-12-20 17:16:37 +08:00
import org.springframework.beans.factory.annotation.Value;
2023-12-17 15:44:38 +08:00
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@Component
@Aspect
public class DockingAspect {
@Autowired
2023-12-18 16:58:08 +08:00
private DockSendMessageService sendMessageService;
2023-12-17 15:44:38 +08:00
@Autowired
private DockingRelationService dockingRelationService;
2023-12-20 17:16:37 +08:00
@Value("${baseimgpath}")
public String baseimgpath;
2023-12-17 15:44:38 +08:00
@Pointcut("@annotation(com.zcloud.aspect.DockAnnotation)")
2023-12-20 11:08:33 +08:00
public void pointcut() {
}
2023-12-17 15:44:38 +08:00
public static Map<String, String> dockingRelationMap = new HashMap();
// 增强逻辑:在切点方法执行前打印日志
@Around("pointcut()")
public Object beforeApi(ProceedingJoinPoint joinPoint) throws Throwable {
2023-12-20 11:08:33 +08:00
2023-12-17 15:44:38 +08:00
try {
2023-12-20 11:08:33 +08:00
Object proceed = joinPoint.proceed();
HashMap<String, String> parseProceed = JSON.parseObject(JSON.toJSONString(proceed), HashMap.class);
if (parseProceed == null) {
return parseProceed;
2023-12-18 18:17:51 +08:00
}
2023-12-20 11:08:33 +08:00
// 数据同步
dataSync(parseProceed, joinPoint);
return proceed;
2023-12-17 15:44:38 +08:00
} catch (Exception e) {
2023-12-20 11:08:33 +08:00
throw e;
}
//
//
// MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
// try {
// HttpServletRequest request = getRequest();
// //获取资源映射路径servlet路径
// String servletPath = request.getServletPath();
// //验证该数据是否需要对接
// System.out.println("------------------------切面方法执行------------------------");
// //将路径换为对应的访问路径
// /*servletPath=dockingRelationMap.get(servletPath);
// //完整访问路径(访问港务局的路径,不是被访问的路径)
// String fullPath = gwjUrl+servletPath;
// System.out.println("完整路径:"+fullPath);*/
// //获取所有访问参数
// PageData pageData = getPageData();
// DockAnnotation annotation = method.getAnnotation(DockAnnotation.class);
// if (annotation != null) {
// if (annotation.isAdd()) {
// if (Tools.notEmpty(proceed.get("dockData"))) {
// pageData.put("dockData", proceed.get("dockData"));
// }
// } else if (annotation.hasAnnex()) {
// //查找附件
// String dockDataJson = pageData.getString("dockData");
// if (Tools.notEmpty(dockDataJson)) {
// Map<String, String> dockData = (Map<String, String>) JSON.parse(dockDataJson);
// String img = dockData.get("img");
// for (String imgurl : img.split(",")) {
// //获取附件名称
// String[] split = imgurl.split("/");
// String filename = split[split.length - 1];
// PageData imgpd = new PageData();
// imgpd.put("imgUrl", imgurl);
// imgpd.put("filename", filename);
// imgpd.put("url", "/file/saveImg");
// imgpd.put("TARGETURL", "https://qgqy.qhdsafety.com/file/");
// imgpd.put("ORIGINURL", "https://qgqy.qhdsafety.com/file/");
// TenCorpDto tenCorpDto = new TenCorpDto();
// tenCorpDto.setMessage("一条新增消息");
// tenCorpDto.setData(imgpd);
// tenCorpDto.setId(UuidUtil.get32UUID());
// tenCorpDto.setTopic("docking");
// tenCorpDto.setProducer_name("qa-prevention-czks");
// sendMessageService.sendMessage(tenCorpDto);
// }
// }
// }
// }
// PageData login_user = new PageData();
// if (!servletPath.contains("/app/")) {
// login_user.put("USER_ID", Jurisdiction.getUSER_ID());
// login_user.put("USERNAME", Jurisdiction.getUsername());
// login_user.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
// login_user.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
// login_user.put("NAME", Jurisdiction.getName());
// login_user.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
// login_user.put("POST_ID", Jurisdiction.getPOST_ID());
// }
// pageData.put("LOGIN_USER", login_user);
// pageData.put("url", servletPath);
// /*//连接redis
// Jedis jedis = new Jedis(host,port);
// //测试是否连接成功
// String ping = jedis.ping();
// System.out.println(ping);
// //切换数据库
// String select = jedis.select(0);
// System.out.println(select);
// //存储数据至redis
// String rq = JSON.toJSONString(map);
// jedis.zadd("request",0,rq);
// System.out.println(rq);*/
// TenCorpDto tenCorpDto = new TenCorpDto();
// tenCorpDto.setMessage("一条新增消息");
// tenCorpDto.setData(pageData);
// tenCorpDto.setId(UuidUtil.get32UUID());
// tenCorpDto.setTopic("docking");
// tenCorpDto.setProducer_name("qa-prevention-czks");
// sendMessageService.sendMessage(tenCorpDto);
// System.out.println("------------------------方法执行完毕------------------------");
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// return proceed;
}
private void dataSync(HashMap<String, String> proceed, ProceedingJoinPoint joinPoint) throws Exception {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
DockAnnotation annotation = method.getAnnotation(DockAnnotation.class);
//获取资源映射路径servlet路径
String servletPath = getRequest().getServletPath();
//验证该数据是否需要对接
System.out.println("------------------------切面方法执行------------------------");
PageData sendData = this.getPageData();
// app 方法 不取session
PageData login_user = new PageData();
if (!servletPath.contains("/app/")) {
login_user.put("USER_ID", Jurisdiction.getUSER_ID());
login_user.put("USERNAME", Jurisdiction.getUsername());
login_user.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
login_user.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
login_user.put("NAME", Jurisdiction.getName());
login_user.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
login_user.put("POST_ID", Jurisdiction.getPOST_ID());
2023-12-22 08:42:29 +08:00
login_user.put("IS_MAIN", Jurisdiction.getIS_MAIN());
Session session = Jurisdiction.getSession();
User user = (User)session.getAttribute(Const.SESSION_USER);
login_user.put("user",JSON.toJSONString(user));
2023-12-20 11:08:33 +08:00
sendData.put("LOGIN_USER", login_user);
} else {
// 手机app 会传递值。
login_user.put("USER_ID", Tools.notEmpty(sendData.getString("USER_ID")) ? sendData.getString("USER_ID") : "");
sendData.put("LOGIN_USER", login_user);
}
// 路径地址
sendData.put("url", servletPath);
// 有存自己表里的图片 把自己图片服务器的前缀传过去
if (annotation.hasAnnex()) {
2023-12-20 17:16:37 +08:00
sendData.put("BASEIMGPATH", "czks-baseimgpath");
2023-12-20 11:08:33 +08:00
// 自己表里的图片集合
sendData.put("sendPicturesList", proceed.get("sendPicturesList"));
proceed.remove("sendPicturesList");
}
// 有dockData
if (Tools.notEmpty(proceed.get("dockData"))) {
sendData.put("dockData", proceed.get("dockData"));
proceed.remove("dockData");
} else {
sendData.put("dockData", "手动添加的dockData");
2023-12-17 15:44:38 +08:00
}
2023-12-20 11:08:33 +08:00
TenCorpDto tenCorpDto = new TenCorpDto();
tenCorpDto.setMessage("沧州矿石数据同步消息");
tenCorpDto.setData(sendData);
tenCorpDto.setId(UuidUtil.get32UUID());
tenCorpDto.setTopic("docking");
tenCorpDto.setProducer_name("qa-prevention-czks");
sendMessageService.sendMessage(tenCorpDto);
2023-12-20 17:16:37 +08:00
System.out.println("------------------------切面方法结束------------------------");
2023-12-17 15:44:38 +08:00
}
@PostConstruct
private void initDockingRelationship() throws Exception {
dockingRelationMap = dockingRelationService.listAll(null);
}
private PageData getPageData() {
return new PageData(this.getRequest());
}
/**
* request
2023-12-20 11:08:33 +08:00
*
2023-12-17 15:44:38 +08:00
* @return
*/
private HttpServletRequest getRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
return request;
}
}