diff --git a/AliyunService.php b/AliyunService.php index 3a60a3f..1bd37c5 100644 --- a/AliyunService.php +++ b/AliyunService.php @@ -89,12 +89,13 @@ class AliyunService /** * 获取 BSS 费用中心 API 的 regionId 和 endpoint - * 中国站(国内区域): cn-hangzhou + business.aliyuncs.com - * 国际站(海外区域): ap-southeast-1 + business.ap-southeast-1.aliyuncs.com + * 中国站: cn-hangzhou + business.aliyuncs.com + * 国际站: ap-southeast-1 + business.ap-southeast-1.aliyuncs.com + * @param string $siteType 'china' 或 'international' */ - private function getBssEndpoint($targetRegion) + private function getBssEndpoint($siteType = 'china') { - if ($this->isOverseas($targetRegion)) { + if ($siteType === 'international') { return [ 'regionId' => 'ap-southeast-1', 'host' => 'business.ap-southeast-1.aliyuncs.com' @@ -257,14 +258,14 @@ class AliyunService * @return array ['AvailableAmount' => '...', 'Currency' => 'CNY'] * @throws \Exception */ - public function getAccountBalance($key, $secret, $targetRegion = 'cn-hangzhou') + public function getAccountBalance($key, $secret, $siteType = 'china') { $cacheKey = md5($key); if (isset($this->balanceCache[$cacheKey])) { return $this->balanceCache[$cacheKey]; } - $bss = $this->getBssEndpoint($targetRegion); + $bss = $this->getBssEndpoint($siteType); $result = $this->executeWithRetry(function () use ($key, $secret, $bss) { AlibabaCloud::accessKeyClient($key, $secret) @@ -303,9 +304,9 @@ class AliyunService * @return array ['TotalCost' => float, 'Items' => [...]] * @throws \Exception */ - public function getInstanceBill($key, $secret, $instanceId, $billingCycle, $targetRegion = 'cn-hangzhou') + public function getInstanceBill($key, $secret, $instanceId, $billingCycle, $siteType = 'china') { - $bss = $this->getBssEndpoint($targetRegion); + $bss = $this->getBssEndpoint($siteType); $result = $this->executeWithRetry(function () use ($key, $secret, $instanceId, $billingCycle, $bss) { AlibabaCloud::accessKeyClient($key, $secret) @@ -363,9 +364,9 @@ class AliyunService * @return array ['TotalCost' => float, 'Products' => [...]] * @throws \Exception */ - public function getBillOverview($key, $secret, $billingCycle, $targetRegion = 'cn-hangzhou') + public function getBillOverview($key, $secret, $billingCycle, $siteType = 'china') { - $bss = $this->getBssEndpoint($targetRegion); + $bss = $this->getBssEndpoint($siteType); $result = $this->executeWithRetry(function () use ($key, $secret, $billingCycle, $bss) { AlibabaCloud::accessKeyClient($key, $secret) diff --git a/AliyunTrafficCheck.php b/AliyunTrafficCheck.php index d2e6feb..be55517 100644 --- a/AliyunTrafficCheck.php +++ b/AliyunTrafficCheck.php @@ -159,7 +159,8 @@ class AliyunTrafficCheck 'startTime' => $row['start_time'], 'stopTime' => $row['stop_time'] ], - 'remark' => $row['remark'] ?? '' + 'remark' => $row['remark'] ?? '', + 'siteType' => $row['site_type'] ?? 'china' ]; } @@ -549,7 +550,7 @@ class AliyunTrafficCheck $balance = $this->aliyunService->getAccountBalance( $targetAccount['access_key_id'], $targetAccount['access_key_secret'], - $targetAccount['region_id'] + $targetAccount['site_type'] ?? 'china' ); $this->db->setBillingCache($targetAccount['id'], 'balance', '', $balance); } catch (\Exception $e) { @@ -567,7 +568,7 @@ class AliyunTrafficCheck $targetAccount['access_key_secret'], $targetAccount['instance_id'], $billingCycle, - $targetAccount['region_id'] + $targetAccount['site_type'] ?? 'china' ); $this->db->setBillingCache($targetAccount['id'], 'instance_bill', $billingCycle, $bill); } catch (\Exception $e) { @@ -721,7 +722,7 @@ class AliyunTrafficCheck $balance = $this->aliyunService->getAccountBalance( $account['access_key_id'], $account['access_key_secret'], - $account['region_id'] + $account['site_type'] ?? 'china' ); $costInfo['balance'] = $balance['AvailableAmount']; $costInfo['currency'] = $balance['Currency'] ?? 'CNY'; @@ -743,7 +744,7 @@ class AliyunTrafficCheck $account['access_key_secret'], $account['instance_id'], $billingCycle, - $account['region_id'] + $account['site_type'] ?? 'china' ); $costInfo['monthly_cost'] = $bill['TotalCost']; $this->db->setBillingCache($account['id'], 'instance_bill', $billingCycle, $bill); diff --git a/ConfigManager.php b/ConfigManager.php index 0597de6..112e67e 100644 --- a/ConfigManager.php +++ b/ConfigManager.php @@ -135,8 +135,8 @@ class ConfigManager } $keptIds = []; - $insertStmt = $this->db->prepare("INSERT INTO accounts (access_key_id, access_key_secret, region_id, instance_id, max_traffic, schedule_enabled, start_time, stop_time, remark, traffic_used, instance_status, updated_at, last_keep_alive_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 'Unknown', 0, 0)"); - $updateStmt = $this->db->prepare("UPDATE accounts SET access_key_secret = ?, region_id = ?, instance_id = ?, max_traffic = ?, schedule_enabled = ?, start_time = ?, stop_time = ?, remark = ? WHERE id = ?"); + $insertStmt = $this->db->prepare("INSERT INTO accounts (access_key_id, access_key_secret, region_id, instance_id, max_traffic, schedule_enabled, start_time, stop_time, remark, site_type, traffic_used, instance_status, updated_at, last_keep_alive_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 'Unknown', 0, 0)"); + $updateStmt = $this->db->prepare("UPDATE accounts SET access_key_secret = ?, region_id = ?, instance_id = ?, max_traffic = ?, schedule_enabled = ?, start_time = ?, stop_time = ?, remark = ?, site_type = ? WHERE id = ?"); foreach ($newAccounts as $acc) { $key = $acc['AccessKeyId']; @@ -152,7 +152,8 @@ class ConfigManager ($acc['schedule']['enabled'] ?? false) ? 1 : 0, $acc['schedule']['startTime'] ?? '', $acc['schedule']['stopTime'] ?? '', - $acc['remark'] ?? '' + $acc['remark'] ?? '', + $acc['siteType'] ?? 'china' ]; if (isset($existingMap[$compositeKey])) { @@ -207,7 +208,7 @@ class ConfigManager $this->db->exec("DELETE FROM accounts"); $this->db->exec("DELETE FROM sqlite_sequence WHERE name='accounts'"); - $insertStmt = $this->db->prepare("INSERT INTO accounts (id, access_key_id, access_key_secret, region_id, instance_id, max_traffic, schedule_enabled, start_time, stop_time, remark, traffic_used, instance_status, updated_at, last_keep_alive_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $insertStmt = $this->db->prepare("INSERT INTO accounts (id, access_key_id, access_key_secret, region_id, instance_id, max_traffic, schedule_enabled, start_time, stop_time, remark, site_type, traffic_used, instance_status, updated_at, last_keep_alive_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $newId = 1; foreach ($rows as $row) { @@ -222,6 +223,7 @@ class ConfigManager $row['start_time'], $row['stop_time'], $row['remark'] ?? '', + $row['site_type'] ?? 'china', $row['traffic_used'], $row['instance_status'], $row['updated_at'], diff --git a/Database.php b/Database.php index c42a033..72b0148 100644 --- a/Database.php +++ b/Database.php @@ -137,6 +137,7 @@ class Database $this->ensureColumn('accounts', 'updated_at', 'INTEGER DEFAULT 0'); $this->ensureColumn('accounts', 'last_keep_alive_at', 'INTEGER DEFAULT 0'); $this->ensureColumn('accounts', 'remark', "TEXT DEFAULT ''"); + $this->ensureColumn('accounts', 'site_type', "TEXT DEFAULT 'china'"); $this->migrateStatsToAccountId(); } diff --git a/template.html b/template.html index 2e15639..baa1b0f 100644 --- a/template.html +++ b/template.html @@ -801,11 +801,20 @@ -