package com.jfinal.club._front;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.club.common.model.Users;
import com.jfinal.club.util.CacheSessionUtil;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.PropKit;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @descriptions: 用户session 拦截器
 * @author: lc
 * @date: 2020-05-09 19:47:07
 * @Version: 1.0
 **/
public class FrontSessionInterface implements Interceptor {

    @Override
    public void intercept(Invocation inv) {

        HttpServletRequest request = inv.getController().getRequest();
        String uid = inv.getController( ).getCookie(Cons.USERID);

        Users users = Users.dao.findById(uid);
        if(users == null){
                String requestUrl = request.getRequestURI();
                if(!"null".equals(request.getQueryString()) && StringUtils.isNotBlank(request.getQueryString())){
                    requestUrl = requestUrl+"?"+request.getQueryString();
                }

                inv.getController().setCookie(Cons.RETURNURL, requestUrl, PropKit.getInt("cookie"), Cons.ContextPath+"/", null, true);

                inv.getController().redirect("/register");

        }else{
            inv.invoke();
        }
    }
}
