namespace Bunny.Common.Context; public class BaseContext { // 使用Lazy初始化确保线程安全 public static readonly ThreadLocal? UserId = null; public static readonly ThreadLocal? Token = null; // 用户id相关 public static long? GetUserId() { return UserId?.Value; } public static void SetUserId(long? userId) { UserId!.Value = userId; } public static string? GetToken() { return Token?.Value; } public static void SetToken(string token) { Token!.Value = token; } public static void RemoveUser() { if (Token != null) Token.Value = null; if (UserId != null) UserId.Value = null; } }