package com.gonghang.h5clientapp.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.io.File;

/**
 * 可以扩展springmvc的功能
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    // 拦截请求
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login/login");
        registry.addViewController("/index.html").setViewName("login/login");
        registry.addViewController("/login/login.html").setViewName("login/login");
        registry.addViewController("/main.html").setViewName("login/login");
    }

    // 排除拦截请求
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LoginHandlerInterceptor());
        interceptorRegistration.addPathPatterns("/**")
                .excludePathPatterns("/doc.html", "/index.html", "/", "/login", "/system/**")
                .excludePathPatterns("/swagg er-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")
                .excludePathPatterns("/mobile/robotH5/**"); //掌上运维接口;
    }

    //添加资源映射，配置静态访问资源（会对外暴露的访问路径）
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("/audio/**").addResourceLocations("file:" + System.getProperty("user.dir") + "/audio/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/include/**").addResourceLocations("classpath:/view/include/"); //Sky added
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    }

    /**
     * 跨域支持
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路由
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                // 是否允许证书（cookies）
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("*")
                //允许服务端访问的客户端请求头
                .allowedHeaders("*")
                // 跨域允许时间
                .maxAge(3600 * 24);
    }


}
