package org.jeecg.config.GuavaRateLimiter;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class RateLimiterInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //接口限流,如果打开则pdf无法显示,提示授权失败
//        RateLimiter rateLimiter = ((HandlerMethod) handler).getMethodAnnotation(RateLimiter.class);
//        if (Objects.nonNull(rateLimiter)) {
//            GuavaRateLimiterUtils instance = GuavaRateLimiterUtils.getInstance();
//            //限流
//            instance.setPermits(request.getRequestURI(), rateLimiter.permitRate());
//            //限流拦截
//            if (!instance.tryAcquire(request.getRequestURI(), rateLimiter.timeout(), rateLimiter.timeUnit())) {
//                response.setContentType("application/json;charset=UTF-8");
//                response.getWriter().append(JSONObject.toJSONString("接口限流"));
//                return false;
//            }
//        }
        return true;
    }
}


