package com.judong.chuanyiserver.config;

import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.exception.CustomException;
import com.judong.chuanyiserver.util.Blank;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器配置
 */
public class Interceptors implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String token = request.getHeader("token");
        String requestURI = request.getRequestURI();
        if (Blank.isEmpty(token)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
//        boolean verify = TokenUtil.verify(token);
//        if (!verify) {
//            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
//        }
//        return verify;
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

}
