package com.backendsys.modules.common.aspect;

import cn.hutool.core.convert.Convert;
import com.backendsys.exception.CustException;
import com.backendsys.modules.common.config.security.entity.SecurityAppUserInfo;
import com.backendsys.modules.common.config.security.entity.SecurityUserInfo;
import com.backendsys.modules.common.config.security.utils.JwtUtil;
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * [AOP] APP用户登录鉴权
 */
@Aspect
@Component
public class AppUserLoginAspect {

    @Pointcut("@annotation(AppUserLogin)")
    public void checkLogin() {}

    @Before("checkLogin()")
    public void doCheckLogin(JoinPoint joinPoint) throws Throwable {
        SecurityAppUserInfo userInfo = SecurityUtil.getAppUserInfo();
        String tokenRole = Convert.toStr(userInfo.getRole());
        if (!"APP_USER".equals(tokenRole)) {
            throw new CustException("当前接口与 Token 的类型不匹配");
        }
    }
}
