package com.kym.miniapp.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author skyline
 * @description 权限校验
 * @date 2023-07-11 21:49
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    private static final String[] PUBLIC_PATHS = {
            "/wx/notify",
            "/payment/notify",
            "/payment/refundNotify",
            "/error",
            "/user/wxLogin",
            "/user/refresh",
            "/banner",
            "/common/contact",
            "/charge/listStation",
            "/charge/stationStatus",
            "/charge/businessPolicy/*",
            "/charge/stationInfo/*",
            "/charge/stationInfoByShortId/*",
            "/charge/stationInfoByConnectorId/*",
            "/charge/stationInfoByEquipmentId/*",
            "/charge/pullEnStations/*",
    };

    private static final String[] PUBLIC_WILDCARD_PATHS = {
            "/charge/*/query_token",
            "/charge/*/notification_stationStatus",
            "/charge/*/notification_start_charge_result",
            "/charge/*/notification_start_charge_status",
            "/charge/*/notification_equip_charge_status",
            "/charge/*/notification_stop_charge_result",
            "/charge/*/notification_charge_order_info",
            "/charge/*/check_charge_orders",
    };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handle -> {
                    SaRouter
                            .match("/**")
                            .notMatch(PUBLIC_PATHS)
                            .notMatch(PUBLIC_WILDCARD_PATHS)
                            .check(r -> StpUtil.checkLogin());
                }))
                .addPathPatterns("/**");
    }
}
