package com.manage.util;

import com.manage.encrypt.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/8/15 8:57
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/8/15 8:57
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MyCookieUtil {
    public static void remember(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("userName");                    //获取用户名
        String password = request.getParameter("userPwd");        //获取密码
        String remember = request.getParameter("rememberMe");     //获取是否打钩
        String codeName="";
        try {
            codeName = URLEncoder.encode(name, "UTF-8");      //对输入的中文进行编码，防止乱码出现
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //密码base64编码
        password = Base64.encode(password);
        Cookie nameCookie = new Cookie("name", codeName);
        Cookie passwordCookie = new Cookie("password", password);
        nameCookie.setPath(request.getContextPath()+"/");      //设置Cookie的有效路径
        passwordCookie.setPath(request.getContextPath()+"/");//设置Cookie的有效路径
        if(remember != null && "yes".equals(remember)){            //有记住我，就设置cookie的保存时间
            nameCookie.setMaxAge(7*24*60*60);
            passwordCookie.setMaxAge(7*24*60*60);
        }else{                                                                                 //没有记住我，设置cookie的时间为0
            nameCookie.setMaxAge(0);
            passwordCookie.setMaxAge(0);
        }
        response.addCookie(nameCookie);
        response.addCookie(passwordCookie);
    }
}
