package com.emotion.util;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 令牌工具类
 * 
 * @author huazhongmin
 * @date 2025-09-08
 */
@Component
public class TokenUtil {

    /**
     * 从请求中提取访问令牌
     * 
     * @param request HTTP请求
     * @return 访问令牌
     */
    public String extractToken(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            return authHeader.substring(7);
        }

        // 也可以从请求参数中获取
        String tokenParam = request.getParameter("token");
        if (tokenParam != null && !tokenParam.trim().isEmpty()) {
            return tokenParam.trim();
        }

        return null;
    }

    /**
     * 验证令牌是否有效
     * 
     * @param token 令牌
     * @return 是否有效
     */
    public boolean isValidToken(String token) {
        return StringUtils.hasText(token);
    }
}