package com.mini.capi.config;

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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 访问根路径 "/" 时，自动重定向到 Swagger UI
        registry.addRedirectViewController("/", "/login");
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**") // 拦截所有路径
                .excludePathPatterns( // 排除不需要拦截的路径
                        "/",
                        "/login",
                        "/biz/userLogin",
                        "/bootstrap/**",   // 放行bootstrap目录
                        "/css/**",         // 放行css目录
                        "/font-awesome/**",// 放行font-awesome目录
                        "/images/**",      // 放行images目录
                        "/v1/**",          // 放行v1目录
                        "/v2/**",
                        "/erpApi/**",
                        "/appApi/**",
                        "/jobApi/**"
                );
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}
