package com.rf.psychological.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author lpf
 * @description:
 * @date 2021/12/2822:06
 */
@Configuration
@Profile("public")
public class JWTInterceptorPublicConfig implements WebMvcConfigurer {

    @Autowired
    private UserCheckInterceptor userCheckInterceptor;

    @Autowired
    private JWTInterceptor jwtInterceptor;



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //定义排除swagger访问的路径配置
        String[] swaggerExcludes = new String[]{"/swagger-ui.html", "/swagger-resources/**", "/csrf", "/webjars/**"};
        String[] institutionLogin = new String[]{"/user/login", "/institution/loginV2", "/admin/login", "/superAdmin/login"};
        String[] webLogin = new String[]{"/user/authCode", "/user/saveUser", "/user/webLogin", "/user/webUpdatePassword", "/user/*/checkUser"};
        String[] automaticImport = new String[]{"/category/loadCategory", "/subjectInfo", "/subjectInfoList", "/importTest", "/subjectInfoUpload/importTest", "/subjectInfo/update", "/subjectInfoUpload/update"};
        //公网版需要直接显示量表认知任务以及类别
        String[] temp = new String[]{"/category/getCognizeCategoryList", "/category/getCategoryList",
                "/subjectInfo/getSubjectListByAuth", "/subjectInfo/getCognizeListByAuth", "/subjectInfo/getRecallChar", "/subjectInfo/getRecallCharTwo",
                "/subjectInfo/getSubjectByFlag/*","/subjectInfo/getSubjectByFlagV2/*","/scaleInfo/*","/record/getRecordByModelPhone","/api/orderInfo/queryOrderDetail/**","/scaleExternalSource/save"};
        String[] wxPay = new String[]{"/api/wx-pay/native/notify","/api/wx-pay/refunds/notify",
                "/api/wx-pay/getPhone/*","/api/wx-pay/code2openid","/api/wx-pay/h5Pay",
                "/api/wx-pay/code2openid2","/api/wx-pay/queryOrder/**","/api/promotionInfo/queryPromotionDetail/**","/record/getWeiboRecordById","/record/getWeiboRecordById/**"};
        String[] systemApi = new String[]{"/system/login","/system/loginH5","/system/forgotPassword","/system/registerUser","/system/temporaryUser"};
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/index**", "/error")
                .excludePathPatterns(institutionLogin)
                .excludePathPatterns(automaticImport)
                .excludePathPatterns(wxPay)
                .excludePathPatterns(systemApi)
                .excludePathPatterns("/user/saveUserV2", "/institution/saveUser")
                .excludePathPatterns(webLogin)
                .excludePathPatterns("/user/LANSaveUserInfo", "/user/LANLogin", "/user/LANUpdatePassword")
                .excludePathPatterns("/file/**", "/result/download/**")
                .excludePathPatterns("/download/**")
                .excludePathPatterns("/apk/**")
                .excludePathPatterns("/device/**")
                .excludePathPatterns("/getRecordById")
                .excludePathPatterns("/userImport")
                .excludePathPatterns("/test/token")
                .excludePathPatterns(swaggerExcludes)
                .excludePathPatterns(temp)
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/druid/**")
//                .excludePathPatterns(temporarily)
                .excludePathPatterns("/static/**");//排除静态资源
        registry.addInterceptor(userCheckInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/index.html", "/error")
                .excludePathPatterns(institutionLogin)
                .excludePathPatterns(automaticImport)
                .excludePathPatterns(wxPay)
                .excludePathPatterns(systemApi)
                .excludePathPatterns("/user/saveUserV2", "/institution/saveUser")
                .excludePathPatterns(webLogin)
                .excludePathPatterns("/user/LANSaveUserInfo", "/user/LANLogin", "/user/LANUpdatePassword")
                .excludePathPatterns("/file/**", "/result/download/**")
                .excludePathPatterns("/download/**")
                .excludePathPatterns("/apk/**")
                .excludePathPatterns("/device/**")
                .excludePathPatterns("/getRecordById")
                .excludePathPatterns("/userImport")
                .excludePathPatterns("/test/token")
//                .excludePathPatterns(temporarily)
                .excludePathPatterns(swaggerExcludes)
                .excludePathPatterns(temp)
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/druid/**")
                .excludePathPatterns("/static/**");//排除静态资源
    }

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