首页>代码>spring boot+mybatis开发海底捞的排队系统(简易版)>/hdl/src/main/java/vip/waitfor/hdl/controller/BaseController.java
package vip.waitfor.hdl.controller;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.waitfor.hdl.service.exception.DuplicatekeyException;
import vip.waitfor.hdl.service.exception.ServiceException;
import vip.waitfor.hdl.service.exception.UsernameDuplicateException;
import vip.waitfor.hdl.util.ResponseResult;


import javax.servlet.http.HttpSession;

/**
 * 当前项目中所有控制器的基类
 */
public abstract class BaseController {

    /*
    正确响应时的代号
     */
    public static final Integer SUCCESS = 200;

    @ExceptionHandler({ServiceException.class})
    @ResponseBody
    public ResponseResult<Void> handleException(Exception e) {
        Integer state = null;
        if (e instanceof DuplicatekeyException) {
            state = 400;//400-违反了Unique约束的异常
        } else if (e instanceof UsernameDuplicateException) {
            state = 401;//401-用户数据不存在
        }
        return new ResponseResult<Void>(state, e);

    }

    /**
     * 从Session中获取uid
     *
     * @param session HttpSession对象
     * @return 当前登录的用户的id
     */
    protected Integer getUidFromSession(HttpSession session) {
        return Integer.valueOf(session.getAttribute("uid").toString());
    }

    /**
     * 从Session中获取uid
     *
     * @param session httpSession对象
     * @return 当前登录的用户的用户名
     */
    protected String getUsernameFromSession(HttpSession session) {
        return String.valueOf(session.getAttribute("username").toString());
    }
}
最近下载更多
TY0165  LV20 2024年6月18日
autonomy  LV1 2024年4月12日
kkkllll  LV2 2024年1月24日
hbj1285984841  LV4 2023年10月19日
iceboard  LV2 2023年6月4日
万紫怡  LV4 2023年5月15日
youzitao  LV11 2023年4月17日
朱朱啊哈  LV16 2023年1月31日
LukeMaster  LV1 2022年11月19日
crosa_Don  LV18 2022年10月7日
最近浏览更多
Phoebe_HUI  LV5 1月14日
yimaoermao  LV1 2024年11月28日
17507331220 2024年11月19日
暂无贡献等级
grant1123  LV2 2024年6月22日
TY0165  LV20 2024年6月18日
来一杯西瓜冰咩  LV6 2024年5月14日
1941549176  LV4 2024年5月9日
autonomy  LV1 2024年4月12日
微信网友_6927932988952576  LV12 2024年3月31日
WBelong  LV8 2024年3月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友