mirror of
https://github.com/SECTL/SecScore.git
synced 2026-07-19 12:34:22 +08:00
fix: 修复云空间用量Failed to fetch
This commit is contained in:
@@ -707,6 +707,63 @@ pub struct OnlineStatusResponse {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OAuthStorageUsageResponse {
|
||||
pub used_storage: i64,
|
||||
pub used_storage_formatted: String,
|
||||
pub total_storage: i64,
|
||||
pub total_storage_formatted: String,
|
||||
pub available_storage: i64,
|
||||
pub available_storage_formatted: String,
|
||||
pub percentage: f64,
|
||||
pub file_count: i64,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn oauth_get_storage_usage(
|
||||
access_token: String,
|
||||
platform_id: String,
|
||||
user_id: String,
|
||||
state: State<'_, Arc<RwLock<AppState>>>,
|
||||
) -> Result<IpcResponse<OAuthStorageUsageResponse>, String> {
|
||||
let state_guard = state.read();
|
||||
let client = &state_guard.http_client;
|
||||
|
||||
let response = client
|
||||
.get("https://appwrite.sectl.top/api/cloud/storage/usage")
|
||||
.query(&[("client_id", platform_id), ("user_id", user_id)])
|
||||
.header("Authorization", format!("Bearer {}", access_token))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Request failed: {}", e))?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let error_text = response
|
||||
.text()
|
||||
.await
|
||||
.unwrap_or_else(|_| "Unknown error".to_string());
|
||||
let error_message =
|
||||
if let Ok(json_err) = serde_json::from_str::<serde_json::Value>(&error_text) {
|
||||
json_err
|
||||
.get("error_description")
|
||||
.or_else(|| json_err.get("error"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or(&error_text)
|
||||
.to_string()
|
||||
} else {
|
||||
error_text
|
||||
};
|
||||
return Ok(IpcResponse::error(&error_message));
|
||||
}
|
||||
|
||||
let storage_usage: OAuthStorageUsageResponse = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to parse response: {}", e))?;
|
||||
|
||||
Ok(IpcResponse::success(storage_usage))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn oauth_report_online(
|
||||
platform_id: String,
|
||||
@@ -898,3 +955,4 @@ pub async fn oauth_refresh_access_token(
|
||||
|
||||
Ok(IpcResponse::success(token_response))
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +86,7 @@ pub fn run() {
|
||||
oauth_stop_callback_server,
|
||||
oauth_report_online,
|
||||
oauth_get_device_uuid,
|
||||
oauth_get_storage_usage,
|
||||
oauth_save_login_state,
|
||||
oauth_load_login_state,
|
||||
oauth_clear_login_state,
|
||||
@@ -449,3 +450,4 @@ fn setup_window_events(app: &mut App) -> Result<(), Box<dyn std::error::Error>>
|
||||
fn setup_window_events(_app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -206,7 +206,7 @@ export function ContentArea({
|
||||
|
||||
try {
|
||||
const api = (window as any).api
|
||||
if (!api?.oauthLoadLoginState) {
|
||||
if (!api?.oauthLoadLoginState || !api?.oauthGetStorageUsage) {
|
||||
setStorageUsage(null)
|
||||
setStorageUsageError("当前环境不支持云用量查询")
|
||||
return
|
||||
@@ -229,37 +229,24 @@ export function ContentArea({
|
||||
return
|
||||
}
|
||||
|
||||
const params = new URLSearchParams({
|
||||
client_id: platformId,
|
||||
user_id: oauthState.user_id,
|
||||
})
|
||||
|
||||
const response = await fetch(
|
||||
`https://appwrite.sectl.top/api/cloud/storage/usage?${params.toString()}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${oauthState.access_token}`,
|
||||
},
|
||||
}
|
||||
const usageRes = await api.oauthGetStorageUsage(
|
||||
oauthState.access_token,
|
||||
platformId,
|
||||
oauthState.user_id
|
||||
)
|
||||
|
||||
const responseText = await response.text()
|
||||
let payload: any = {}
|
||||
try {
|
||||
payload = responseText ? JSON.parse(responseText) : {}
|
||||
} catch {
|
||||
payload = {}
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(payload?.error_description || payload?.message || "获取云空间用量失败")
|
||||
if (!usageRes?.success || !usageRes.data) {
|
||||
throw new Error(usageRes?.message || "获取云空间用量失败")
|
||||
}
|
||||
|
||||
setStorageUsage({
|
||||
used_storage_formatted: String(payload?.used_storage_formatted || "0 B"),
|
||||
total_storage_formatted: String(payload?.total_storage_formatted || "0 B"),
|
||||
percentage: Number.isFinite(payload?.percentage) ? Number(payload.percentage) : 0,
|
||||
file_count: Number.isFinite(payload?.file_count) ? Number(payload.file_count) : 0,
|
||||
used_storage_formatted: String(usageRes.data.used_storage_formatted || "0 B"),
|
||||
total_storage_formatted: String(usageRes.data.total_storage_formatted || "0 B"),
|
||||
percentage: Number.isFinite(usageRes.data.percentage)
|
||||
? Number(usageRes.data.percentage)
|
||||
: 0,
|
||||
file_count: Number.isFinite(usageRes.data.file_count)
|
||||
? Number(usageRes.data.file_count)
|
||||
: 0,
|
||||
})
|
||||
} catch (error: any) {
|
||||
setStorageUsage(null)
|
||||
@@ -697,3 +684,4 @@ export function ContentArea({
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -565,6 +565,24 @@ const api = {
|
||||
data: string
|
||||
message?: string
|
||||
}> => invoke("oauth_get_device_uuid"),
|
||||
oauthGetStorageUsage: (
|
||||
accessToken: string,
|
||||
platformId: string,
|
||||
userId: string
|
||||
): Promise<{
|
||||
success: boolean
|
||||
data: {
|
||||
used_storage: number
|
||||
used_storage_formatted: string
|
||||
total_storage: number
|
||||
total_storage_formatted: string
|
||||
available_storage: number
|
||||
available_storage_formatted: string
|
||||
percentage: number
|
||||
file_count: number
|
||||
}
|
||||
message?: string
|
||||
}> => invoke("oauth_get_storage_usage", { accessToken, platformId, userId }),
|
||||
oauthSaveLoginState: (state: {
|
||||
access_token: string
|
||||
refresh_token: string
|
||||
@@ -846,3 +864,4 @@ const api = {
|
||||
|
||||
export default api
|
||||
export { api }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user