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

import cn.wisenergy.chnmuseum.party.auth.util.JwtTokenUtil;
wzp's avatar
wzp committed
4
import cn.wisenergy.chnmuseum.party.common.enums.*;
5
import cn.wisenergy.chnmuseum.party.common.util.DateUtil80;
wzp's avatar
wzp committed
6
import cn.wisenergy.chnmuseum.party.common.vo.AuditStatusParam;
wzp's avatar
wzp committed
7
import cn.wisenergy.chnmuseum.party.common.vo.GenericPageParam;
8
import cn.wisenergy.chnmuseum.party.mapper.SysLogMapper;
wzp's avatar
wzp committed
9
import cn.wisenergy.chnmuseum.party.model.*;
wzp's avatar
wzp committed
10
import cn.wisenergy.chnmuseum.party.service.TOperationLogService;
wzp's avatar
wzp committed
11
import cn.wisenergy.chnmuseum.party.service.impl.AuditServiceImpl;
wzp's avatar
wzp committed
12
import cn.wisenergy.chnmuseum.party.service.impl.CopyrightOwnerServiceImpl;
13 14 15
import cn.wisenergy.chnmuseum.party.service.impl.TUserServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
wzp's avatar
wzp committed
16
import org.apache.shiro.authc.AuthenticationException;
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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
35
import java.time.LocalDateTime;
36
import java.util.HashMap;
wzp's avatar
wzp committed
37
import java.util.Map;
liqin's avatar
liqin committed
38
import java.util.Objects;
39

liqin's avatar
liqin committed
40
import static cn.wisenergy.chnmuseum.party.common.enums.CopyrightOwnerTypeEnum.VIDEO_CONTENT;
41 42 43 44

@Service
@Aspect
@Transactional
liqin's avatar
liqin committed
45
public class SystemOperationLogService extends ServiceImpl<SysLogMapper, SysLog> {
46 47 48 49 50 51 52

    @Resource
    private SysLogMapper sysLogMapper;

    @Autowired
    private TUserServiceImpl userService;

wzp's avatar
wzp committed
53 54 55
    @Autowired
    private AuditServiceImpl auditService;

wzp's avatar
wzp committed
56 57 58
    @Autowired
    private CopyrightOwnerServiceImpl copyrightOwnerService;

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

62 63 64 65 66 67
    public SystemOperationLogService() {
    }

    public TUser getCurAdmin(HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        if (StringUtils.isBlank(header)) {
wzp's avatar
wzp committed
68
            throw new AuthenticationException("token失效,请重新登录");
69 70
        }
        String username = JwtTokenUtil.getUsername(header);
liqin's avatar
liqin committed
71
        return userService.selectByUsername(username);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }

    /**
     * 切点
     */
    @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 {
liqin's avatar
liqin committed
90
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
91 92
        MethodLog methodLog = getAnnotationLog(point);
        String ip = getIp(request);
liqin's avatar
liqin committed
93
        TUser user;
wzp's avatar
wzp committed
94 95 96 97 98
        try {
            user = getCurAdmin(request);
        } catch (Exception e) {
            HashMap<Object, Object> resultMap = new HashMap<>();
            resultMap.put("resultCode", "500");
wzp's avatar
wzp committed
99
            resultMap.put("message", e.getMessage());
wzp's avatar
wzp committed
100 101
            return resultMap;
        }
102 103 104 105 106
        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
107
                return getFailResult();
108 109
            }
        }
liqin's avatar
liqin committed
110 111 112
        Object[] method_param;
        Object object = point.proceed();
        method_param = point.getArgs(); //获取方法参数
wzp's avatar
wzp committed
113
        if (OperModule.STBOPERATION.getMsg().equals(methodLog.operModule().getMsg())) {
wzp's avatar
wzp committed
114 115 116
            TOperationLog operationLog = new TOperationLog();
            operationLog.setCreateTime(LocalDateTime.now());
            operationLog.setUserId(user.getId());
wzp's avatar
wzp committed
117 118 119
            if (user.getAreaName()!=null) {
                operationLog.setArea(user.getAreaName());
            }
wzp's avatar
wzp committed
120
            operationLog.setOperationType(methodLog.operType().getMsg());
121 122
            operationLogService.save(operationLog);
            return object;
wzp's avatar
wzp committed
123
        }
124
        SysLog sysLog = new SysLog();
liqin's avatar
liqin committed
125
        // todo null
wzp's avatar
wzp committed
126
        if (user.getRoleList().contains("1")) {
127
            sysLog.setType(1);
wzp's avatar
wzp committed
128
        } else {
129 130 131 132
            sysLog.setType(2);
        }
        sysLog.setOperator(user.getUserName());
        sysLog.setOperationIp(ip);
wzp's avatar
wzp committed
133
//        sysLog.setOperationContent(methodName);
134 135
        sysLog.setOperationTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        // 处理设置注解上的参数
wzp's avatar
wzp committed
136
        getControllerMethodDescription(user, methodLog, sysLog, method_param);
liqin's avatar
liqin committed
137
        sysLogMapper.insert(sysLog);
138
        return object;
139 140 141 142 143
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
wzp's avatar
wzp committed
144
     * @param sysLog 日志
145 146 147
     * @param
     * @throws Exception
     */
wzp's avatar
wzp committed
148
    public void getControllerMethodDescription(TUser user, MethodLog methodLog, SysLog sysLog, Object[] method_param) throws Exception {
149
        // 设置action动作
wzp's avatar
wzp committed
150 151 152 153 154 155
        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
156
            String type = null;
wzp's avatar
wzp committed
157 158
            if (methodLog.operType().getCode() == OperType.ADD.getCode() || methodLog.operType().getCode() == OperType.UPDATE.getCode()) {
                u = (TUser) method_param[0];
wzp's avatar
wzp committed
159
                type = u.getType();
wzp's avatar
wzp committed
160 161
            } else if (methodLog.operType().getCode() == OperType.CHANGE_PASSWORD.getCode()) {
                u = user;
wzp's avatar
wzp committed
162
                type = u.getType();
wzp's avatar
wzp committed
163 164
            } else if (methodLog.operType().getCode() == OperType.SELECT.getCode()) {
                String s = (String) method_param[0];
wzp's avatar
wzp committed
165 166
                type = s;
            } else {
wzp's avatar
wzp committed
167 168
                String s = (String) method_param[0];
                u = userService.getById(s);
wzp's avatar
wzp committed
169
                type = u.getType();
wzp's avatar
wzp committed
170
            }
wzp's avatar
wzp committed
171
            switch (type) {
wzp's avatar
wzp committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                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();
wzp's avatar
wzp committed
198 199
                status = auditStatusParam.getStatus();
                if (AuditStatusEnum.REFUSED.name().equals(status)) {
wzp's avatar
wzp committed
200
                    sysLog.setOperationContent(AuditStatusEnum.REFUSED.getMsg());
wzp's avatar
wzp committed
201
                } else {
wzp's avatar
wzp committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215
                    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());
            }
wzp's avatar
wzp committed
216
        } else if (methodLog.operModule().getCode() == OperModule.STBOPERATION.getCode() && methodLog.operType().getCode() == OperType.ACTIVATION.getCode()) {
wzp's avatar
wzp committed
217
            TBoxOperation o = (TBoxOperation) method_param[0];
wzp's avatar
wzp committed
218
            if (o.getStatus() == 2) {
wzp's avatar
wzp committed
219
                sysLog.setOperationType(OperType.ACTIVATION.getMsg());
wzp's avatar
wzp committed
220
            } else {
wzp's avatar
wzp committed
221 222
                sysLog.setOperationType(OperType.FAULT.getMsg());
            }
wzp's avatar
wzp committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        } else if (methodLog.operModule().getCode() == OperModule.LEARNCONTENT.getCode() && methodLog.operType().getCode() == OperType.ENABLE.getCode()) {
            Boolean o = (Boolean) method_param[1];
            if (o) {
                sysLog.setOperationType(OperType.ENABLE.getMsg());
            } else {
                sysLog.setOperationType(OperType.DISABLE.getMsg());
            }
        } else if (methodLog.operModule().getCode() == OperModule.DISPLAYCONTENT.getCode() && methodLog.operType().getCode() == OperType.UPDATE.getCode()) {
            Boolean o = (Boolean) method_param[1];
            if (o) {
                sysLog.setOperationType(OperType.UPDATE.getMsg());
            } else {
                sysLog.setOperationType(OperType.LOWER.getMsg());
            }

liqin's avatar
liqin committed
238
        } else if (methodLog.operModule().getCode() == OperModule.VIDEOCOPYRIGHT.getCode()) {
wzp's avatar
wzp committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
            GenericPageParam g = (GenericPageParam) method_param[0];
            if (VIDEO_CONTENT.name().equals(g.getOwnerType())) {
                sysLog.setOperationObject(OperModule.VIDEOCOPYRIGHT.getMsg());
            } else {
                sysLog.setOperationObject(OperModule.DISPLAYCOPYRIGHT.getMsg());
            }

        } else if (methodLog.operModule().getCode() == OperModule.DISPLAYCOPYRIGHT.getCode()) {
            CopyrightOwner c = null;
            String type = null;
            if (methodLog.operType().getCode() == OperType.ADD.getCode() || methodLog.operType().getCode() == OperType.UPDATE.getCode()) {
                c = (CopyrightOwner) method_param[0];
                type = c.getOwnerType();
            } else if (methodLog.operType().getCode() == OperType.DETAILS.getCode() || methodLog.operType().getCode() == OperType.DELETE.getCode()) {
                String id = (String) method_param[0];
                CopyrightOwner byId = copyrightOwnerService.getById(id);
                type = byId.getOwnerType();
            } else if (methodLog.operType().getCode() == OperType.SELECT.getCode()) {
                CopyrightOwnerTypeEnum s = (CopyrightOwnerTypeEnum) method_param[0];
                type = s.name();
            }
            switch (type) {
                case "EXHIBITION_BOARD":
                    // 设置标题
                    sysLog.setOperationObject(OperModule.DISPLAYCOPYRIGHT.getMsg());
                    break;
                case "VIDEO_CONTENT":
                    sysLog.setOperationObject(OperModule.VIDEOCOPYRIGHT.getMsg());
                    break;
            }
wzp's avatar
wzp committed
269 270
        }

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    }

    /**
     * 是否存在注解,如果存在就获取
     */
    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
287 288
    protected Map<String, Object> getFailResult() {
        Map<String, Object> map = new HashMap<>();
wzp's avatar
wzp committed
289 290
        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
291 292 293
        return map;
    }

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    /**
     * 方法异常时调用
     *
     * @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
311
//        String ip = request.getHeader("x-forwarded-for");
wzp's avatar
wzp committed
312
        String ip = ip = request.getRemoteAddr();
313 314 315 316 317 318 319 320 321 322 323 324 325
        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
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340
            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;
    }
}