luofl
2025-02-23 0facef68a06fb36cc1ac12730b076cead36348f9
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.panzhihua.common.controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.panzhihua.common.model.vos.community.ComActVO;
import com.panzhihua.common.model.vos.sangeshenbian.SystemUserVo;
import com.panzhihua.common.utlis.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.constants.Constants;
import com.panzhihua.common.constants.TokenConstant;
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.exceptions.UnAuthenticationException;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import com.panzhihua.common.utlis.AES;
 
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
 
import static java.util.Objects.isNull;
import static org.apache.commons.lang3.StringUtils.isBlank;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 基础controller
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-24 09:31
 **/
@Slf4j
public class BaseController {
    public static void main(String[] args) {
        byte[] bytes = AES.parseHexStr2Byte(
            "5730CB290AD203B32D2FA4347CD54A50A684E16D2C7A4544CF352D54D8A763BA6652C323B13BEEE682A5095B8BCC76842225A914253CE84AFB6B58A9D1BE30A7992786E151A933FDEC6EBDCD1928CA68927047770F296F7541268149B34FDFCEF1340CF2266A041484744CB45B216EE8B4D96E243098F9CB3F661ACA7AF356C422BFE9A009FA478DF606A71DA7DBD776527E5120C3F13281BA8FC5587BACD5E403EE069762B5CF060BFA3CF4FD3417F5653CC178D2CA117284D85F442C0FAB076DB6F6D873B5363FAB93EBA9284CC87A5E97243EF4DC73F1ADA57BD9E9AAB2E4972AC3A4615B2DF6F9062BC04428D1B84B6C892A306F191B1D9DDBBB201D2767CE928020489AFC6BCFDC8A74F8C95080F74B4EC64AD1A6354B17A156B95AA9467C7461BD3C3F15F8F65BD7F3272184435D77003738B5B942EEA9603CFF249764718732595EC44DE4ED1BB763F9C88BAFACF5540E689FE84A4702E31D3D1D05BAC25C10F8E0C3948C777478537397BCB67D37066385DFCBDE3F4A53D6BA14481DBCB07E8452ACD2790A76669A9DE6D7080C8CFD9FB774035FCC3AC1C436581144969B7150318024A2E893FE926654E002097EB4735E49F3A9698A405969888A7D88A55A0FA60318C4A1E2D243BEE6D438");
        byte[] decrypt = AES.decrypt(bytes, Constants.AES_KEY);
        System.out.println(decrypt);
        String s = new String(decrypt);
        System.out.println(s);
    }
 
    /**
     * 获取request对象
     */
    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }
 
 
    /**
     * 获取request对象
     */
    public HttpServletResponse getResponse() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
    }
 
    /**
     * 获取登录对象信息
     *
     * @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();
//        if (null == communityId) {
//            throw new ServiceException("用户未绑定社区");
//        }
        return communityId;
    }
 
    /**
     * 获取登录对象所在社区名称
     *
     * @return 社区名称
     */
    public String getCommunityName() {
        LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
        Long communityId = loginUserInfo.getCommunityId();
        if (null == communityId || 0 == communityId) {
            throw new ServiceException("用户未绑定社区");
        }
        String communityName = loginUserInfo.getCommunityName();
        return communityName;
    }
 
    /**
     * 获取登录对象所在小区
     *
     * @return 小区id
     */
    public Long getAreaId() {
        LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
        Long areaId = loginUserInfo.getAreaId();
        if (null == areaId) {
            throw new ServiceException("用户未绑定小区");
        }
        return areaId;
    }
    /**
     * 获取登录对象所在区域编码
     *
     * @return
     */
    public String getAreaCode() {
        String appid = this.getRequest().getHeader("appid");
        if(StringUtils.isNotEmpty(appid)){
            if(appid.equals("wx08932ba29546ff82")){
                return "510411";
            }
            else if(appid.equals("wx50d8c395af50481b")){
                return "510402";
            }
            else {
                return "510423";
            }
        } else {
            LoginUserInfoVO loginUserInfoVO=this.getLoginUserInfo();
            ComActVO comActVO=loginUserInfoVO.getComActVO();
            if(isNull(comActVO) || isBlank(comActVO.getAreaCode())){
                return "510423";
            }
            return comActVO.getAreaCode();
        }
    }
 
 
    /**
     * 获取登录token
     *
     * @return token
     */
    public String getToken() {
        HttpServletRequest request = this.getRequest();
        String header = request.getHeader(TokenConstant.TOKEN_LOGOUT);
        return header;
    }
 
    public String getAppId(){
        String appid = this.getRequest().getHeader("appid");
        if(StringUtils.isEmpty(appid)){
            return "wx0cef797390444b75";
        }
        return appid;
    }
 
    public String getAppSecret(){
        LoginUserInfoVO loginUserInfo = this.getLoginUserInfo();
        return loginUserInfo.getAppSecret();
    }
    /**
     * 获取登录对象所有信息
     *
     * @return 所有信息
     */
    @SneakyThrows
    public LoginUserInfoVO getLoginUserInfo() {
        HttpServletRequest request = this.getRequest();
        String userInfo = request.getHeader(TokenConstant.TOKEN_USERINFO);
        boolean empty = ObjectUtils.isEmpty(userInfo);
        if (empty) {
            throw new UnAuthenticationException("获取登录人信息失败");
        }
        // log.info("userInfo【{}】",userInfo);
        byte[] bytes = AES.parseHexStr2Byte(userInfo);
        // log.info("bytes【{}】",bytes);
        byte[] decrypt = AES.decrypt(bytes, Constants.AES_KEY);
        // log.info("decrypt【{}】",decrypt);
        userInfo = new String(decrypt);
        LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(userInfo, LoginUserInfoVO.class);
        return loginUserInfoVO;
    }
 
    @SneakyThrows
    public LoginUserInfoVO getLoginUserInfoSureNoLogin() {
        HttpServletRequest request = this.getRequest();
        String userInfo = request.getHeader(TokenConstant.TOKEN_USERINFO);
        boolean empty = ObjectUtils.isEmpty(userInfo);
        if (empty) {
            return null;
            // throw new UnAuthenticationException("获取登录人信息失败");
        }
        // log.info("userInfo【{}】",userInfo);
        byte[] bytes = AES.parseHexStr2Byte(userInfo);
        // log.info("bytes【{}】",bytes);
        byte[] decrypt = AES.decrypt(bytes, Constants.AES_KEY);
        // log.info("decrypt【{}】",decrypt);
        userInfo = new String(decrypt);
        LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(userInfo, LoginUserInfoVO.class);
        return loginUserInfoVO;
    }
    
    
    /**
     * 获取三个身边管理后台登录用户信息
     * @return
     */
    @SneakyThrows
    public SystemUserVo getLoginUserInfoSanGeShenBian() {
        HttpServletRequest request = this.getRequest();
        String userInfo = request.getHeader(TokenConstant.TOKEN_USERINFO);
        boolean empty = ObjectUtils.isEmpty(userInfo);
        if (empty) {
            return null;
        }
        byte[] bytes = AES.parseHexStr2Byte(userInfo);
        byte[] decrypt = AES.decrypt(bytes, Constants.AES_KEY);
        userInfo = new String(decrypt);
        SystemUserVo loginUserInfoVO = JSONObject.parseObject(userInfo, SystemUserVo.class);
        return loginUserInfoVO;
    }
}