package cn.qinys.platform.upms.config.mybatisplus;


import cn.qinys.platform.base.security.utils.CurrentUtils;
import cn.qinys.platform.base.vo.LoginUserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * Description:
 *
 * @author tanlie
 * Date: 2024/5/20 14:58
 */
public class TenantInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 没有token，不处理
        String token = CurrentUtils.getToken();
        if (!StringUtils.hasLength(token)) {
            return true;
        }
        try {
            LoginUserInfo userInfo = CurrentUtils.getCurrentUserInfo();
            if (userInfo != null) {
                TenantContextHolder.setTenantId(userInfo.getTenantId());
            }
        } catch (Exception ignored) {
        }
        return true;
    }

}
