File size: 3,069 Bytes
a21c316 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | use serde::{Deserialize, Serialize};
use crate::modules::user_token_db::{self, UserToken, TokenIpBinding};
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateTokenRequest {
pub username: String,
pub expires_type: String,
pub description: Option<String>,
pub max_ips: i32,
pub curfew_start: Option<String>,
pub curfew_end: Option<String>,
pub custom_expires_at: Option<i64>, // 自定义过期时间戳 (秒)
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateTokenRequest {
pub username: Option<String>,
pub description: Option<String>,
pub enabled: Option<bool>,
pub max_ips: Option<i32>,
pub curfew_start: Option<Option<String>>,
pub curfew_end: Option<Option<String>>,
}
// 命令实现
/// 列出所有令牌
#[tauri::command]
pub async fn list_user_tokens() -> Result<Vec<UserToken>, String> {
user_token_db::list_tokens()
}
/// 创建新令牌
#[tauri::command]
pub async fn create_user_token(request: CreateTokenRequest) -> Result<UserToken, String> {
user_token_db::create_token(
request.username,
request.expires_type,
request.description,
request.max_ips,
request.curfew_start,
request.curfew_end,
request.custom_expires_at,
)
}
/// 更新令牌
#[tauri::command]
pub async fn update_user_token(id: String, request: UpdateTokenRequest) -> Result<(), String> {
user_token_db::update_token(
&id,
request.username,
request.description,
request.enabled,
request.max_ips,
request.curfew_start,
request.curfew_end,
)
}
/// 删除令牌
#[tauri::command]
pub async fn delete_user_token(id: String) -> Result<(), String> {
user_token_db::delete_token(&id)
}
/// 续期令牌
#[tauri::command]
pub async fn renew_user_token(id: String, expires_type: String) -> Result<(), String> {
user_token_db::renew_token(&id, &expires_type)
}
/// 获取令牌 IP 绑定
#[tauri::command]
pub async fn get_token_ip_bindings(token_id: String) -> Result<Vec<TokenIpBinding>, String> {
user_token_db::get_token_ips(&token_id)
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UserTokenStats {
pub total_tokens: usize,
pub active_tokens: usize,
pub total_users: usize,
pub today_requests: i64,
}
/// 获取简单的统计信息
#[tauri::command]
pub async fn get_user_token_summary() -> Result<UserTokenStats, String> {
let tokens = user_token_db::list_tokens()?;
let active_tokens = tokens.iter().filter(|t| t.enabled).count();
// 统计唯一用户
let mut users = std::collections::HashSet::new();
for t in &tokens {
users.insert(t.username.clone());
}
// 这里简单返回一些数据,请求数最好从数据库聚合查询
// 目前仅作为演示,请求数暂不精确统计今日的
Ok(UserTokenStats {
total_tokens: tokens.len(),
active_tokens,
total_users: users.len(),
today_requests: 0, // TODO: Implement daily stats query
})
}
|