whycxzp
7 天以前 08879b1fb5f25265b22becd95a31ce9c8709b8ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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);
    }
 
 
}