SystemOperationLogService.java 7.89 KB
Newer Older
1 2 3 4
package cn.wisenergy.chnmuseum.party.common.log;


import cn.wisenergy.chnmuseum.party.auth.util.JwtTokenUtil;
wzp's avatar
wzp committed
5 6
import cn.wisenergy.chnmuseum.party.common.enums.RESPONSE_CODE_ENUM;
import cn.wisenergy.chnmuseum.party.common.enums.RESULT_INFO_ENUM;
7
import cn.wisenergy.chnmuseum.party.common.util.DateUtil80;
wzp's avatar
wzp committed
8
import cn.wisenergy.chnmuseum.party.core.annotations.OperationLog;
9 10
import cn.wisenergy.chnmuseum.party.mapper.SysLogMapper;
import cn.wisenergy.chnmuseum.party.model.SysLog;
wzp's avatar
wzp committed
11
import cn.wisenergy.chnmuseum.party.model.TOperationLog;
12
import cn.wisenergy.chnmuseum.party.model.TUser;
wzp's avatar
wzp committed
13
import cn.wisenergy.chnmuseum.party.service.TOperationLogService;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import cn.wisenergy.chnmuseum.party.service.impl.TUserServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javassist.util.HotSwapper;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
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;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.net.InetAddress;
wzp's avatar
wzp committed
36
import java.time.LocalDateTime;
37
import java.util.HashMap;
38
import java.util.List;
wzp's avatar
wzp committed
39
import java.util.Map;
40 41 42 43 44 45 46 47 48 49 50 51 52


@Service
@Aspect
@Transactional
class SystemOperationLogService extends ServiceImpl<SysLogMapper, SysLog> {

    @Resource
    private SysLogMapper sysLogMapper;

    @Autowired
    private TUserServiceImpl userService;

wzp's avatar
wzp committed
53 54 55
    @Autowired
    private TOperationLogService operationLogService;

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    public SystemOperationLogService() {
        System.out.println("Aop");
    }

    public TUser getCurAdmin(HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        if (StringUtils.isBlank(header)) {
            return null;
        }
        String username = JwtTokenUtil.getUsername(header);
        TUser user = userService.selectByUsername(username);
        return user;
    }

    /**
     * 切点
     */
    @Pointcut("@annotation(cn.wisenergy.chnmuseum.party.common.log.MethodLog)")
    public void methodCachePointcut() {
    }


    /**
     * 切面
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("methodCachePointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        MethodLog methodLog = getAnnotationLog(point);
        String ip = getIp(request);
        TUser user = getCurAdmin(request);
wzp's avatar
wzp committed
93 94 95 96 97 98
//        if (user==null){
//            HashMap<String, Object> resultMap = new HashMap<>();
//            resultMap.put("resultCode", "500");
//            resultMap.put("message", "用户未登录!");
//            return resultMap;
//        }
99 100 101 102 103 104 105 106 107
        String methodName = user.getUserName() + "登录本系统";
        if (user.getOrgName() != null) {
            methodName += ",机构" + user.getOrgName();
        }
        String packages = point.getThis().getClass().getName();
        if (packages.indexOf("$$EnhancerByCGLIB$$") > -1) { // 如果是CGLIB动态生成的类
            try {
                packages = packages.substring(0, packages.indexOf("$$"));
            } catch (Exception ex) {
wzp's avatar
wzp committed
108
                return getFailResult();
109 110 111 112 113
            }
        }
        String operatingcontent = "";
        Object[] method_param = null;

wzp's avatar
wzp committed
114
        Object object = null;
115 116 117 118 119 120
        try {
            method_param = point.getArgs(); //获取方法参数
            // String param=(String) point.proceed(point.getArgs());
            object = point.proceed();
        } catch (Exception e) {
            // 异常处理记录日志..log.error(e);
wzp's avatar
wzp committed
121
            return getFailResult();
122
        }
wzp's avatar
wzp committed
123 124 125 126 127
        if (methodLog.operModule().getMsg().contains("运维")) {
            TOperationLog operationLog = new TOperationLog();
            operationLog.setCreateTime(LocalDateTime.now());
            operationLog.setUserId(user.getId());
            operationLog.setOperationType(methodLog.operType().getMsg());
128 129
            operationLogService.save(operationLog);
            return object;
wzp's avatar
wzp committed
130
        }
131 132 133
        SysLog sysLog = new SysLog();
        if ("1".equals(user.getId())) {
            sysLog.setType(1);
wzp's avatar
wzp committed
134
        } else {
135 136 137 138 139 140 141 142 143 144
            sysLog.setType(2);
        }
        sysLog.setOperator(user.getUserName());
        sysLog.setOperationIp(ip);

        sysLog.setOperationContent(methodName);
        sysLog.setOperationTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        // 处理设置注解上的参数
        getControllerMethodDescription(methodLog, sysLog);

wzp's avatar
wzp committed
145
        int insert = sysLogMapper.insert(sysLog);
146
        return object;
147 148 149 150 151 152 153 154 155 156 157
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param log 日志
     * @param
     * @throws Exception
     */
    public void getControllerMethodDescription(MethodLog log, SysLog sysLog) throws Exception {
        // 设置action动作
158
        sysLog.setOperationType(log.operType().getMsg());
159
        // 设置标题
160
        sysLog.setOperationObject(log.operModule().getMsg());
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    }

    /**
     * 是否存在注解,如果存在就获取
     */
    private MethodLog getAnnotationLog(JoinPoint joinPoint) throws Exception {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null) {
            return method.getAnnotation(MethodLog.class);
        }
        return null;
    }

wzp's avatar
wzp committed
177 178
    protected Map<String, Object> getFailResult() {
        Map<String, Object> map = new HashMap<>();
wzp's avatar
wzp committed
179 180
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), RESPONSE_CODE_ENUM.SERVER_ERROR.getResultCode());
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), RESPONSE_CODE_ENUM.SERVER_ERROR.getMessage());
wzp's avatar
wzp committed
181 182 183
        return map;
    }

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    /**
     * 方法异常时调用
     *
     * @param
     */
    @AfterThrowing("methodCachePointcut()")
    public void afterThrowing(JoinPoint point) throws Throwable {

    }

    /**
     * 获取请求ip
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ip = inet.getHostAddress();
            }
        }
        return ip;
    }
}