package com.roma.romaapi.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import static com.roma.romaapi.utils.JWTUtil.SIGN;

// 系统公共工具类
@Component
public class CommonUtil {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    // 通过header中token信息,获取用户Id
    public String getUserIdByHeaderAuthorization(String headerAuthorization){
        String token = headerAuthorization.substring(7); // 截取token
        String redisTokenKey = SIGN + token;
        String userId = stringRedisTemplate.opsForValue().get(redisTokenKey);
        if(null == userId || userId.equals("")){
            return "";
        }
        return userId;
    }

    // 退出登录删除缓存中的token
    public Boolean logoutDeleteRedisToken(String headerAuthorization){
        String token = headerAuthorization.substring(7); // 截取token
        String redisTokenKey = SIGN + token;
        return stringRedisTemplate.delete(redisTokenKey);
    }
}
