| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.common.aspectj; |
| | | |
| | | import com.ruoyi.common.annotation.AddOrgId; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import org.aspectj.lang.JoinPoint; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.annotation.Before; |
| | | import org.aspectj.lang.reflect.MethodSignature; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | | * è®¾ç½®æºæID |
| | | * |
| | | * @author ls |
| | | */ |
| | | @Aspect |
| | | @Component |
| | | public class SetOrgIdAspect |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(SetOrgIdAspect.class); |
| | | |
| | | @Before("@annotation(setOrgId)") |
| | | public void setOrgId(JoinPoint joinPoint, AddOrgId setOrgId) throws Throwable |
| | | { |
| | | if (setOrgId != null) |
| | | { |
| | | // è·åå½åç»å½ç¨æ· |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | if (loginUser != null && loginUser.getUser() != null) |
| | | { |
| | | String orgid = loginUser.getUser().getOrgid(); |
| | | String campusid = loginUser.getUser().getCampusid(); |
| | | |
| | | // è·åæ¹æ³åæ° |
| | | Object[] args = joinPoint.getArgs(); |
| | | int paramIndex = setOrgId.paramIndex(); |
| | | if (paramIndex >= 0 && paramIndex < args.length) |
| | | { |
| | | Object param = args[paramIndex]; |
| | | if (param != null) |
| | | { |
| | | // 设置orgid |
| | | if (orgid != null) |
| | | { |
| | | // è·ååæ®µåç§° |
| | | String fieldName = setOrgId.field(); |
| | | |
| | | // æ£æ¥å段æ¯å¦å·²ç»æå¼ï¼å¦ææä¸skipIfPresent为trueï¼åè·³è¿è®¾ç½® |
| | | Object currentValue = getFieldValue(param, fieldName); |
| | | if (setOrgId.skipIfPresent() && currentValue != null && !currentValue.toString().isEmpty()) { |
| | | // 妿忮µå·²ç»æå¼ä¸skipIfPresent为trueï¼åè·³è¿è®¾ç½® |
| | | log.debug("åæ®µ {} å·²æå¼: {}ï¼è·³è¿è®¾ç½®", fieldName, currentValue); |
| | | } else { |
| | | setFieldValue(param, fieldName, orgid); |
| | | } |
| | | } |
| | | |
| | | // 妿éè¦åæ¶è®¾ç½®campusid |
| | | if (setOrgId.withCampusId() && campusid != null) |
| | | { |
| | | String campusFieldName = setOrgId.campusField(); |
| | | |
| | | // æ£æ¥å段æ¯å¦å·²ç»æå¼ï¼å¦ææä¸skipIfPresent为trueï¼åè·³è¿è®¾ç½® |
| | | Object currentValueCampus = getFieldValue(param, campusFieldName); |
| | | if (setOrgId.skipIfPresent() && currentValueCampus != null && !currentValueCampus.toString().isEmpty()) { |
| | | // 妿忮µå·²ç»æå¼ä¸skipIfPresent为trueï¼åè·³è¿è®¾ç½® |
| | | log.debug("åæ®µ {} å·²æå¼: {}ï¼è·³è¿è®¾ç½®", campusFieldName, currentValueCampus); |
| | | } else { |
| | | setFieldValue(param, campusFieldName, campusid); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å注解 |
| | | */ |
| | | private AddOrgId getAnnotationLog(JoinPoint joinPoint) throws Exception |
| | | { |
| | | MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| | | Method method = signature.getMethod(); |
| | | if (method != null) |
| | | { |
| | | return method.getAnnotation(AddOrgId.class); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * è·åå¯¹è±¡åæ®µçå¼ |
| | | */ |
| | | private Object getFieldValue(Object obj, String fieldName) throws Exception |
| | | { |
| | | Class<?> clazz = obj.getClass(); |
| | | Field field = null; |
| | | |
| | | // æ¥æ¾å段ï¼å
æ¬ç¶ç±»ä¸çåæ®µ |
| | | while (clazz != null && field == null) |
| | | { |
| | | try |
| | | { |
| | | field = clazz.getDeclaredField(fieldName); |
| | | } |
| | | catch (NoSuchFieldException e) |
| | | { |
| | | clazz = clazz.getSuperclass(); |
| | | } |
| | | } |
| | | |
| | | if (field != null) |
| | | { |
| | | field.setAccessible(true); |
| | | return field.get(obj); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * è®¾ç½®å¯¹è±¡åæ®µçå¼ |
| | | */ |
| | | private void setFieldValue(Object obj, String fieldName, Object value) throws Exception |
| | | { |
| | | Class<?> clazz = obj.getClass(); |
| | | Field field = null; |
| | | |
| | | // æ¥æ¾å段ï¼å
æ¬ç¶ç±»ä¸çåæ®µ |
| | | while (clazz != null && field == null) |
| | | { |
| | | try |
| | | { |
| | | field = clazz.getDeclaredField(fieldName); |
| | | } |
| | | catch (NoSuchFieldException e) |
| | | { |
| | | clazz = clazz.getSuperclass(); |
| | | } |
| | | } |
| | | |
| | | if (field != null) |
| | | { |
| | | field.setAccessible(true); |
| | | field.set(obj, value); |
| | | } |
| | | else |
| | | { |
| | | log.warn("æªæ¾å°å段: {} å¨ç±»: {} åå
¶ç¶ç±»ä¸", fieldName, obj.getClass().getName()); |
| | | } |
| | | } |
| | | } |