package com.ruoyi.system.utils;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ZouTiancong
 * @Date: 2022/05/23/22:39
 * @Description:
 */
@Component
@Aspect
public class AuthAspect {

    private HttpServletRequest request;
    private final RedisCache redisCache;

    public AuthAspect(RedisCache redisCache) {
        this.redisCache = redisCache;
    }

    @Resource
    public void setHttpServletRequest(HttpServletRequest request) {
        this.request = request;
    }


    @Pointcut("@annotation(com.ruoyi.system.utils.Auth)")
    private void authPointcut() {
    }

    @Around("authPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取方法签名信息从而获取方法名和参数类型
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

        // 获取目标方法对象上注解中的属性值
        Auth auth = methodSignature.getMethod().getAnnotation(Auth.class);

        // JWT 只解析和验签一次，后续会话校验及 Controller 均复用这里提取的可信身份。
        if (auth.token()) {
            Map<String, Object> claims = JwtUtil.verifyToken(request.getHeader("token"));
            if (!Boolean.TRUE.equals(claims.get("valid"))) {
                return AjaxResult.error(401,"token已过期，请重新登录！");
            }

            Long userId = parseUserId(claims.get("id"));
            Object jtiValue = claims.get("jti");
            String jti = jtiValue instanceof String ? (String) jtiValue : null;
            if (auth.session() && (userId == null || jti == null || jti.isBlank()
                    || !Boolean.TRUE.equals(redisCache.hasKey(jti)))) {
                throw new ServiceException(MessageUtils.message("merchant.session.invalid"));
            }

            request.setAttribute(AuthContext.USER_ID_ATTRIBUTE, userId);
            request.setAttribute(AuthContext.JTI_ATTRIBUTE, jti);
        }
        return joinPoint.proceed();
    }

    private Long parseUserId(Object claim) {
        if (!(claim instanceof String value) || value.isBlank() || "null".equals(value)) {
            return null;
        }
        try {
            return Long.valueOf(value);
        } catch (NumberFormatException exception) {
            return null;
        }
    }
}
