package cn.com.ty.lift.ud.utils;

import cn.com.ty.lift.ud.redis.RedisUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;


//@DependsOn("redisUtil")
public class CurrentUserInfo {

    private static RedisUtil redisUtil = ApplicationContextUtil.getBean(RedisUtil.class);

    /**
     * 获取当前登录人手机号
     * @return
     */
    public static String userMobile(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String requestToken = request.getHeader("Authorization");
        if(requestToken == null) {
            return null;
        }
        Optional<String> value = Optional.ofNullable(redisUtil.getValue(requestToken));
        if (value.isPresent()){
            return redisUtil.getValue(requestToken).substring(1,12);
        }
        return null;
    }
}
