2024-08-30 16:08:08 +08:00
|
|
|
|
namespace Bunny.Common.Context;
|
|
|
|
|
|
|
|
|
|
public class BaseContext
|
|
|
|
|
{
|
|
|
|
|
// 使用Lazy初始化确保线程安全
|
2024-08-30 20:27:48 +08:00
|
|
|
|
public static readonly ThreadLocal<long?>? UserId = null;
|
|
|
|
|
public static readonly ThreadLocal<string?>? Token = null;
|
2024-08-30 16:08:08 +08:00
|
|
|
|
|
|
|
|
|
// 用户id相关
|
|
|
|
|
public static long? GetUserId()
|
|
|
|
|
{
|
2024-08-30 20:27:48 +08:00
|
|
|
|
return UserId?.Value;
|
2024-08-30 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetUserId(long? userId)
|
|
|
|
|
{
|
2024-08-30 20:27:48 +08:00
|
|
|
|
UserId!.Value = userId;
|
2024-08-30 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string? GetToken()
|
|
|
|
|
{
|
2024-08-30 20:27:48 +08:00
|
|
|
|
return Token?.Value;
|
2024-08-30 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetToken(string token)
|
|
|
|
|
{
|
2024-08-30 20:27:48 +08:00
|
|
|
|
Token!.Value = token;
|
2024-08-30 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RemoveUser()
|
|
|
|
|
{
|
2024-08-30 20:27:48 +08:00
|
|
|
|
if (Token != null) Token.Value = null;
|
|
|
|
|
if (UserId != null) UserId.Value = null;
|
2024-08-30 16:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|