package org.linlinjava.jz.hm.config.mvc;

import java.util.Map;

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

import org.linlinjava.jz.core.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class WebInterceptor implements HandlerInterceptor {

	private static final Logger logger = LoggerFactory.getLogger(WebInterceptor.class);

	@Autowired
	RedisTemplate redisTemplate;

	@Value("${spring.application.name}")
	public String applicationName;
 
 

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		String body = HttpHelper.getBodyString(request);
		String url = request.getRequestURI();

		logger.info("当前执行的地址是：" + url);
		logger.info("当前执行的body参数是：" + body);
		Map<String, String[]> parameterMap = request.getParameterMap();
		logger.info("当前执行的query参数是" + JsonUtil.getJson(parameterMap));
		logger.info("---------------------------------------");

		String token = request.getHeader("token");
		if (null == token) {
			token = request.getParameter("token");
		}
		//if (null == token) {
		//	return true;
			// throw new LoginException("登陆过时，或未登陆");
		//}

		HandlerMethod hm = (HandlerMethod) handler;
//		System.out.println(hm.getBeanType().toString());
//		String classBean=hm.getBeanType().toString();//classBean+"."+hm.getMethod().getName()
//		String classType="com.jzlife.servantequity.web.bussiness";
//		if(classBean.contains(classType)) {

//		Object objUser = redisDataUtil.getCurrUser(token);
		String openId=request.getParameter("open_id"); 
		 
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			@Nullable ModelAndView modelAndView) throws Exception {
		// controller 方法处理完毕后，调用此方法
//		String url = request.getRequestURI();
//		if (!(url.indexOf("find") != -1 || url.indexOf("select") != -1)) {// 重复提交校验 urlRequestConfig.comperUrl(url)
//			String token = request.getHeader("token");
//			if (null == token) {
//				token = request.getParameter("token");
//			}
//			if (token != null) {
//				try {
//					redisTemplate.delete(token + url);
//				} catch (Exception e) {
//				}
//			}
//		}
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			@Nullable Exception ex) throws Exception {

//		System.out.println("afterCompletion");
//		// 页面渲染完毕后调用次方法，通常用来清除某些资源，类似java中的finnally
//		int status = response.getStatus();
//		// 当前时间
//		long currentTime = System.currentTimeMillis();
//		long startTime = Long.valueOf(request.getAttribute("STARTTIME").toString());
//		System.out.println("拦截器方式请求耗时：" + (currentTime - startTime) + "ms");
	}

}