package com.qs.mp.handler.auth;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class WebConfigurer implements WebMvcConfigurer {

    @Autowired
    private SignInterceptor signInterceptor;

    // 这个方法是用来配置静态资源的，比如html，js，css等
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }
    // 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
    // addPathPatterns("/**") 表示拦截所有的请求，// excludePathPatterns("/login",
    // "/register") 表示除了登陆与注册之外，因为登陆注册不需要登陆也可以访问

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(signInterceptor).
        addPathPatterns("/**")
        .excludePathPatterns(
            "/service/notify/wxcallback",
            "/service/notify/payCallback",
            "/captchaImage");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
        builder.indentOutput(true);
        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));

    }
}
