SystemOperationLogService.java 14.3 KB
Newer Older
liqin's avatar
liqin committed
1
package cn.chnmuseum.party.common.log;
2

liqin's avatar
liqin committed
3 4 5 6 7 8 9 10 11 12 13
import cn.chnmuseum.party.auth.util.JwtTokenUtil;
import cn.chnmuseum.party.common.enums.*;
import cn.chnmuseum.party.common.util.DateUtil80;
import cn.chnmuseum.party.common.vo.AuditStatusParam;
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.mapper.SysLogMapper;
import cn.chnmuseum.party.model.*;
import cn.chnmuseum.party.service.TOperationLogService;
import cn.chnmuseum.party.service.impl.AuditServiceImpl;
import cn.chnmuseum.party.service.impl.CopyrightOwnerServiceImpl;
import cn.chnmuseum.party.service.impl.TUserServiceImpl;
14 15
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
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
liqin's avatar
liqin committed
21
import org.aspectj.lang.annotation.Before;
22 23 24 25 26 27 28 29 30 31 32 33
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
34
import java.time.LocalDateTime;
35
import java.util.HashMap;
wzp's avatar
wzp committed
36
import java.util.Map;
liqin's avatar
liqin committed
37
import java.util.Objects;
38

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

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

    @Resource
    private SysLogMapper sysLogMapper;

    @Autowired
    private TUserServiceImpl userService;

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

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

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

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

    public TUser getCurAdmin(HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        if (StringUtils.isBlank(header)) {
wzp's avatar
wzp committed
67
            throw new AuthenticationException("token失效,请重新登录");
68 69
        }
        String username = JwtTokenUtil.getUsername(header);
liqin's avatar
liqin committed
70
        return userService.selectByUsername(username);
71 72 73 74 75
    }

    /**
     * 切点
     */
liqin's avatar
liqin committed
76
    @Pointcut("@annotation(cn.chnmuseum.party.common.log.MethodLog)")
77 78 79 80 81 82 83 84 85
    public void methodCachePointcut() {
    }

    /**
     * 切面
     *
     * @param point
     * @return
     */
liqin's avatar
liqin committed
86 87
    @Before("methodCachePointcut()")
    public Object doBefore(JoinPoint point) {
liqin's avatar
liqin committed
88
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
89 90
        MethodLog methodLog = getAnnotationLog(point);
        String ip = getIp(request);
liqin's avatar
liqin committed
91
        TUser user;
wzp's avatar
wzp committed
92 93 94 95 96
        try {
            user = getCurAdmin(request);
        } catch (Exception e) {
            HashMap<Object, Object> resultMap = new HashMap<>();
            resultMap.put("resultCode", "500");
wzp's avatar
wzp committed
97
            resultMap.put("message", e.getMessage());
wzp's avatar
wzp committed
98 99
            return resultMap;
        }
100
        String packages = point.getThis().getClass().getName();
liqin's avatar
liqin committed
101
        if (packages.contains("$$EnhancerByCGLIB$$")) { // 如果是CGLIB动态生成的类
102 103 104
            try {
                packages = packages.substring(0, packages.indexOf("$$"));
            } catch (Exception ex) {
wzp's avatar
wzp committed
105
                return getFailResult();
106 107
            }
        }
liqin's avatar
liqin committed
108
        Object[] method_param = point.getArgs(); //获取方法参数;
wzp's avatar
wzp committed
109
        if (OperModule.STBOPERATION.getMsg().equals(methodLog.operModule().getMsg())) {
liqin's avatar
liqin committed
110 111 112 113 114 115 116 117
            Object obj = method_param[0];
            String status;
            if (obj instanceof TBoxOperation) {
                TBoxOperation t = (TBoxOperation) obj;
                status = t.getStatus() + "";
            } else {
                status = (String) obj;
            }
wzp's avatar
wzp committed
118 119 120
            TOperationLog operationLog = new TOperationLog();
            operationLog.setCreateTime(LocalDateTime.now());
            operationLog.setUserId(user.getId());
liqin's avatar
liqin committed
121
            if (user.getAreaName() != null) {
wzp's avatar
wzp committed
122 123
                operationLog.setArea(user.getAreaName());
            }
liqin's avatar
liqin committed
124 125 126
            if (OperType.ACTIVATION.getMsg().equals(methodLog.operType().getMsg()) && "3".equals(status)) {
                operationLog.setOperationType(OperType.FAULT.getMsg());
            } else {
wzp's avatar
wzp committed
127
                operationLog.setOperationType(OperType.ACTIVATION.getMsg());
wzp's avatar
wzp committed
128
            }
129
            operationLogService.save(operationLog);
liqin's avatar
liqin committed
130
            return method_param;
wzp's avatar
wzp committed
131
        }
132
        SysLog sysLog = new SysLog();
liqin's avatar
liqin committed
133
        // todo null
wzp's avatar
wzp committed
134
        if (user.getRoleList().contains("1")) {
135
            sysLog.setType(1);
wzp's avatar
wzp committed
136
        } else {
137 138 139 140
            sysLog.setType(2);
        }
        sysLog.setOperator(user.getUserName());
        sysLog.setOperationIp(ip);
wzp's avatar
wzp committed
141
//        sysLog.setOperationContent(methodName);
142 143
        sysLog.setOperationTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        // 处理设置注解上的参数
wzp's avatar
wzp committed
144
        getControllerMethodDescription(user, methodLog, sysLog, method_param);
liqin's avatar
liqin committed
145
        sysLogMapper.insert(sysLog);
liqin's avatar
liqin committed
146
        return method_param;
147 148 149 150 151
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
wzp's avatar
wzp committed
152
     * @param sysLog 日志
153 154
     * @param
     */
liqin's avatar
liqin committed
155
    public void getControllerMethodDescription(TUser user, MethodLog methodLog, SysLog sysLog, Object[] method_param) {
156
        // 设置action动作
wzp's avatar
wzp committed
157 158 159 160
        sysLog.setOperationType(methodLog.operType().getMsg());
        sysLog.setOperationContent(methodLog.operType().getMsg());
        sysLog.setOperationObject(methodLog.operModule().getMsg());
        //判断是哪个页面调的用户接口,返回不同日志操作对象
liqin's avatar
liqin committed
161 162 163 164
        if (methodLog.operModule().getCode().equals(OperModule.USER.getCode())) {
            TUser u;
            String type;
            if (methodLog.operType().getCode().equals(OperType.ADD.getCode()) || methodLog.operType().getCode().equals(OperType.UPDATE.getCode())) {
wzp's avatar
wzp committed
165
                u = (TUser) method_param[0];
wzp's avatar
wzp committed
166
                type = u.getType();
liqin's avatar
liqin committed
167
            } else if (methodLog.operType().getCode().equals(OperType.CHANGE_PASSWORD.getCode())) {
wzp's avatar
wzp committed
168
                u = user;
wzp's avatar
wzp committed
169
                type = u.getType();
liqin's avatar
liqin committed
170 171
            } else if (methodLog.operType().getCode().equals(OperType.SELECT.getCode())) {
                type = (String) method_param[0];
wzp's avatar
wzp committed
172
            } else {
wzp's avatar
wzp committed
173 174
                String s = (String) method_param[0];
                u = userService.getById(s);
wzp's avatar
wzp committed
175
                type = u.getType();
wzp's avatar
wzp committed
176
            }
wzp's avatar
wzp committed
177
            switch (type) {
wzp's avatar
wzp committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
                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;
            }
        }
        //判断是哪个页面调的审核接口,返回不同日志操作对象
liqin's avatar
liqin committed
197
        else if (methodLog.operModule().getCode().equals(OperModule.CHECKVIDEO.getCode())) {
wzp's avatar
wzp committed
198
            String type = null;
liqin's avatar
liqin committed
199 200
            String status;
            if (methodLog.operType().getCode().equals(OperType.AUDIT.getCode())) {
wzp's avatar
wzp committed
201 202
                AuditStatusParam auditStatusParam = (AuditStatusParam) method_param[0];
                Audit byId = auditService.getById(auditStatusParam.getId());
liqin's avatar
liqin committed
203 204 205 206 207 208 209 210
                if (byId != null) {
                    type = byId.getType();
                    status = auditStatusParam.getStatus();
                    if (AuditStatusEnum.REFUSED.name().equals(status)) {
                        sysLog.setOperationContent(AuditStatusEnum.REFUSED.getMsg());
                    } else {
                        sysLog.setOperationContent(AuditStatusEnum.APPROVED_FINAL.getMsg());
                    }
wzp's avatar
wzp committed
211
                } else {
wzp's avatar
wzp committed
212 213
                    sysLog.setOperationContent(AuditStatusEnum.APPROVED_FINAL.getMsg());
                }
liqin's avatar
liqin committed
214
            } else if (methodLog.operType().getCode().equals(OperType.DETAILS.getCode())) {
wzp's avatar
wzp committed
215
                type = (String) method_param[1];
liqin's avatar
liqin committed
216
            } else if (methodLog.operType().getCode().equals(OperType.SELECT.getCode())) {
wzp's avatar
wzp committed
217 218 219 220 221 222 223 224 225
                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());
            }
liqin's avatar
liqin committed
226
        } else if (methodLog.operModule().getCode().equals(OperModule.STBOPERATION.getCode()) && methodLog.operType().getCode().equals(OperType.ACTIVATION.getCode())) {
wzp's avatar
wzp committed
227
            TBoxOperation o = (TBoxOperation) method_param[0];
wzp's avatar
wzp committed
228
            if (o.getStatus() == 2) {
wzp's avatar
wzp committed
229
                sysLog.setOperationType(OperType.ACTIVATION.getMsg());
wzp's avatar
wzp committed
230
            } else {
wzp's avatar
wzp committed
231 232
                sysLog.setOperationType(OperType.FAULT.getMsg());
            }
liqin's avatar
liqin committed
233
        } else if (methodLog.operModule().getCode().equals(OperModule.LEARNCONTENT.getCode()) && methodLog.operType().getCode().equals(OperType.ENABLE.getCode())) {
wzp's avatar
wzp committed
234 235 236 237 238 239
            Boolean o = (Boolean) method_param[1];
            if (o) {
                sysLog.setOperationType(OperType.ENABLE.getMsg());
            } else {
                sysLog.setOperationType(OperType.DISABLE.getMsg());
            }
liqin's avatar
liqin committed
240
        } else if (methodLog.operModule().getCode().equals(OperModule.DISPLAYCONTENT.getCode()) && methodLog.operType().getCode().equals(OperType.UPDATE.getCode())) {
liqin's avatar
liqin committed
241
            sysLog.setOperationType(OperType.UPDATE.getMsg());
liqin's avatar
liqin committed
242
        } else if (methodLog.operModule().getCode().equals(OperModule.VIDEOCOPYRIGHT.getCode())) {
wzp's avatar
wzp committed
243 244 245 246 247 248
            GenericPageParam g = (GenericPageParam) method_param[0];
            if (VIDEO_CONTENT.name().equals(g.getOwnerType())) {
                sysLog.setOperationObject(OperModule.VIDEOCOPYRIGHT.getMsg());
            } else {
                sysLog.setOperationObject(OperModule.DISPLAYCOPYRIGHT.getMsg());
            }
liqin's avatar
liqin committed
249 250
        } else if (methodLog.operModule().getCode().equals(OperModule.DISPLAYCOPYRIGHT.getCode())) {
            CopyrightOwner c;
wzp's avatar
wzp committed
251
            String type = null;
liqin's avatar
liqin committed
252
            if (methodLog.operType().getCode().equals(OperType.ADD.getCode()) || methodLog.operType().getCode().equals(OperType.UPDATE.getCode())) {
wzp's avatar
wzp committed
253 254
                c = (CopyrightOwner) method_param[0];
                type = c.getOwnerType();
liqin's avatar
liqin committed
255
            } else if (methodLog.operType().getCode().equals(OperType.DETAILS.getCode()) || methodLog.operType().getCode().equals(OperType.DELETE.getCode())) {
wzp's avatar
wzp committed
256 257 258
                String id = (String) method_param[0];
                CopyrightOwner byId = copyrightOwnerService.getById(id);
                type = byId.getOwnerType();
liqin's avatar
liqin committed
259
            } else if (methodLog.operType().getCode().equals(OperType.SELECT.getCode())) {
wzp's avatar
wzp committed
260 261 262
                CopyrightOwnerTypeEnum s = (CopyrightOwnerTypeEnum) method_param[0];
                type = s.name();
            }
liqin's avatar
liqin committed
263
            switch (Objects.requireNonNull(type)) {
wzp's avatar
wzp committed
264 265 266 267 268 269 270 271
                case "EXHIBITION_BOARD":
                    // 设置标题
                    sysLog.setOperationObject(OperModule.DISPLAYCOPYRIGHT.getMsg());
                    break;
                case "VIDEO_CONTENT":
                    sysLog.setOperationObject(OperModule.VIDEOCOPYRIGHT.getMsg());
                    break;
            }
wzp's avatar
wzp committed
272
        }
273 274 275 276 277
    }

    /**
     * 是否存在注解,如果存在就获取
     */
liqin's avatar
liqin committed
278
    private MethodLog getAnnotationLog(JoinPoint joinPoint) {
279 280 281 282 283 284 285 286 287 288
        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
289 290
    protected Map<String, Object> getFailResult() {
        Map<String, Object> map = new HashMap<>();
wzp's avatar
wzp committed
291 292
        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
293 294 295
        return map;
    }

296 297 298 299 300 301
    /**
     * 方法异常时调用
     *
     * @param
     */
    @AfterThrowing("methodCachePointcut()")
liqin's avatar
liqin committed
302
    public void afterThrowing(JoinPoint point) {
303 304 305 306 307 308 309 310 311 312

    }

    /**
     * 获取请求ip
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
wzp's avatar
wzp committed
313
//        String ip = request.getHeader("x-forwarded-for");
wzp's avatar
wzp committed
314
        String ip = ip = request.getRemoteAddr();
315 316 317 318 319 320 321 322 323 324 325 326 327
        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
328

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