sa_token_plugin_gotham/
state.rs

1use std::sync::Arc;
2use sa_token_adapter::storage::SaStorage;
3use sa_token_core::{SaTokenManager, SaTokenConfig, StpUtil};
4
5/// 中文 | English
6/// Sa-Token 状态 | Sa-Token State
7/// 
8/// 包含 Sa-Token 管理器和配置 | Contains Sa-Token manager and configuration
9#[derive(Clone)]
10pub struct SaTokenState {
11    pub manager: Arc<SaTokenManager>,
12}
13
14impl SaTokenState {
15    /// 中文 | English
16    /// 从存储和配置创建状态 | Create state from storage and config
17    pub fn new(storage: Arc<dyn SaStorage>, config: SaTokenConfig) -> Self {
18        Self {
19            manager: Arc::new(SaTokenManager::new(storage, config)),
20        }
21    }
22    
23    /// 中文 | English
24    /// 从 SaTokenManager 创建状态 | Create state from SaTokenManager
25    pub fn from_manager(manager: SaTokenManager) -> Self {
26        // 自动初始化全局 StpUtil | Auto-initialize global StpUtil
27        StpUtil::init_manager(manager.clone());
28        
29        Self {
30            manager: Arc::new(manager),
31        }
32    }
33    
34    /// 中文 | English
35    /// 使用构建器模式创建状态 | Create state using builder pattern
36    pub fn builder() -> SaTokenStateBuilder {
37        SaTokenStateBuilder::default()
38    }
39}
40
41/// 中文 | English
42/// SaTokenState 构建器 | SaTokenState builder
43#[derive(Default)]
44pub struct SaTokenStateBuilder {
45    config_builder: sa_token_core::config::SaTokenConfigBuilder,
46}
47
48impl SaTokenStateBuilder {
49    /// 中文 | English
50    /// 创建新的构建器 | Create a new builder
51    pub fn new() -> Self {
52        Self::default()
53    }
54
55    /// 中文 | English
56    /// 设置 token 名称 | Set token name
57    pub fn token_name(mut self, name: impl Into<String>) -> Self {
58        self.config_builder = self.config_builder.token_name(name);
59        self
60    }
61    
62    /// 中文 | English
63    /// 设置 token 有效期(秒) | Set token timeout (seconds)
64    pub fn timeout(mut self, timeout: i64) -> Self {
65        self.config_builder = self.config_builder.timeout(timeout);
66        self
67    }
68    
69    /// 中文 | English
70    /// 设置 token 临时有效期(秒) | Set token active timeout (seconds)
71    pub fn active_timeout(mut self, timeout: i64) -> Self {
72        self.config_builder = self.config_builder.active_timeout(timeout);
73        self
74    }
75    
76    /// 中文 | English
77    /// 设置是否开启自动续签 | Set whether to enable auto renew
78    pub fn auto_renew(mut self, enabled: bool) -> Self {
79        self.config_builder = self.config_builder.auto_renew(enabled);
80        self
81    }
82    
83    /// 中文 | English
84    /// 设置是否允许并发登录 | Set whether to allow concurrent login
85    pub fn is_concurrent(mut self, concurrent: bool) -> Self {
86        self.config_builder = self.config_builder.is_concurrent(concurrent);
87        self
88    }
89    
90    /// 中文 | English
91    /// 设置是否共享 token | Set whether to share token
92    pub fn is_share(mut self, share: bool) -> Self {
93        self.config_builder = self.config_builder.is_share(share);
94        self
95    }
96    
97    /// 中文 | English
98    /// 设置 token 风格 | Set token style
99    pub fn token_style(mut self, style: sa_token_core::config::TokenStyle) -> Self {
100        self.config_builder = self.config_builder.token_style(style);
101        self
102    }
103    
104    /// 中文 | English
105    /// 设置 token 前缀 | Set token prefix
106    pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
107        self.config_builder = self.config_builder.token_prefix(prefix);
108        self
109    }
110    
111    /// 中文 | English
112    /// 设置 JWT 密钥 | Set JWT secret key
113    pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
114        self.config_builder = self.config_builder.jwt_secret_key(key);
115        self
116    }
117    
118    /// 中文 | English
119    /// 设置存储实现 | Set storage implementation
120    pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
121        self.config_builder = self.config_builder.storage(storage);
122        self
123    }
124    
125    /// 中文 | English
126    /// 构建 SaTokenState | Build SaTokenState
127    pub fn build(self) -> SaTokenState {
128        // config_builder.build() 已经自动初始化了 StpUtil
129        // config_builder.build() already auto-initializes StpUtil
130        let manager = self.config_builder.build();
131        // 直接创建 SaTokenState,不再调用 from_manager 避免重复初始化
132        // Create SaTokenState directly, don't call from_manager to avoid duplicate initialization
133        SaTokenState {
134            manager: Arc::new(manager),
135        }
136    }
137}