package com.mosty.common.token;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.mosty.common.base.exception.BusinessException;

public class UserInfoManager {

    /**
     * 用户信息
     */
    private static final ThreadLocal<UserInfo> USER_CONTEXT = TransmittableThreadLocal.withInitial(() -> null);

    public UserInfoManager() {}

    public static void set(UserInfo userInfo) {
        USER_CONTEXT.set(userInfo);
    }

    public static void clear() {
        USER_CONTEXT.remove();
    }

    public static UserInfo get() {
        UserInfo user = USER_CONTEXT.get();
        if (user == null) {
            throw new BusinessException("登录信息获取异常，请重新登录！！");
        }
        return user;
    }

    public static UserInfo getUser() {
        return USER_CONTEXT.get();
    }
}
