package com.bcxin.ins.filter;

import com.bcxin.ins.spring.util.JedisUtils;
import com.bcxin.ins.util.IpAddress;
import com.bcxin.ins.vo.ConstProp;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ImageFilter implements Filter{  
  
	
    @Override  
    public void destroy() {  
          
    }  
  
    @Override  
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {  
    	
        HttpServletRequest request = (HttpServletRequest) arg0;    
        HttpServletResponse response = (HttpServletResponse) arg1;   
        String yanzhengm = request.getParameter("j_captcha");  
        String yzType = request.getParameter("yzType"); //普通用户 0,管理员 5,input hidden 
        String autoLoginFlg = request.getParameter("autoLoginFlg");
        String key = ConstProp.YZM_EN+ IpAddress.getIpAddress(request);
        String sessionyanz = JedisUtils.get(key);
        //用完销毁
        JedisUtils.del(key);
        if("1".equals(autoLoginFlg) || yanzhengm.equals(sessionyanz)){
        	
        	String url = request.getHeader("Referer");
        	System.out.println(url);
            
            
        	/*if("1".equals(loginType)){
        		if(user != null){
        			if("ROLE_USER".equals(user.getRole().getKind())){
        				arg2.doFilter(request, response);  
        			}else{
        				request.getSession(true).setAttribute("msg", "未找到用户");
        				response.sendRedirect(request.getContextPath() + "/user/login/init");
        			}
        		}else{
        			arg2.doFilter(request, response);  
        		}
        	}else if("2".equals(loginType)){
        		if(user != null){
        			if("ROLE_BID".equals(user.getRole().getKind())){
        				arg2.doFilter(request, response);  
        			}else{
        				request.getSession(true).setAttribute("msg", "未找到用户");
        				response.sendRedirect(request.getContextPath() + "/user/login/init");
        			}
        		}else{
        			arg2.doFilter(request, response);  
        		}
        	}*/
        	
        	arg2.doFilter(request, response); 
            
            
            
        }else{
        	request.getSession(true).setAttribute("yzkeyerror", "验证码错误请从新输入");
        	if ("0".equals(yzType)) {
        		response.sendRedirect(request.getContextPath() + ConstProp.SLASH);
        	} else if("5".equals(yzType)) {
        	    //登录页面
        	} else {
        		response.sendRedirect(request.getContextPath() + "/user/regist/init");
        	}
        }  
    }  
  
    @Override  
    public void init(FilterConfig arg0) throws ServletException {  
          
    }  
      
}
