package com.fkzy.warn.common.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * web拦截
 *
 * @author yuyantian
 * @date 2021/9/7
 */

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Value("${file.fileUploadRootPath}")
    private String fileUploadRootPath;

    @Value("${file.fileUploadPath}")
    private String fileUploadPath;

    @Value("${springfox.documentation.swagger.v2.path:/v2/api-docs}")
    private String swaggerDocs;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // 除login外都拦截
        registry.addInterceptor(webInterceptor())
            .addPathPatterns("/**")
            // springboot 错误路径
            .excludePathPatterns("/error")
            // swagger: springfox.documentation.swagger.web.ApiResourceController
            .excludePathPatterns("/configuration/security", "/configuration/ui",
                "/swagger-resources")
            // swagger: springfox.documentation.swagger2.web.Swagger2Controller
            .excludePathPatterns(swaggerDocs)
            // 静态资源不拦截
            .excludePathPatterns("/static/**", "swagger-ui.html", "/webjars/**",
                "/" + fileUploadPath + "/**")
            .excludePathPatterns("/weChat/*")

        ;
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
            .addResourceLocations("/static/");
        // swagger start
        registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/images/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/images/");
        // swagger end
        //附件虚拟路径的配置
        registry.addResourceHandler("/" + fileUploadPath + "/**")
            .addResourceLocations("file:" + fileUploadRootPath + fileUploadPath);
    }

    @Bean
    public WebInterceptor webInterceptor() {
        return new WebInterceptor();
    }
}
