huanghongfa
2020-12-07 f404947d2563df3436a9cf31ede8804ebf860b39
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
package com.panzhihua.common.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.constants.TokenConstant;
import com.panzhihua.common.constants.UserConstants;
import com.panzhihua.common.exceptions.UnAuthenticationException;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import io.swagger.models.auth.In;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 基础controller
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-24 09:31
 **/
public class BaseController {
    /**
     * 获取request对象
     */
    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
 
    /**
     * 获取登录对象信息
     * @return 对象userid
     */
    public Long getUserId(){
        LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
        Long userId = loginUserInfo.getUserId();
        return userId;
    }
 
    /**
     * 获取登录对象所在社区id
     * @return 社区id
     */
    public Long getCommunityId(){
        LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
        Long communityId = loginUserInfo.getCommunityId();
        return communityId;
    }
 
    /**
     * 获取登录token
     * @return token
     */
    public String getToken(){
        HttpServletRequest request = this.getRequest();
        String header = request.getHeader(TokenConstant.TOKEN_LOGOUT);
        return header;
    }
 
    /**
     * 获取登录对象所有信息
     * @return 所有信息
     */
    public LoginUserInfoVO getLoginUserInfo(){
        HttpServletRequest request = this.getRequest();
        String userInfo = request.getHeader(UserConstants.LOGIN_USER_INFO);
        boolean empty = ObjectUtils.isEmpty(userInfo);
        if (empty) {
            throw new UnAuthenticationException("获取登录人信息失败");
        }
        LoginUserInfoVO loginUserInfoVO= JSONObject.parseObject(userInfo,LoginUserInfoVO.class);
        return loginUserInfoVO;
    }
 
}