|  |  |  | 
|---|
|  |  |  | import com.moral.api.entity.ManageAccount; | 
|---|
|  |  |  | import com.moral.api.entity.ManageLog; | 
|---|
|  |  |  | import com.moral.api.mapper.ManageLogMapper; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.login.AccountInfoDTO; | 
|---|
|  |  |  | import com.moral.api.pojo.redisBean.AccountInfoDTO; | 
|---|
|  |  |  | import com.moral.api.service.UnitConversionService; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.util.TokenUtils; | 
|---|
|  |  |  | import com.moral.util.WebUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
|---|
|  |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import java.util.Scanner; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @ClassName LogUtils | 
|---|
|  |  |  | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | public class LogUtils { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | public ManageLogMapper manageLogMapper; | 
|---|
|  |  |  | private static ManageLogMapper manageLogMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public void saveOperationForManage(HttpServletRequest request, String content) { | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | public void setManageLogMapper(ManageLogMapper manageLogMapper){LogUtils.manageLogMapper = manageLogMapper;} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void saveOperationForManage(String content,String type){ | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | saveOperationForManage(request,content,type); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 通用插入日志 | 
|---|
|  |  |  | * @Param: [request, content] | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/4/8 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void saveOperationForManage(HttpServletRequest request, String content,String type) { | 
|---|
|  |  |  | String token = request.getHeader("token"); | 
|---|
|  |  |  | AccountInfoDTO accountInfoDTO = (AccountInfoDTO) TokenUtils.getUserInfoByToken(token); | 
|---|
|  |  |  | ManageAccount account = accountInfoDTO.getAccount(); | 
|---|
|  |  |  | Integer id = account.getId(); | 
|---|
|  |  |  | String userName = account.getUserName(); | 
|---|
|  |  |  | ManageAccount manageAccount = accountInfoDTO.getAccount(); | 
|---|
|  |  |  | Integer id = manageAccount.getId(); | 
|---|
|  |  |  | String userName = manageAccount.getUserName(); | 
|---|
|  |  |  | String account = manageAccount.getAccount(); | 
|---|
|  |  |  | String ip = WebUtils.getIpAddr(request); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ManageLog log = new ManageLog(); | 
|---|
|  |  |  | log.setType(type); | 
|---|
|  |  |  | log.setUserName(userName); | 
|---|
|  |  |  | log.setAccountId(id); | 
|---|
|  |  |  | log.setIp(ip); | 
|---|
|  |  |  | log.setAccount(account); | 
|---|
|  |  |  | log.setContent(content); | 
|---|
|  |  |  | log.setUserName(userName); | 
|---|
|  |  |  | manageLogMapper.insert(log); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 插入登陆操作日志 | 
|---|
|  |  |  | * @Param: [request, content, id, userName] | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/4/8 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void saveLoginForManage(HttpServletRequest request,String content,ManageAccount account,String userName){ | 
|---|
|  |  |  | ManageLog log = new ManageLog(); | 
|---|
|  |  |  | String ip = WebUtils.getIpAddr(request); | 
|---|
|  |  |  | log.setAccountId(account.getId()); | 
|---|
|  |  |  | log.setAccount(account.getAccount()); | 
|---|
|  |  |  | log.setUserName(userName); | 
|---|
|  |  |  | log.setIp(ip); | 
|---|
|  |  |  | log.setContent(content); | 
|---|
|  |  |  | log.setType(Constants.LOGIN_OPERTATE_TYPE); | 
|---|
|  |  |  | manageLogMapper.insert(log); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 存储token解析失败的日志 | 
|---|
|  |  |  | * @Param: [request, content] | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/10/8 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void saveFailedRequestForManage(HttpServletRequest request,String content){ | 
|---|
|  |  |  | ManageLog log = new ManageLog(); | 
|---|
|  |  |  | String ip = WebUtils.getIpAddr(request); | 
|---|
|  |  |  | log.setAccountId(0); | 
|---|
|  |  |  | log.setAccount("null"); | 
|---|
|  |  |  | log.setUserName("null"); | 
|---|
|  |  |  | log.setIp(ip); | 
|---|
|  |  |  | log.setContent(content); | 
|---|
|  |  |  | log.setType(Constants.LOGIN_OPERTATE_TYPE); | 
|---|
|  |  |  | manageLogMapper.insert(log); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|