首页>代码>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());
    }
}
最近下载更多
autonomy  LV1 4月12日
kkkllll  LV2 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日
cmbzyjw  LV3 2022年8月17日
最近浏览更多
autonomy  LV1 4月12日
WBelong  LV7 3月29日
暂无贡献等级
kkkllll  LV2 1月24日
xshxxm1  LV21 1月23日
kaye7549317 1月16日
暂无贡献等级
fff2003  LV6 2023年12月19日
微信网友_6200582435409920 2023年12月11日
暂无贡献等级
廖业贵  LV18 2023年11月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友