Commit 61f71cf7 authored by 李驰骋's avatar 李驰骋

接口日志模块添加

parent 56fce6b3
......@@ -15,7 +15,7 @@ import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* 操作日志记录
* 接口日志记录
*
*/
@RestController
......@@ -50,4 +50,12 @@ public class SysApiLogController extends BaseController
operLogService.cleanApiLog();
return AjaxResult.success();
}
@Log(title = "再次执行", businessType = BusinessType.OTHER)
@PreAuthorize("@ss.hasPermi('monitor:apilog:remove')")
@PostMapping("/execute")
public AjaxResult execute(Long[] logIds) throws Exception {
operLogService.executeByIds(logIds);
return AjaxResult.success();
}
}
package com.ximai.mes.config;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.StrJoiner;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.ximai.common.annotation.ApiLog;
import com.ximai.common.annotation.Log;
import com.ximai.common.enums.BusinessStatus;
......@@ -165,26 +168,31 @@ public class ApiLogAspect {
* @param operLog 操作日志
* @throws Exception 异常
*/
private void setRequestValue(JoinPoint joinPoint, SysApiLog operLog) throws Exception {
String params = argsArrayToString(joinPoint.getArgs());
operLog.setOperParam(params);
private void setRequestValue(JoinPoint joinPoint, SysApiLog operLog) {
argsArrayToString(operLog, joinPoint.getArgs());
}
/**
* 参数拼装
*/
private String argsArrayToString(Object[] paramsArray) {
private String argsArrayToString(SysApiLog operLog, Object[] paramsArray) {
String params = "";
if (paramsArray != null && paramsArray.length > 0) {
int i=1;
StrJoiner joiner = StrJoiner.of(",");
for (Object o : paramsArray) {
joiner.append(o.getClass().getName());
if (StringUtils.isNotNull(o) && !isFilterObject(o)) {
try {
Object jsonObj = JSON.toJSON(o);
params += jsonObj.toString() + " ";
BeanUtil.setFieldValue(operLog,"operParam"+i, jsonObj);
} catch (Exception e) {
e.printStackTrace();
}
}
i++;
}
operLog.setParamType(joiner.toString());
}
return params.trim();
}
......
package com.ximai.system.domain;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ximai.common.annotation.Excel;
import com.ximai.common.annotation.Excel.ColumnType;
import com.ximai.common.core.domain.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ximai.common.exception.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 操作日志记录表 oper_log
*/
@Data
public class SysApiLog extends BaseEntity {
private static final long serialVersionUID = 1L;
......@@ -84,11 +93,22 @@ public class SysApiLog extends BaseEntity {
@Excel(name = "操作地点")
private String operLocation;
@ApiModelProperty(value = "方法参数1")
private String operParam1;
@ApiModelProperty(value = "方法参数2")
private String operParam2;
@ApiModelProperty(value = "方法参数3")
private String operParam3;
@ApiModelProperty(value = "方法参数4")
private String operParam4;
@ApiModelProperty(value = "方法参数5")
private String operParam5;
/**
* 请求参数
*/
@Excel(name = "请求参数")
private String operParam;
@ApiModelProperty(value = "参数类型")
private String paramType;
/**
* 返回参数
......@@ -115,139 +135,35 @@ public class SysApiLog extends BaseEntity {
@Excel(name = "操作时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date operTime;
public Long getOperId() {
return operId;
}
public void setOperId(Long operId) {
this.operId = operId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getBusinessType() {
return businessType;
}
public void setBusinessType(Integer businessType) {
this.businessType = businessType;
}
public Integer[] getBusinessTypes() {
return businessTypes;
}
public void setBusinessTypes(Integer[] businessTypes) {
this.businessTypes = businessTypes;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getRequestMethod() {
return requestMethod;
}
public void setRequestMethod(String requestMethod) {
this.requestMethod = requestMethod;
}
public Integer getOperatorType() {
return operatorType;
}
public void setOperatorType(Integer operatorType) {
this.operatorType = operatorType;
}
public String getOperName() {
return operName;
}
public void setOperName(String operName) {
this.operName = operName;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getOperUrl() {
return operUrl;
}
public void setOperUrl(String operUrl) {
this.operUrl = operUrl;
}
public String getOperIp() {
return operIp;
}
public void setOperIp(String operIp) {
this.operIp = operIp;
}
public String getOperLocation() {
return operLocation;
}
public void setOperLocation(String operLocation) {
this.operLocation = operLocation;
}
public String getOperParam() {
return operParam;
}
public void setOperParam(String operParam) {
this.operParam = operParam;
}
public String getJsonResult() {
return jsonResult;
}
public void setJsonResult(String jsonResult) {
this.jsonResult = jsonResult;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public Date getOperTime() {
return operTime;
}
public void setOperTime(Date operTime) {
this.operTime = operTime;
public Object[] parseParamObject() throws ClassNotFoundException {
List<String> params = new ArrayList<String>();
if(StringUtils.isNotEmpty(this.getOperParam1())){
params.add(this.getOperParam1());
}
if(StringUtils.isNotEmpty(this.getOperParam2())){
params.add(this.getOperParam2());
}
if(StringUtils.isNotEmpty(this.getOperParam3())){
params.add(this.getOperParam3());
}
if(StringUtils.isNotEmpty(this.getOperParam4())){
params.add(this.getOperParam4());
}
if(StringUtils.isNotEmpty(this.getOperParam5())){
params.add(this.getOperParam5());
}
String[] paramTypeArr = this.getParamType().split(",");
List<Object> paramsObj = new ArrayList<>();
int i=0;
if(paramTypeArr.length!=params.size()){
throw new ServiceException("参数不致,检查方法是否发生变化");
}
for (String param : params) {
Class clz = Class.forName(paramTypeArr[i]);
Object obj1 = JSONUtil.toBean(param, clz);
paramsObj.add(obj1);
i++;
}
return paramsObj.toArray();
}
}
......@@ -43,4 +43,11 @@ public interface ISysApiLogService {
* 清空操作日志
*/
void cleanApiLog();
/**
* 执行日志记录相关操作
* @param logIds
* @throws Exception
*/
void executeByIds(Long[] logIds) throws Exception;
}
......@@ -3,18 +3,24 @@ package com.ximai.system.service.impl;
import com.ximai.system.domain.SysApiLog;
import com.ximai.system.mapper.SysApiLogMapper;
import com.ximai.system.service.ISysApiLogService;
import org.apache.commons.beanutils.MethodUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
/**
* 操作日志 服务层处理
* 接口日志 服务层处理
*/
@Service
public class SysApiLogServiceImpl implements ISysApiLogService {
@Autowired
private SysApiLogMapper operApiMapper;
@Autowired
private ApplicationContext applicationContext;
/**
* 新增操作日志
......@@ -23,6 +29,7 @@ public class SysApiLogServiceImpl implements ISysApiLogService {
*/
@Override
public void insertApiLog(SysApiLog operLog) {
operLog.setCreateTime(new Date());
operApiMapper.insertApiLog(operLog);
}
......@@ -66,4 +73,19 @@ public class SysApiLogServiceImpl implements ISysApiLogService {
public void cleanApiLog() {
operApiMapper.cleanApiLog();
}
@Override
public void executeByIds(Long[] logIds) throws Exception {
for(Long logid : logIds){
SysApiLog log = this.selectApiLogById(logid);
String clzStr = log.getMethod();
String clz = clzStr.substring(0,clzStr.lastIndexOf("."));
String methodStr = clzStr.substring(clzStr.lastIndexOf(".")+1);
Object obj = applicationContext.getBean(Class.forName(clz));
//参数转换
Object[] params = log.parseParamObject();
MethodUtils.invokeMethod(obj, methodStr.replace("()",""), params);
}
}
}
......@@ -16,7 +16,12 @@
<result property="operUrl" column="oper_url"/>
<result property="operIp" column="oper_ip"/>
<result property="operLocation" column="oper_location"/>
<result property="operParam" column="oper_param"/>
<result property="operParam1" column="oper_param1"/>
<result property="operParam2" column="oper_param2"/>
<result property="operParam3" column="oper_param3"/>
<result property="operParam4" column="oper_param4"/>
<result property="operParam5" column="oper_param5"/>
<result property="paramType" column="param_type"/>
<result property="jsonResult" column="json_result"/>
<result property="status" column="status"/>
<result property="errorMsg" column="error_msg"/>
......@@ -35,7 +40,12 @@
oper_url,
oper_ip,
oper_location,
oper_param,
oper_param1,
oper_param2,
oper_param3,
oper_param4,
oper_param5,
param_type,
json_result,
status,
error_msg,
......@@ -45,9 +55,9 @@
<insert id="insertApiLog" parameterType="SysApiLog">
insert into sys_api_log(title, business_type, method, request_method, operator_type, oper_name, dept_name,
oper_url, oper_ip, oper_location, oper_param, json_result, status, error_msg, oper_time)
oper_url, oper_ip, oper_location, oper_param1, oper_param2, oper_param3, oper_param4, oper_param5, param_type, json_result, status, error_msg, oper_time)
values (#{title}, #{businessType}, #{method}, #{requestMethod}, #{operatorType}, #{operName}, #{deptName},
#{operUrl}, #{operIp}, #{operLocation}, #{operParam}, #{jsonResult}, #{status}, #{errorMsg}, sysdate())
#{operUrl}, #{operIp}, #{operLocation}, #{operParam1}, #{operParam2}, #{operParam3}, #{operParam4}, #{operParam5}, #{paramType}, #{jsonResult}, #{status}, #{errorMsg}, #{createTime})
</insert>
<select id="selectApiLogList" parameterType="SysApiLog" resultMap="SysApiLogResult">
......
......@@ -236,6 +236,8 @@
<if test="menuId != null and menuId != 0">#{menuId},</if>
<if test="parentId != null and parentId != 0">#{parentId},</if>
<if test="menuName != null and menuName != ''">#{menuName},</if>
<if test="menuNameEn != null and menuNameEn != ''">#{menuNameEn},</if>
<if test="menuNameTh != null and menuNameTh != ''">#{menuNameTh},</if>
<if test="orderNum != null and orderNum != ''">#{orderNum},</if>
<if test="path != null and path != ''">#{path},</if>
<if test="component != null and component != ''">#{component},</if>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment