package com.sy.coinage.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson2.JSON;
import com.sy.coinage.core.constants.CacheKey;
import com.sy.coinage.core.redis.RedisService;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.core.interceptor.TokenVerify;

import lombok.extern.slf4j.Slf4j;

/**
 * -获取登录用户信息实现类
 */
@Slf4j
@Configuration
public class TokenVerifyConfig {

    @Autowired
    private RedisService redisService;

    @Bean
    public TokenVerify tokenVerify() {
        return new TokenVerify() {
            @Override
            public CacheUserVO getCacheUser(String token) {
                if (log.isDebugEnabled()) {
                    log.debug("token={}", token);
                }
                CacheUserVO user = null;
                String key = CacheKey.TOKEN + token;
                String json = redisService.get(key);
                if (json != null && !"".equals(json) && !"null".equals(json)) {
                    user = JSON.parseObject(json, CacheUserVO.class);
                    redisService.expire(key, CacheKey.EXPIRED_TWO_HOUR);
                }
                return user;
            }
        };
    }

}
