Просмотр исходного кода

feat(client-tauri): 扫货购买成功时使用账号各自的充值卡价比例计算人民币价格

- 前端在开始扫货时获取选中Steam账号的充值卡价比例
- 新增账号卡价比例映射传递给Rust后端
- 购买成功计算人民币价格时优先使用账号自己的充值卡价比例
- 如果账号没有单独比例则使用全局配置比例作为兜底

Made-with: Cursor
wuzilong 6 месяцев назад
Родитель
Сommit
8f224b3def

+ 2 - 0
client-tauri/src-tauri/src/commands.rs

@@ -4813,6 +4813,7 @@ pub async fn spawn_buff_sweep_local_runner(
     selection_request_id: i64,
     sweep_worker_count: Option<u32>,
     selected_accounts: Option<Vec<String>>,
+    account_card_rate_map: Option<std::collections::HashMap<String, f64>>,
 ) -> Result<(), String> {
     // 扫货启动时,确保用户文件和目录存在
     ensure_sweep_data_files_exist()?;
@@ -4824,6 +4825,7 @@ pub async fn spawn_buff_sweep_local_runner(
         selection_request_id,
         sweep_worker_count.unwrap_or(3).max(1),
         selected_accounts.unwrap_or_default(),
+        account_card_rate_map.unwrap_or_default(),
     )
     .await
 }

+ 26 - 5
client-tauri/src-tauri/src/commands/buff_sweep_runner.rs

@@ -59,6 +59,19 @@ fn get_cached_steam_card_ratio(selection_request_id: i64) -> f64 {
         .unwrap_or(7.5)  // 默认卡价比例 7.5
 }
 
+/// 根据账号名获取对应的卡价比例
+/// 优先使用账号自己的充值卡价比例,如果没有则使用全局配置的卡价比例
+fn get_account_card_rate(account_name: &str, account_card_rate_map: &std::collections::HashMap<String, f64>, selection_request_id: i64) -> f64 {
+    // 优先使用账号自己的卡价比例
+    if let Some(&rate) = account_card_rate_map.get(account_name) {
+        if rate > 0.0 {
+            return rate;
+        }
+    }
+    // 兜底使用全局配置的卡价比例
+    get_cached_steam_card_ratio(selection_request_id)
+}
+
 /// 存储选品配置到缓存
 fn store_selection_config_cache(selection_request_id: i64, steam_card_ratio: f64) {
     let mut cache = SELECTION_CONFIG_CACHE.lock().unwrap();
@@ -2345,6 +2358,9 @@ struct SweepShared {
     selected_accounts: Vec<String>,
     /// selected_accounts 的长度(用于计算)
     selected_accounts_len: usize,
+    /// 账号充值卡价比例映射(key: 账号名, value: 卡价比例)
+    /// 如果账号没有单独的比例,使用全局默认比例
+    account_card_rate_map: std::collections::HashMap<String, f64>,
 }
 
 fn sweep_proxy_slot(s: Option<&SweepShared>) -> Option<&Arc<Mutex<Option<String>>>> {
@@ -2966,9 +2982,9 @@ async fn purchase_ready_with_account_rotation(
             .and_then(|v| v.as_str())
             .map(|s| s.to_string());
 
-        // 计算成交价格人民币
+        // 计算成交价格人民币(优先使用账号自己的充值卡价比例)
         let purchase_price_cny = purchase_price.map(|price_usd| {
-            let ratio = get_cached_steam_card_ratio(shared.selection_request_id);
+            let ratio = get_account_card_rate(&used_account, &shared.account_card_rate_map, shared.selection_request_id);
             price_usd * ratio
         });
 
@@ -3130,9 +3146,9 @@ async fn purchase_ready(shared: Arc<SweepShared>, ready: PurchaseReady) {
             .and_then(|v| v.as_str())
             .map(|s| s.to_string());
 
-        // 计算成交价格人民币
+        // 计算成交价格人民币(优先使用账号自己的充值卡价比例)
         let purchase_price_cny = purchase_price.map(|price_usd| {
-            let ratio = get_cached_steam_card_ratio(shared.selection_request_id);
+            let ratio = get_account_card_rate(&used_account, &shared.account_card_rate_map, shared.selection_request_id);
             price_usd * ratio
         });
 
@@ -3276,6 +3292,7 @@ async fn run_sweep_inner(
     selection_request_id: i64,
     sweep_worker_count: u32,
     selected_accounts: Vec<String>,
+    account_card_rate_map: std::collections::HashMap<String, f64>,
 ) {
     app_log::info(&format!(
         "[RUN-SWEEP-INNER] 扫货任务线程启动: sweep_request_id={}, selection_request_id={}, worker_count={}, selected_accounts={:?}",
@@ -3654,6 +3671,7 @@ async fn run_sweep_inner(
             purchase_failed_total: Arc::clone(&purchase_failed_total),
             selected_accounts: selected_accounts_vec.clone(),
             selected_accounts_len,
+            account_card_rate_map: account_card_rate_map.clone(),
         })
     };
 
@@ -3686,6 +3704,7 @@ async fn run_sweep_inner(
         purchase_failed_total: Arc::clone(&purchase_failed_total),
         selected_accounts: selected_accounts_vec.clone(),
         selected_accounts_len,
+        account_card_rate_map: account_card_rate_map.clone(),
     });
 
     let init_line = json!({
@@ -3894,6 +3913,7 @@ pub async fn spawn_runner(
     selection_request_id: i64,
     sweep_worker_count: u32,
     selected_accounts: Vec<String>,
+    account_card_rate_map: std::collections::HashMap<String, f64>,
 ) -> Result<(), String> {
     // 任务启动时记录完整资源状态
     app_log::log_resource_diagnostics("spawn-start");
@@ -3924,6 +3944,7 @@ pub async fn spawn_runner(
     let mid = machine_code.clone();
     let wc = sweep_worker_count.max(1);
     let sel_accs = selected_accounts.clone();
+    let acc_card_rates = account_card_rate_map.clone();
 
     app_log::log_sweep_start(sweep_request_id, selection_request_id, wc);
 
@@ -3939,7 +3960,7 @@ pub async fn spawn_runner(
         app_log::log_resource_diagnostics("tokio-task-start");
         
         // 直接调用(async 函数中的 panic 会被 tokio 捕获并记录)
-        run_sweep_inner(sid, mid, sweep_request_id, selection_request_id, wc, sel_accs).await;
+        run_sweep_inner(sid, mid, sweep_request_id, selection_request_id, wc, sel_accs, acc_card_rates).await;
         
         app_log::info(&format!("[SPAWN-RUNNER] run_sweep_inner 执行完毕(正常返回)"));
         app_log::log_sweep_end(sweep_request_id, "completed");

+ 4 - 2
client-tauri/src/utils/secureApiClient.ts

@@ -1199,7 +1199,8 @@ class SecureApiClient {
     sweepRequestId: number,
     selectionRequestId: number,
     sweepWorkerCount?: number,
-    selectedAccounts?: string[]
+    selectedAccounts?: string[],
+    accountCardRateMap?: Record<string, number>
   ): Promise<{ ok: boolean; msg?: string }> {
     try {
       await invoke<void>('spawn_buff_sweep_local_runner', {
@@ -1208,7 +1209,8 @@ class SecureApiClient {
         sweepRequestId,
         selectionRequestId,
         sweepWorkerCount: sweepWorkerCount ?? 3,
-        selectedAccounts: selectedAccounts ?? []
+        selectedAccounts: selectedAccounts ?? [],
+        accountCardRateMap: accountCardRateMap ?? {}
       })
       return { ok: true }
     } catch (error: any) {

+ 9 - 1
client-tauri/src/views/BuffSelectionView.vue

@@ -3392,6 +3392,13 @@ async function handleStartSweep(isAuto: boolean, sourceSelectionRequestId?: numb
     if (!isAuto) message.warning('请至少勾选一个 Steam 账号后再开始扫货')
     return
   }
+  // 构建选中账号的充值卡价比例映射
+  const accountCardRateMap: Record<string, number> = {}
+  for (const account of boundSteamAccounts.value) {
+    if (selectedBoundSteamUsernames.value.has(account.steam_username) && account.cardRate != null) {
+      accountCardRateMap[account.steam_username] = account.cardRate
+    }
+  }
   // 验证系统配置限制
   const maxWorkerCount = configStore.config.buff_sweep_max_worker_count ?? 32
   const minSteamCardRatio = configStore.config.buff_steam_card_ratio_min ?? 4
@@ -3468,7 +3475,8 @@ async function handleStartSweep(isAuto: boolean, sourceSelectionRequestId?: numb
         sweepRid,
         selRid,
         wc,
-        selectedAccounts
+        selectedAccounts,
+        accountCardRateMap
       )
       if (!spawned.ok && !isAuto) {
         message.warning(spawned.msg || '本地扫货引擎未启动,请稍后重试')