package com.changhu.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.changhu.support.interceptor.JsonBodyInterceptor;
import com.changhu.support.interceptor.OpenApiInterceptor;
import com.changhu.support.interceptor.UserTypeInterceptor;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * author: luozhun
 * desc: WebConfig
 * createTime: 2023/8/18 10:56
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final List<String> whiteList = new ArrayList<>();

    public WebConfig() {
        whiteList.add("/common/**");
        whiteList.add("/open/**");
        whiteList.add("/test/**");
        whiteList.add("/login");
        whiteList.add("/logout");
        whiteList.add("/favicon.ico");
        //druid console
        whiteList.add("/druid/**");
        //knife4j
        whiteList.add("/doc.html/**");
        whiteList.add("/static/**");
        whiteList.add("/swagger-resources");
        whiteList.add("/**webjars/**");
        whiteList.add("/v3/**");
        //获取企业注册审核状态
        whiteList.add("/management/getCheckStatus");
        //微信小程序注册
        whiteList.add("/miniProgramUser/register");
        //二维码表单录入保安人员
        whiteList.add("/miniProgramUser/qrCodeFormInputSecurityUser");
    }

    @Override
    public void addInterceptors(@NotNull InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns(whiteList);
        // 注册jsonBody 拦截器 用于标识是否需要JsonResult返回
        registry.addInterceptor(new JsonBodyInterceptor());
        // 注册clientType 拦截器 用于校验当前用户是否匹配操作客户端
        registry.addInterceptor(new UserTypeInterceptor());
        // 注册开放接口 拦截器 用于校验第三方是否携带指定apiKey
        registry.addInterceptor(new OpenApiInterceptor())
                .addPathPatterns("/open/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "OPTION", "PUT", "DELETE")
                .allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
                        "Access-Control-Request-Headers", "Authorization", "Token", "*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
