package com.sandu.common.log;
|
|
import cn.hutool.extra.servlet.ServletUtil;
|
import cn.hutool.http.useragent.UserAgent;
|
import cn.hutool.http.useragent.UserAgentInfo;
|
import cn.hutool.http.useragent.UserAgentUtil;
|
import com.sandu.common.security.SecurityUtils;
|
import com.sandu.common.service.impl.BaseServiceImpl;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.springframework.stereotype.Service;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.lang.reflect.Method;
|
import java.util.Optional;
|
|
/**
|
* @author hupeng
|
* @date 2018-11-24
|
*/
|
@Service
|
public class LogService extends BaseServiceImpl<LogDao, AccessLog> implements LogServiceImpl {
|
|
@Override
|
public void saveOperation(HttpServletRequest request, ProceedingJoinPoint joinPoint, AccessLog accessLog) {
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
Method method = signature.getMethod();
|
Log aopLog = method.getAnnotation(Log.class);
|
|
// 方法路径
|
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
|
|
StringBuilder params = new StringBuilder("{");
|
//参数值
|
Object[] argValues = joinPoint.getArgs();
|
//参数名称
|
String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
|
if (argValues != null) {
|
for (int i = 0; i < argValues.length; i++) {
|
params.append(" ").append(argNames[i]).append(": ").append(argValues[i]);
|
}
|
}
|
accessLog.setDescription(aopLog.value());
|
//类型 0-后台 1-前台
|
accessLog.setType(aopLog.type());
|
accessLog.setUid(SecurityUtils.getUserId());
|
|
UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
accessLog.setBrowser(Optional.ofNullable(userAgent).map(UserAgent::getBrowser).map(UserAgentInfo::getName).orElse(""));
|
accessLog.setRequestIp(ServletUtil.getClientIP(request));
|
|
accessLog.setMethod(methodName);
|
accessLog.setUsername(SecurityUtils.getUsername());
|
accessLog.setParams(params.toString() + " }");
|
this.save(accessLog);
|
}
|
|
}
|