package common.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import common.utils.http.MyRet;

public class LoginInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation invocation) {
        Controller controller = invocation.getController();

        String serverToken = (String)controller.getSession().getAttribute("dl-token");
        String clientToken = controller.getHeader("dl-token");
        String id = controller.getSessionAttr("user_id");
        String mobileNumber = controller.getSessionAttr("mobile_number");

        if (StrKit.notBlank(serverToken, clientToken) && clientToken.equals(serverToken) && StrKit.notBlank(id, mobileNumber)) {
            invocation.invoke();
        } else {
            MyRet ret = MyRet.fail("登录信息已过期，请重新登录。");
            
            ret.setCode(MyRet.CODE_NO_LOGIN);

            controller.renderJson(ret);
        }
    }
}
