package com.fdage.listner;

//import com.fdage.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
        import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * Created by Hb_zzZ on 2018/12/6.
 */
@Slf4j
@Component
public class LoggerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        log.info("进入" + uri + "请求");

//        String token = request.getHeader("token");
//        if(StringUtils.isEmpty(token) || "null".equals(token)){
//            log.info(uri + "请求token为空");
//            response.setContentType("text/html;charset=UTF-8");
//            JSONObject responseJson = new JSONObject();
//            responseJson.put("code", 202);
//            responseJson.put("message", "token不能为空");
//            response.getWriter().println(responseJson);
//            return false;
//        }
//        Claims claims = JwtUtil.parseJWT(token);
//        Object obj = redisUtils.get(CommonConf.REDIS_LOGIN_TOKEN + claims.get("username", String.class));
//        if(obj == null || !obj.equals(token)){
//            log.info(uri + "请求token错误被拦截");
//            response.setContentType("text/html;charset=UTF-8");
//            JSONObject responseJson = new JSONObject();
//            responseJson.put("code", 202);
//            responseJson.put("message", "token不正确请重新登陆");
//            response.getWriter().println(responseJson);
//            return false;
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        String uri = request.getRequestURI();
        log.info(uri + "请求结束");
    }
}
