package com.cm.common.wechat.config;

import com.cm.common.constants.ISystemConstant;
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
import com.cm.common.wechat.filter.WechatFilter;
import com.cm.common.wechat.filter.WechatMiniAppFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: WechatFilterConfig
 * @Description: 微信过滤器
 * @Author: WangGeng
 * @Date: 2020/3/2 11:24 下午
 * @Version: 1.0
 **/
@Configuration
public class WechatFilterConfig {

    @Autowired
    private WechatOfficialAccountProperties wechatOfficialAccountProperties;

    @Bean
    public FilterRegistrationBean wechatFilterRegister() {
        WechatFilter wechatFilter = new WechatFilter();
        wechatFilter.setWechatOfficialAccountProperties(wechatOfficialAccountProperties);

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(wechatFilter);
        filterRegistrationBean.addUrlPatterns(ISystemConstant.WECHAT_PREFIX + "/*", ISystemConstant.WECHAT_ROUTE_PREFIX + "/*");
        filterRegistrationBean.setName("wechatFilter");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean wechatMiniAppRegister() {
        WechatMiniAppFilter wechatMiniAppFilter = new WechatMiniAppFilter();

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(wechatMiniAppFilter);
        filterRegistrationBean.addUrlPatterns(ISystemConstant.WECHAT_MINI_APP_PREFIX + "/*");
        filterRegistrationBean.setName("wechatMiniAppFilter");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

}
