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.CommonUtil;
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-08-20 19:47:07
 * @Version: 1.0
 **/
public class OpenidSessionInterface implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
        if(CommonUtil.isWechatJSPI(inv.getController().getRequest())){
            HttpServletRequest request = inv.getController( ).getRequest( );
            StringBuffer requestURL = request.getRequestURL( );
            String openid = inv.getController( ).getSessionAttr("openid");
            if(StringUtils.isBlank(openid)){
                inv.getController().redirect(PropKit.get("host")+"/wxAuth?url="+requestURL);
            }else{
                String headimg = inv.getController( ).getSessionAttr("headimg");
                inv.getController().set("headimg",headimg);
                inv.invoke();
            }
        }else{
            inv.invoke();
        }
    }
}
