| | |
| | | package com.moral.api.aop.log; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.moral.api.entity.ManageLog; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.mapper.ManageLogMapper; |
| | | import com.moral.api.mapper.OrganizationMapper; |
| | | import com.moral.api.pojo.dto.login.AccountInfoDTO; |
| | | import com.moral.api.pojo.form.organization.OrganizationDeleteForm; |
| | | import com.moral.api.pojo.form.organization.OrganizationInsertForm; |
| | | import com.moral.api.pojo.form.organization.OrganizationUpdateForm; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import com.moral.util.TokenUtils; |
| | | import com.moral.util.WebUtils; |
| | | import lombok.Data; |
| | | import org.aspectj.lang.JoinPoint; |
| | | import org.aspectj.lang.annotation.AfterReturning; |
| | | 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.beans.factory.annotation.Value; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.context.request.RequestAttributes; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.lang.reflect.Method; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @ClassName OperationLogAspect |
| | |
| | | **/ |
| | | @Aspect |
| | | @Component |
| | | @ConfigurationProperties(prefix = "log-aspect") |
| | | @Data |
| | | public class OperationLogAspect { |
| | | |
| | | @Autowired |
| | | ManageLogMapper manageLogMapper; |
| | | |
| | | @Autowired |
| | | OrganizationMapper organizationMapper; |
| | | |
| | | Map<String,String> organizationFormMap; |
| | | |
| | | |
| | | /** |
| | | * @Description: 设置切入点为OperationLogAnno注解使用的地方 |
| | |
| | | * @Date: 2021/3/31 |
| | | */ |
| | | @AfterReturning(returning = "result", value = "pointCut()") |
| | | @Transactional |
| | | public void saveOperLog(JoinPoint joinPoint, Object result) throws Throwable { |
| | | //判断操作是否成功,失败不计入日志 |
| | | Map resultMap = JSONObject.parseObject(JSONObject.toJSONString(result), Map.class); |
| | | if (!ResponseCodeEnum.SUCCESS.getCode().equals(resultMap.get("code"))) |
| | | return; |
| | | //获取请求参数 |
| | | RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); |
| | | HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); |
| | | Object parameters = request.getAttribute("parameters"); |
| | | String token = request.getHeader("token"); |
| | | //获取module和type |
| | | MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获取切入点方法信号 |
| | | Method method = signature.getMethod();//获取切入点的方法 |
| | | OperationLogAnno anno = method.getAnnotation(OperationLogAnno.class);//获取方法的注解 |
| | | String module = anno.module();//获取操作的模块 |
| | | String type = anno.type();//获取操作的类型 |
| | | saveLog(request,parameters, module, type); |
| | | //获取操作用户id ip 名称 |
| | | Integer id = Integer.parseInt(TokenUtils.getUidByToken(token)); |
| | | String ip = WebUtils.getIpAddr(request); |
| | | AccountInfoDTO account = (AccountInfoDTO) TokenUtils.getUserInfoByToken(token); |
| | | String accountName = account.getAccount().getUserName(); |
| | | //获取content |
| | | String content = getContent(type,module,parameters); |
| | | content = content+"操作人员:"+accountName+";"; |
| | | //存入数据库 |
| | | ManageLog manageLog = new ManageLog(); |
| | | manageLog.setContent(content); |
| | | manageLog.setIp(ip); |
| | | manageLog.setAccountId(id); |
| | | manageLogMapper.insert(manageLog); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @Description: 判断模块 |
| | | * @Param: [paramters, module, type] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/31 |
| | | */ |
| | | public void saveLog(HttpServletRequest request,Object paramters, String module, String type) { |
| | | public String getContent(String type, String module,Object parameters) { |
| | | String content = ""; |
| | | if (module.equals(Constants.ORGANIZATION_MODULE_NAME)) { |
| | | saveOrganizationLog(request,paramters, module, type); |
| | | content = getOrganizationContent(type,parameters); |
| | | } |
| | | |
| | | return content; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 处理组织日志 |
| | | * @Param: [paramters, module, type] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/31 |
| | | */ |
| | | @Transactional |
| | | public void saveOrganizationLog(HttpServletRequest request,Object paramters, String module, String type) { |
| | | * @Description: 获取组织模块操作描述 |
| | | * @Param: [type, parameters] |
| | | * @return: java.lang.String |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/4/1 |
| | | */ |
| | | public String getOrganizationContent(String type,Object parameters) { |
| | | StringBuilder content = new StringBuilder(); |
| | | if (type.equals(Constants.INSERT_OPERATE_TYPE)) {//处理组织添加日志插入 |
| | | OrganizationInsertForm form = JSON.parseObject(JSON.toJSONString(paramters), OrganizationInsertForm.class); |
| | | Integer accountId = 1;//固定 尚未更改 |
| | | String accountName = "测试人员"; |
| | | OrganizationInsertForm form = JSON.parseObject(JSON.toJSONString(parameters), OrganizationInsertForm.class); |
| | | String organizationName = form.getName(); |
| | | String ip = WebUtils.getIpAddr(request); |
| | | String content = accountName+"添加了新组织:"+organizationName; |
| | | |
| | | ManageLog manageLog = new ManageLog(); |
| | | manageLog.setContent(content); |
| | | manageLog.setIp(ip); |
| | | manageLog.setAccountId(accountId); |
| | | manageLog.setModule(module); |
| | | manageLog.setType(type); |
| | | manageLogMapper.insert(manageLog); |
| | | content.append("添加了组织:").append(organizationName).append(";"); |
| | | } else if (type.equals(Constants.DELETE_OPERATE_TYPE)) { //处理组织删除日志 |
| | | |
| | | OrganizationDeleteForm form = JSON.parseObject(JSON.toJSONString(parameters), OrganizationDeleteForm.class); |
| | | Organization organization = organizationMapper.selectById(form.getOrganizationId()); |
| | | String organizationName = organization.getName(); |
| | | content.append("删除了组织:").append(organizationName).append(";"); |
| | | } else if (type.equals(Constants.UPDATE_OPERATE_TYPE)) { //处理组织更新日志 |
| | | |
| | | OrganizationUpdateForm form = JSON.parseObject(JSON.toJSONString(parameters), OrganizationUpdateForm.class); |
| | | Map<String,Object> formMap = JSON.parseObject(JSON.toJSONString(parameters), Map.class); |
| | | Organization organization = organizationMapper.selectById(form.getOrganizationId()); |
| | | String organizationName = organization.getName(); |
| | | content.append("更新了组织:").append(organizationName).append(";"); |
| | | //将formMap中的Key全部替换为中文写入日志 |
| | | formMap.forEach((k,v)->{ |
| | | String key = organizationFormMap.get(k); |
| | | if(key!=null) |
| | | content.append(key).append("->").append(v).append(";"); |
| | | }); |
| | | } |
| | | |
| | | return content.toString(); |
| | | } |
| | | |
| | | |
| | | } |