SystemOperationLogService.java 14.1 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())) {
wzp's avatar
wzp committed
110
            TBoxOperation t = (TBoxOperation) method_param[0];
wzp's avatar
wzp committed
111 112 113
            TOperationLog operationLog = new TOperationLog();
            operationLog.setCreateTime(LocalDateTime.now());
            operationLog.setUserId(user.getId());
liqin's avatar
liqin committed
114
            if (user.getAreaName() != null) {
wzp's avatar
wzp committed
115 116
                operationLog.setArea(user.getAreaName());
            }
wzp's avatar
wzp committed
117
            if (OperType.ACTIVATION.getMsg().equals(methodLog.operType().getMsg())&&3==(t.getStatus())){
wzp's avatar
wzp committed
118 119
                    operationLog.setOperationType(OperType.FAULT.getMsg());
            }else {
wzp's avatar
wzp committed
120
                operationLog.setOperationType(OperType.ACTIVATION.getMsg());
wzp's avatar
wzp committed
121
            }
122
            operationLogService.save(operationLog);
liqin's avatar
liqin committed
123
            return method_param;
wzp's avatar
wzp committed
124
        }
125
        SysLog sysLog = new SysLog();
liqin's avatar
liqin committed
126
        // todo null
wzp's avatar
wzp committed
127
        if (user.getRoleList().contains("1")) {
128
            sysLog.setType(1);
wzp's avatar
wzp committed
129
        } else {
130 131 132 133
            sysLog.setType(2);
        }
        sysLog.setOperator(user.getUserName());
        sysLog.setOperationIp(ip);
wzp's avatar
wzp committed
134
//        sysLog.setOperationContent(methodName);
135 136
        sysLog.setOperationTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        // 处理设置注解上的参数
wzp's avatar
wzp committed
137
        getControllerMethodDescription(user, methodLog, sysLog, method_param);
liqin's avatar
liqin committed
138
        sysLogMapper.insert(sysLog);
liqin's avatar
liqin committed
139
        return method_param;
140 141 142 143 144
    }

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

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

289 290 291 292 293 294
    /**
     * 方法异常时调用
     *
     * @param
     */
    @AfterThrowing("methodCachePointcut()")
liqin's avatar
liqin committed
295
    public void afterThrowing(JoinPoint point) {
296 297 298 299 300 301 302 303 304 305

    }

    /**
     * 获取请求ip
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
wzp's avatar
wzp committed
306
//        String ip = request.getHeader("x-forwarded-for");
wzp's avatar
wzp committed
307
        String ip = ip = request.getRemoteAddr();
308 309 310 311 312 313 314 315 316 317 318 319 320
        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
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335
            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;
    }
}