package com.test.musichouduan.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
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;

import java.util.List;

/**
 * Sa-Token配置
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor(handle -> {
            // 打印调试信息
            System.out.println("\n=== Sa-Token 拦截器检查登录状态 ===\n");

            // 获取当前登录用户ID
            Object loginId = StpUtil.getLoginIdDefaultNull();
            System.out.println("当前登录用户ID: " + loginId);

            // 获取当前用户角色
            List<String> roleList = StpUtil.getRoleList();
            System.out.println("当前用户角色: " + roleList);

            // 检查登录状态
            StpUtil.checkLogin();
        }))
                .addPathPatterns("/api/**")
                .excludePathPatterns(
                        "/api/user/login",
                        "/api/user/register",
                        "/api/music/list",
                        "/api/music/latest",
                        "/api/music/hot",
                        "/api/music/*/play",
                        "/api/playlist/list",
                        "/api/playlist/latest",
                        "/api/playlist/hot",
                        "/api/playlist/official",
                        "/api/playlist/*/play",
                        "/api/singer/list",
                        "/api/singer/latest",
                        "/api/type/**",
                        "/api/banner/list",
                        "/api/comment/list"
                );
    }
}
