BaseController.java 8.05 KB
Newer Older
liqin's avatar
liqin committed
1
package cn.chnmuseum.party.web.controller.base;
liqin's avatar
liqin committed
2

liqin's avatar
liqin committed
3 4 5 6 7
import cn.chnmuseum.party.auth.util.JwtTokenUtil;
import cn.chnmuseum.party.common.enums.RESPONSE_CODE_ENUM;
import cn.chnmuseum.party.common.enums.RESULT_INFO_ENUM;
import cn.chnmuseum.party.model.TUser;
import cn.chnmuseum.party.service.TUserService;
liqin's avatar
liqin committed
8 9 10 11 12
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
wzp's avatar
wzp committed
13
import org.apache.shiro.authc.AuthenticationException;
liqin's avatar
liqin committed
14 15 16 17
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
liqin's avatar
liqin committed
18
import javax.servlet.http.HttpServletRequest;
liqin's avatar
liqin committed
19
import java.io.Serializable;
liqin's avatar
liqin committed
20
import java.util.LinkedHashMap;
liqin's avatar
liqin committed
21
import java.util.List;
liqin's avatar
liqin committed
22
import java.util.Map;
liqin's avatar
liqin committed
23

liqin's avatar
liqin committed
24 25 26 27
/**
 * controller 基类控制器
 */
public class BaseController implements Serializable {
liqin's avatar
liqin committed
28

liqin's avatar
liqin committed
29 30
    private static final long serialVersionUID = 624841049563451448L;
    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);
liqin's avatar
liqin committed
31

32

liqin's avatar
liqin committed
33 34
    @Resource
    protected HttpServletRequest request;
liqin's avatar
liqin committed
35

36
    @Resource
liqin's avatar
liqin committed
37
    protected TUserService userService;
38

liqin's avatar
liqin committed
39 40 41 42 43 44 45
    /**
     * <p>
     * 获取分页对象
     * </p>
     */
    protected <T> Page<T> getPage() {
        return getPage(10L);
liqin's avatar
liqin committed
46 47
    }

liqin's avatar
liqin committed
48 49 50 51 52 53 54 55 56
    protected <T> Page<T> getPage(long size) {
        long _size = size, _index = 1;
        if (request.getParameter("_size") != null) {
            _size = Integer.parseInt(request.getParameter("_size"));
        }
        if (request.getParameter("_index") != null) {
            _index = Integer.parseInt(request.getParameter("_index"));
        }
        return new Page<>(_index, _size);
liqin's avatar
liqin committed
57 58
    }

liqin's avatar
liqin committed
59 60 61 62 63 64 65
    /**
     * 添加数据到结果对象中
     *
     * @param obj 封装接口集合参数
     * @return map
     */
    protected Map<String, Object> getResult(Object obj) {
liqin's avatar
liqin committed
66
        Map<String, Object> map = new LinkedHashMap<>();
wzp's avatar
wzp committed
67 68
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), RESPONSE_CODE_ENUM.REQUEST_SUCCESS.getResultCode());
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), RESPONSE_CODE_ENUM.REQUEST_SUCCESS.getMessage());
liqin's avatar
liqin committed
69
        map.put(RESULT_INFO_ENUM.RESULT_BODY.getKey(), obj);
liqin's avatar
liqin committed
70 71 72 73 74 75 76 77 78
        return map;
    }

    /**
     * 返回成功
     *
     * @return map
     */
    protected Map<String, Object> getSuccessResult() {
liqin's avatar
liqin committed
79
        Map<String, Object> map = new LinkedHashMap<>();
wzp's avatar
wzp committed
80 81
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), RESPONSE_CODE_ENUM.REQUEST_SUCCESS.getResultCode());
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), RESPONSE_CODE_ENUM.REQUEST_SUCCESS.getMessage());
liqin's avatar
liqin committed
82
        map.put(RESULT_INFO_ENUM.RESULT_BODY.getKey(), "");
liqin's avatar
liqin committed
83 84 85 86 87 88 89 90 91 92 93
        return map;
    }

    /**
     * 返回失败
     *
     * @param code 状态码
     * @param msg  失败原因
     * @return map
     */
    protected Map<String, Object> getFailResult(String code, String msg) {
liqin's avatar
liqin committed
94
        Map<String, Object> map = new LinkedHashMap<>();
liqin's avatar
liqin committed
95 96
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), code);
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), msg);
liqin's avatar
liqin committed
97 98 99 100 101 102 103 104 105
        return map;
    }

    /**
     * 返回失败
     *
     * @param responseCodeEnum 特定状态码
     * @return map
     */
liqin's avatar
liqin committed
106
    protected Map<String, Object> getFailResult(RESPONSE_CODE_ENUM responseCodeEnum) {
liqin's avatar
liqin committed
107
        Map<String, Object> map = new LinkedHashMap<>();
wzp's avatar
wzp committed
108 109
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), responseCodeEnum.getResultCode());
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), responseCodeEnum.getMessage());
liqin's avatar
liqin committed
110 111 112 113 114 115 116 117 118
        return map;
    }

    /**
     * 返回失败
     *
     * @return map
     */
    protected Map<String, Object> getFailResult() {
liqin's avatar
liqin committed
119
        Map<String, Object> map = new LinkedHashMap<>();
liqin's avatar
liqin committed
120
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), RESPONSE_CODE_ENUM.SERVER_ERROR.getResultCode());
wzp's avatar
wzp committed
121
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), RESPONSE_CODE_ENUM.SERVER_ERROR.getMessage());
liqin's avatar
liqin committed
122 123 124
        return map;
    }

liqin's avatar
liqin committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138
    /**
     * 添加数据到结果对象中
     *
     * @param obj 封装接口集合参数
     * @return map
     */
    protected Map<String, Object> getFailResult(Object obj) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), "400");
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), "操作失败");
        map.put(RESULT_INFO_ENUM.RESULT_BODY.getKey(), obj);
        return map;
    }

yangtianyou's avatar
yangtianyou committed
139 140 141 142 143 144
    /**
     * 返回失败
     *
     * @return map
     */
    protected Map<String, Object> getFailResult(String msg) {
liqin's avatar
liqin committed
145
        Map<String, Object> map = new LinkedHashMap<>();
liqin's avatar
liqin committed
146
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), RESPONSE_CODE_ENUM.SERVER_ERROR.getResultCode());
yangtianyou's avatar
yangtianyou committed
147 148 149 150
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), msg);
        return map;
    }

liqin's avatar
liqin committed
151 152 153 154 155 156 157 158 159 160 161 162 163
    /**
     * 返回失败
     *
     * @return map
     */
    protected Map<String, Object> getFailResult(String code, String msg, Object obj) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put(RESULT_INFO_ENUM.RESULT_CODE.getKey(), code);
        map.put(RESULT_INFO_ENUM.RESULT_MSG.getKey(), msg);
        map.put(RESULT_INFO_ENUM.RESULT_BODY.getKey(), obj);
        return map;
    }

liqin's avatar
liqin committed
164 165 166 167 168 169 170 171 172 173 174 175 176
    /**
     * JSON 过滤相关字段
     *
     * @param obj          待过滤的对象
     * @param propertyList 需要过滤掉的字段集合
     * @return 过滤后的JSON结果
     */
    protected String propertyFilter(Object obj, List<String> propertyList) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        for (String params : propertyList) {
            filter.getExcludes().add(params);
        }
        return JSONObject.toJSONString(obj, filter);
liqin's avatar
liqin committed
177 178 179
    }

    /**
liqin's avatar
liqin committed
180
     * JSON 过滤相关字段,并保持结果为JSON对象
liqin's avatar
liqin committed
181
     *
liqin's avatar
liqin committed
182 183 184
     * @param obj          待过滤的对象
     * @param propertyList 需要过滤掉的字段集合
     * @return 过滤后的JSON对象结果
liqin's avatar
liqin committed
185
     */
liqin's avatar
liqin committed
186 187 188 189
    protected JSONObject propertyFilterToJsonObject(Object obj, List<String> propertyList) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        for (String params : propertyList) {
            filter.getExcludes().add(params);
liqin's avatar
liqin committed
190
        }
liqin's avatar
liqin committed
191
        return JSONObject.parseObject(JSONObject.toJSONString(obj, filter));
liqin's avatar
liqin committed
192 193 194
    }

    /**
liqin's avatar
liqin committed
195
     * JSON 过滤相关字段,并保持结果为JSON数组
liqin's avatar
liqin committed
196
     *
liqin's avatar
liqin committed
197 198 199
     * @param obj          待过滤的对象
     * @param propertyList 需要过滤掉的字段集合
     * @return 过滤后的JSON数组结果
liqin's avatar
liqin committed
200
     */
liqin's avatar
liqin committed
201 202 203 204 205 206
    protected JSONArray propertyFilterToJsonArray(Object obj, List<String> propertyList) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        for (String params : propertyList) {
            filter.getExcludes().add(params);
        }
        return JSONObject.parseArray(JSONObject.toJSONString(obj, filter));
liqin's avatar
liqin committed
207 208
    }

liqin's avatar
liqin committed
209 210 211 212 213 214 215 216 217 218 219
    /**
     * <p>
     * 根据token信息
     * </p>
     */
    protected String getUserId() {
        String authorization = request.getHeader("Authorization");
        if (StringUtils.isNotBlank(authorization)) {
            if (authorization.startsWith("Bearer ")) {
                authorization = authorization.substring(7);
            }
liqin's avatar
liqin committed
220
        } else {
wzp's avatar
wzp committed
221
            throw new AuthenticationException("token失效,请重新登录");
liqin's avatar
liqin committed
222
        }
liqin's avatar
liqin committed
223
        return JwtTokenUtil.getEmployeeId(authorization);
liqin's avatar
liqin committed
224 225
    }

liqin's avatar
liqin committed
226 227 228 229 230 231 232 233 234 235 236
    /**
     * <p>
     * 根据token信息
     * </p>
     */
    protected String getUserName() {
        String authorization = request.getHeader("Authorization");
        if (StringUtils.isNotBlank(authorization)) {
            if (authorization.startsWith("Bearer ")) {
                authorization = authorization.substring(7);
            }
liqin's avatar
liqin committed
237
        } else {
wzp's avatar
wzp committed
238
            throw new AuthenticationException("token失效,请重新登录");
liqin's avatar
liqin committed
239 240
        }
        return JwtTokenUtil.getUsername(authorization);
liqin's avatar
liqin committed
241 242
    }

243 244 245 246 247 248 249 250 251 252 253
    /**
     * <p>
     * 根据token信息获取当前用户
     * </p>
     */
    protected TUser getcurUser() {
        String userName = getUserName();
        TUser user = userService.selectByUsername(userName);
        return user;
    }

liqin's avatar
liqin committed
254
}