package com.whyc.util;
|
|
import com.whyc.constant.YamlProperties;
|
import com.whyc.pojo.db_user.User;
|
import com.whyc.service.UserLogService;
|
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.subject.Subject;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.system.ApplicationHome;
|
import org.springframework.stereotype.Component;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.File;
|
|
import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;
|
|
/**
|
* 通用工具列
|
*/
|
@Component
|
public class CommonUtil {
|
|
private static UserLogService userLogService;
|
|
@Autowired
|
public void setUserLogService(UserLogService userLogService) {
|
CommonUtil.userLogService = userLogService;
|
}
|
|
/**获取当前Session中的属性user*/
|
public static User getUser(HttpServletRequest request) {
|
return (User) request.getSession().getAttribute("user");
|
}
|
|
/**
|
* Shiro框架下的用户获取
|
* @return
|
*/
|
public static User getUser() {
|
User user;
|
Subject currentUser = SecurityUtils.getSubject();
|
if(currentUser!=null && currentUser.isAuthenticated()){
|
user = (User) currentUser.getPrincipal();
|
}else{
|
user = new User();
|
user.setName("unlogged_user");
|
user.setId(0);
|
user.setRole(1);
|
}
|
return user;
|
}
|
|
public static String classesPath(){
|
ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
|
File jarFile = applicationHome.getDir();
|
return jarFile.toString();
|
}
|
|
public static String getRootFile(){
|
ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
|
File jarFile = applicationHome.getDir();
|
String baseDirPath;
|
if(YamlProperties.runModel == 1) {
|
//开发路径
|
baseDirPath = jarFile.getParentFile().toString()+File.separator+"pis_file"+File.separator;
|
}else {
|
//打包路径
|
baseDirPath = jarFile.toString()+File.separator+"pis_file"+File.separator;
|
}
|
return baseDirPath;
|
}
|
|
/**
|
* 获取项目所在文件夹路径
|
* @return 获取项目所在文件夹路径
|
*/
|
public static String getProjectDir(){
|
ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
|
File jarFile = applicationHome.getDir();
|
String baseDirPath;
|
if (YamlProperties.runModel == 1) {
|
//开发路径
|
baseDirPath = jarFile.getParentFile().toString();
|
} else {
|
//打包路径
|
baseDirPath = jarFile.toString();
|
}
|
return baseDirPath;
|
}
|
|
/**
|
* 手动记录特定日志
|
*/
|
public static void record(int category, int type, String message,String messageDetail) {
|
userLogService.add(category, type, message, messageDetail);
|
}
|
|
|
}
|