File size: 7,314 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | import i18n from '../i18n';
import { Account, DeviceProfile, DeviceProfileVersion, QuotaData } from '../types/account';
import { request as invoke } from '../utils/request';
// 检查环境 (可选)
function ensureTauriEnvironment() {
// Web 模式下 request 也是一个 function,所以这里不应抛错
if (typeof invoke !== 'function') {
throw new Error(i18n.t('common.tauri_api_not_loaded'));
}
}
export async function listAccounts(): Promise<Account[]> {
const response = await invoke<any>('list_accounts');
// 如果返回的是对象格式 { accounts: [...] }, 则取其 accounts 属性
if (response && typeof response === 'object' && Array.isArray(response.accounts)) {
return response.accounts;
}
// 否则直接返回响应内容(假设为数组)
return response || [];
}
export async function getCurrentAccount(): Promise<Account | null> {
return await invoke('get_current_account');
}
export async function addAccount(email: string, refreshToken: string): Promise<Account> {
return await invoke('add_account', { email, refreshToken });
}
export async function deleteAccount(accountId: string): Promise<void> {
return await invoke('delete_account', { accountId });
}
export async function deleteAccounts(accountIds: string[]): Promise<void> {
return await invoke('delete_accounts', { accountIds });
}
export async function switchAccount(accountId: string): Promise<void> {
return await invoke('switch_account', { accountId });
}
export async function fetchAccountQuota(accountId: string): Promise<QuotaData> {
return await invoke('fetch_account_quota', { accountId });
}
export interface RefreshStats {
total: number;
success: number;
failed: number;
details: string[];
}
export async function refreshAllQuotas(): Promise<RefreshStats> {
return await invoke('refresh_all_quotas');
}
// OAuth
export async function startOAuthLogin(oauthClientKey?: string): Promise<Account> {
ensureTauriEnvironment();
try {
return await invoke('start_oauth_login', oauthClientKey ? { oauthClientKey } : undefined);
} catch (error) {
// 增强错误信息
if (typeof error === 'string') {
// 如果是 refresh_token 缺失错误,保持原样(已包含详细说明)
if (error.includes('Refresh Token') || error.includes('refresh_token')) {
throw error;
}
// 其他错误添加上下文
throw i18n.t('accounts.add.oauth_error', { error });
}
throw error;
}
}
export async function completeOAuthLogin(): Promise<Account> {
ensureTauriEnvironment();
try {
return await invoke('complete_oauth_login');
} catch (error) {
if (typeof error === 'string') {
if (error.includes('Refresh Token') || error.includes('refresh_token')) {
throw error;
}
throw i18n.t('accounts.add.oauth_error', { error });
}
throw error;
}
}
export async function cancelOAuthLogin(): Promise<void> {
ensureTauriEnvironment();
return await invoke('cancel_oauth_login');
}
export interface OAuthClientInfo {
key: string;
label: string;
client_id: string;
is_active: boolean;
is_builtin: boolean;
}
export async function listOAuthClients(): Promise<OAuthClientInfo[]> {
const res = await invoke<{ clients: OAuthClientInfo[] }>('list_oauth_clients');
if (res && Array.isArray(res.clients)) return res.clients;
return (res as unknown as OAuthClientInfo[]) || [];
}
export async function getActiveOAuthClient(): Promise<string> {
const res = await invoke<{ client_key: string } | string>('get_active_oauth_client');
if (typeof res === 'string') return res;
return res?.client_key || '';
}
export async function setActiveOAuthClient(clientKey: string): Promise<void> {
return await invoke('set_active_oauth_client', { clientKey });
}
// 导入
export async function importV1Accounts(): Promise<Account[]> {
return await invoke('import_v1_accounts');
}
export async function importFromDb(): Promise<Account> {
return await invoke('import_from_db');
}
export async function importFromCustomDb(path: string): Promise<Account> {
return await invoke('import_custom_db', { path });
}
export async function syncAccountFromDb(): Promise<Account | null> {
return await invoke('sync_account_from_db');
}
export async function toggleProxyStatus(accountId: string, enable: boolean, reason?: string): Promise<void> {
return await invoke('toggle_proxy_status', { accountId, enable, reason });
}
/**
* 重新排序账号列表
* @param accountIds 按新顺序排列的账号ID数组
*/
export async function reorderAccounts(accountIds: string[]): Promise<void> {
return await invoke('reorder_accounts', { accountIds });
}
// 设备指纹相关
export interface DeviceProfilesResponse {
current_storage?: DeviceProfile;
history?: DeviceProfileVersion[];
baseline?: DeviceProfile;
}
export async function getDeviceProfiles(accountId: string): Promise<DeviceProfilesResponse> {
return await invoke('get_device_profiles', { accountId });
}
export async function bindDeviceProfile(accountId: string, mode: 'capture' | 'generate'): Promise<DeviceProfile> {
return await invoke('bind_device_profile', { accountId, mode });
}
export async function restoreOriginalDevice(): Promise<string> {
return await invoke('restore_original_device');
}
export async function listDeviceVersions(accountId: string): Promise<DeviceProfilesResponse> {
return await invoke('list_device_versions', { accountId });
}
export async function restoreDeviceVersion(accountId: string, versionId: string): Promise<DeviceProfile> {
return await invoke('restore_device_version', { accountId, versionId });
}
export async function deleteDeviceVersion(accountId: string, versionId: string): Promise<void> {
return await invoke('delete_device_version', { accountId, versionId });
}
export async function openDeviceFolder(): Promise<void> {
return await invoke('open_device_folder');
}
export async function previewGenerateProfile(): Promise<DeviceProfile> {
return await invoke('preview_generate_profile');
}
export async function bindDeviceProfileWithProfile(accountId: string, profile: DeviceProfile): Promise<DeviceProfile> {
return await invoke('bind_device_profile_with_profile', { accountId, profile });
}
// 预热相关
export async function warmUpAllAccounts(): Promise<string> {
return await invoke('warm_up_all_accounts');
}
export async function warmUpAccount(accountId: string): Promise<string> {
return await invoke('warm_up_account', { accountId });
}
// 导出账号相关
export interface ExportAccountItem {
email: string;
refresh_token: string;
}
export interface ExportAccountsResponse {
accounts: ExportAccountItem[];
}
export async function exportAccounts(accountIds: string[]): Promise<ExportAccountsResponse> {
return await invoke('export_accounts', { accountIds });
}
// 自定义标签相关
export async function updateAccountLabel(accountId: string, label: string): Promise<void> {
return await invoke('update_account_label', { accountId, label });
}
|