SystemOperationLogService.java 12 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.AuditStatusEnum;
import cn.wisenergy.chnmuseum.party.common.enums.AuditTypeEnum;
wzp's avatar
wzp committed
7 8
import cn.wisenergy.chnmuseum.party.common.enums.RESPONSE_CODE_ENUM;
import cn.wisenergy.chnmuseum.party.common.enums.RESULT_INFO_ENUM;
9
import cn.wisenergy.chnmuseum.party.common.util.DateUtil80;
wzp's avatar
wzp committed
10
import cn.wisenergy.chnmuseum.party.common.vo.AuditStatusParam;
wzp's avatar
wzp committed
11
import cn.wisenergy.chnmuseum.party.core.annotations.OperationLog;
12
import cn.wisenergy.chnmuseum.party.mapper.SysLogMapper;
wzp's avatar
wzp committed
13
import cn.wisenergy.chnmuseum.party.model.*;
wzp's avatar
wzp committed
14
import cn.wisenergy.chnmuseum.party.service.TOperationLogService;
wzp's avatar
wzp committed
15
import cn.wisenergy.chnmuseum.party.service.impl.AuditServiceImpl;
16 17 18
import cn.wisenergy.chnmuseum.party.service.impl.TUserServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javassist.util.HotSwapper;
wzp's avatar
wzp committed
19
import org.apache.commons.beanutils.BeanUtils;
20
import org.apache.commons.lang3.StringUtils;
wzp's avatar
wzp committed
21
import org.apache.shiro.authc.AuthenticationException;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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
40
import java.time.LocalDateTime;
41
import java.util.HashMap;
42
import java.util.List;
wzp's avatar
wzp committed
43
import java.util.Map;
44 45 46 47 48 49 50 51 52 53 54 55 56


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

    @Resource
    private SysLogMapper sysLogMapper;

    @Autowired
    private TUserServiceImpl userService;

wzp's avatar
wzp committed
57 58 59
    @Autowired
    private AuditServiceImpl auditService;

wzp's avatar
wzp committed
60 61 62
    @Autowired
    private TOperationLogService operationLogService;

63 64 65 66 67 68 69
    public SystemOperationLogService() {
        System.out.println("Aop");
    }

    public TUser getCurAdmin(HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        if (StringUtils.isBlank(header)) {
wzp's avatar
wzp committed
70
            throw new AuthenticationException("token失效,请重新登录");
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        }
        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);
wzp's avatar
wzp committed
99 100 101 102 103 104
        TUser user = null;
        try {
            user = getCurAdmin(request);
        } catch (Exception e) {
            HashMap<Object, Object> resultMap = new HashMap<>();
            resultMap.put("resultCode", "500");
wzp's avatar
wzp committed
105
            resultMap.put("message", e.getMessage());
wzp's avatar
wzp committed
106 107
            return resultMap;
        }
108 109 110 111 112
        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
113
                return getFailResult();
114 115 116 117 118
            }
        }
        String operatingcontent = "";
        Object[] method_param = null;

wzp's avatar
wzp committed
119
        Object object = null;
120 121 122 123 124
        try {
            method_param = point.getArgs(); //获取方法参数
            // String param=(String) point.proceed(point.getArgs());
            object = point.proceed();
        } catch (Exception e) {
wzp's avatar
wzp committed
125
            // 异常处理记录日志..
wzp's avatar
wzp committed
126
            e.printStackTrace();
wzp's avatar
wzp committed
127
            return getFailResult();
128
        }
wzp's avatar
wzp committed
129

wzp's avatar
wzp committed
130 131
//        getMethodLog(user,methodLog,method_param);

wzp's avatar
wzp committed
132
        if (OperModule.STBOPERATION.getMsg().equals(methodLog.operModule().getMsg())) {
wzp's avatar
wzp committed
133 134 135
            TOperationLog operationLog = new TOperationLog();
            operationLog.setCreateTime(LocalDateTime.now());
            operationLog.setUserId(user.getId());
wzp's avatar
wzp committed
136
            operationLog.setArea(user.getAreaName());
wzp's avatar
wzp committed
137
            operationLog.setOperationType(methodLog.operType().getMsg());
138 139
            operationLogService.save(operationLog);
            return object;
wzp's avatar
wzp committed
140
        }
141
        SysLog sysLog = new SysLog();
wzp's avatar
wzp committed
142
        if (user.getRoleList().contains("1")) {
143
            sysLog.setType(1);
wzp's avatar
wzp committed
144
        } else {
145 146 147 148 149
            sysLog.setType(2);
        }
        sysLog.setOperator(user.getUserName());
        sysLog.setOperationIp(ip);

wzp's avatar
wzp committed
150
//        sysLog.setOperationContent(methodName);
151 152
        sysLog.setOperationTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        // 处理设置注解上的参数
wzp's avatar
wzp committed
153
        getControllerMethodDescription(user,methodLog, sysLog,method_param);
154

wzp's avatar
wzp committed
155
        int insert = sysLogMapper.insert(sysLog);
156
        return object;
157 158 159 160 161
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
wzp's avatar
wzp committed
162
     * @param sysLog 日志
163 164 165
     * @param
     * @throws Exception
     */
wzp's avatar
wzp committed
166
    public void getControllerMethodDescription(TUser user,MethodLog methodLog, SysLog sysLog,Object[] method_param) throws Exception {
167
        // 设置action动作
wzp's avatar
wzp committed
168 169 170 171 172 173
        sysLog.setOperationType(methodLog.operType().getMsg());
        sysLog.setOperationContent(methodLog.operType().getMsg());
        sysLog.setOperationObject(methodLog.operModule().getMsg());
        //判断是哪个页面调的用户接口,返回不同日志操作对象
        if (methodLog.operModule().getCode() == OperModule.USER.getCode()) {
            TUser u = null;
wzp's avatar
wzp committed
174
            String type = null;
wzp's avatar
wzp committed
175 176
            if (methodLog.operType().getCode() == OperType.ADD.getCode() || methodLog.operType().getCode() == OperType.UPDATE.getCode()) {
                u = (TUser) method_param[0];
wzp's avatar
wzp committed
177
                type=u.getType();
wzp's avatar
wzp committed
178 179
            } else if (methodLog.operType().getCode() == OperType.CHANGE_PASSWORD.getCode()) {
                u = user;
wzp's avatar
wzp committed
180 181 182 183 184
                type=u.getType();
            } else if (methodLog.operType().getCode() == OperType.SELECT.getCode()) {
                String s = (String) method_param[0];
                type=s;
            }else {
wzp's avatar
wzp committed
185 186
                String s = (String) method_param[0];
                u = userService.getById(s);
wzp's avatar
wzp committed
187
                type=u.getType();
wzp's avatar
wzp committed
188
            }
wzp's avatar
wzp committed
189
            switch (type) {
wzp's avatar
wzp committed
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
                case "1":
                    // 设置标题
                    sysLog.setOperationObject(OperModule.USER.getMsg());
                    break;
                case "2":
                    sysLog.setOperationObject(OperModule.UNITADMIN.getMsg());
                    break;
                case "3":
                    sysLog.setOperationObject(OperModule.STBBASE.getMsg());
                    break;
                case "4":
                    sysLog.setOperationObject(OperModule.STBACCOUNT.getMsg());
                    break;
                case "5":
                    sysLog.setOperationObject(OperModule.STATISTICALUSER.getMsg());
                    break;
            }
        }
        //判断是哪个页面调的审核接口,返回不同日志操作对象
        else if (methodLog.operModule().getCode() == OperModule.CHECKVIDEO.getCode()) {
            String type = null;
            String status = null;
            if (methodLog.operType().getCode() == OperType.AUDIT.getCode()) {
                AuditStatusParam auditStatusParam = (AuditStatusParam) method_param[0];
                Audit byId = auditService.getById(auditStatusParam.getId());
                type = byId.getType();
                status=auditStatusParam.getStatus();
wzp's avatar
wzp committed
217
                if (AuditStatusEnum.REFUSED.name().equals(status)){
wzp's avatar
wzp committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                    sysLog.setOperationContent(AuditStatusEnum.REFUSED.getMsg());
                }else {
                    sysLog.setOperationContent(AuditStatusEnum.APPROVED_FINAL.getMsg());
                }
            } else if (methodLog.operType().getCode() == OperType.DETAILS.getCode()) {
                type = (String) method_param[1];
            } else if (methodLog.operType().getCode() == OperType.SELECT.getCode()) {
                type = (String) method_param[2];
            }
            if (AuditTypeEnum.EXHIBITION_BOARD.name().equals(type)) {
                sysLog.setOperationObject(OperModule.CHECKDISPLAY.getMsg());
            } else if (AuditTypeEnum.LEARNING_CONTENT.name().equals(type)) {
                sysLog.setOperationObject(OperModule.CHECKLEARN.getMsg());
            } else if (AuditTypeEnum.VIDEO_CONTENT.name().equals(type)) {
                sysLog.setOperationObject(OperModule.CHECKVIDEO.getMsg());
            }
        }else if(methodLog.operModule().getCode() == OperModule.STBOPERATION.getCode()&&methodLog.operType().getCode()==OperType.ACTIVATION.getCode()){
            TBoxOperation o = (TBoxOperation) method_param[0];
            if (o.getStatus()==2){
                sysLog.setOperationType(OperType.ACTIVATION.getMsg());
            }else {
                sysLog.setOperationType(OperType.FAULT.getMsg());
            }
        }

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    }

    /**
     * 是否存在注解,如果存在就获取
     */
    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
259 260
    protected Map<String, Object> getFailResult() {
        Map<String, Object> map = new HashMap<>();
wzp's avatar
wzp committed
261 262
        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
263 264 265
        return map;
    }

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    /**
     * 方法异常时调用
     *
     * @param
     */
    @AfterThrowing("methodCachePointcut()")
    public void afterThrowing(JoinPoint point) throws Throwable {

    }

    /**
     * 获取请求ip
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
wzp's avatar
wzp committed
283
//        String ip = request.getHeader("x-forwarded-for");
wzp's avatar
wzp committed
284
        String ip = ip = request.getRemoteAddr();
285 286 287 288 289 290 291 292 293 294 295 296 297
        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)) {
wzp's avatar
wzp committed
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312
            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;
    }
}