//package com.crm.gateway.filter;
//
//import com.crm.gateway.configuration.LocaleMessage;
//import com.crm.gateway.property.ConfigureProperty;
//import com.crm.gateway.service.RedisService;
//import com.crm.gateway.util.GetIpAndMac;
//import com.crm.gateway.util.InteceptorResultUtil;
//import com.crm.rely.backend.core.constant.Constants;
//import com.crm.rely.backend.core.dto.base.BaseResultDto;
//import com.google.common.base.Strings;
//import com.netflix.zuul.ZuulFilter;
//import com.netflix.zuul.context.RequestContext;
//import lombok.SneakyThrows;
//import lombok.extern.log4j.Log4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.RequestMethod;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.UUID;
//
//@Log4j
//@Component
//public class JwtNoRepeatSubmitGatewayFilterFactory extends ZuulFilter {
//    @Autowired
//    ConfigureProperty configureProperty;
//
//    @Autowired
//    private LocaleMessage localeMessage;
//
//    public static final String PRE_TYPE = "pre";
//
//    private static final int EXPIRE_TIME_NOT_TOKEN = 2000;
//
//    private static final int EXPIRE_TIME = 3 * 1000;
//
//    @Autowired
//    private RedisService redisService;
//
//    // 前置过滤器
//    @Override
//    public String filterType() {
//        return PRE_TYPE;
//    }
//
//    // 过滤顺序
//    @Override
//    public int filterOrder() {
//        return 1;
//    }
//
//    @SneakyThrows
//    @Override
//    public boolean shouldFilter() {
//        return true;
//    }
//
//    @SneakyThrows
//    @Override
//    public Object run() {
//        RequestContext requestContext = RequestContext.getCurrentContext();
//        HttpServletRequest request = requestContext.getRequest();
//        HttpServletResponse response = requestContext.getResponse();
//        boolean handle = handle(request, response);
//        if (!handle) {
//            requestContext.setSendZuulResponse(handle);
//            requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
//        }
//        return null;
//    }
//
//    private boolean handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
//            return true;
//        }
//        String uri = httpServletRequest.getRequestURI();
//        String token = httpServletRequest.getHeader(Constants.ACCESS_TOKEN);
//        String ip = GetIpAndMac.getIp(httpServletRequest);
//
//        String key = (Strings.isNullOrEmpty(token) ? "" : token) + ip + uri;
//        String uuid = UUID.randomUUID().toString();
//        int expireTime;
//        if (Strings.isNullOrEmpty(token)) {
//            expireTime = EXPIRE_TIME_NOT_TOKEN;
//        } else {
//            expireTime = EXPIRE_TIME;
//        }
//        if (!redisService.tryLock(key, uuid, expireTime)) {
//            InteceptorResultUtil.returnResult(httpServletResponse, BaseResultDto.result(Constants.FAILED_CODE,
//                    localeMessage.getMessage("no_repeat_submit_error", "no_repeat_submit_error")));
//            return false;
//        }
//        return true;
//    }
//}
