package com.bc.exam.modules.user;

import com.bc.exam.core.api.ApiError;
import com.bc.exam.core.exception.ServiceException;
import com.bc.exam.modules.user.dto.response.SysUserLoginDTO;
import org.apache.shiro.SecurityUtils;

/**
 * @Description 描述:用户静态工具类
 * @Author A贾宇婷034244310
 * @Date 20260615
 */
public class UserUtils {


    /**
     * 获取当前登录用户的ID
     * @param throwable
     * @return
     */
    public static String getUserId(boolean throwable){
        try {
            return ((SysUserLoginDTO) SecurityUtils.getSubject().getPrincipal()).getId();
        }catch (Exception e){
            if(throwable){
                throw new ServiceException(ApiError.ERROR_10010002);
            }
            return null;
        }
    }

    /**
     * 获取当前登录用户的ID，默认是会抛异常的
     * @return
     */
    public static String getUserId(){
        return getUserId(true);
    }
}
