diff --git a/web/app/Controllers/Admin/CloudPageController.php b/web/app/Controllers/Admin/CloudPageController.php index 2fd4fc1b..34ad0eda 100644 --- a/web/app/Controllers/Admin/CloudPageController.php +++ b/web/app/Controllers/Admin/CloudPageController.php @@ -8,6 +8,7 @@ use App\Services\Cloud\CloudAdminBillingService; use App\Services\Cloud\CloudAdminInstanceService; use App\Services\Cloud\CloudAdminOperationsService; use App\Services\Cloud\CloudAdminResourceService; +use App\Services\Cloud\CloudCountryCatalog; use App\Services\Cloud\CloudError; /** Admin pages use database-only first paints; JSON APIs repeat authorisation. */ @@ -52,7 +53,8 @@ final class CloudPageController extends AdminController public function hosts($request, $response, $args) { - return $this->view()->display('admin/hosts/index.tpl'); + return $this->view()->assign('cloud_country_options', CloudCountryCatalog::options()) + ->display('admin/hosts/index.tpl'); } public function templates($request, $response, $args) diff --git a/web/app/Controllers/Admin/HostController.php b/web/app/Controllers/Admin/HostController.php index 99b1f450..9ec43db9 100644 --- a/web/app/Controllers/Admin/HostController.php +++ b/web/app/Controllers/Admin/HostController.php @@ -3,12 +3,14 @@ namespace App\Controllers\Admin; use App\Controllers\AdminController; +use App\Services\Cloud\CloudCountryCatalog; /** Layout preparation only: no credentials, host mutation or remote connection. */ final class HostController extends AdminController { public function index($request, $response, $args) { - return $this->view()->display('admin/hosts/index.tpl'); + return $this->view()->assign('cloud_country_options', CloudCountryCatalog::options()) + ->display('admin/hosts/index.tpl'); } } diff --git a/web/app/Controllers/CloudAdminController.php b/web/app/Controllers/CloudAdminController.php index 2d3dd312..08bc0387 100644 --- a/web/app/Controllers/CloudAdminController.php +++ b/web/app/Controllers/CloudAdminController.php @@ -4,6 +4,7 @@ namespace App\Controllers; use App\Models\CloudHost; use App\Services\Cloud\CloudAccess; use App\Services\Cloud\CloudAdminHostSchedulingService; +use App\Services\Cloud\CloudCountryCatalog; use App\Services\Cloud\CloudDeploymentPageService; use App\Services\Cloud\CloudEngineClient; use App\Services\Cloud\CloudError; @@ -14,7 +15,6 @@ use App\Services\Cloud\CloudUserIsoFeature; final class CloudAdminController { - private const HOST_COUNTRY_CODES = ['CN', 'JP', 'US', 'SG']; private const CLOUD_IMAGE_UPLOAD_MAX_BYTES = 4 * 1024 * 1024 * 1024; private const CLOUD_IMAGE_UPLOAD_MIN_BYTES = 1024 * 1024; private const CLOUD_IMAGE_VERSIONS = [ @@ -121,10 +121,7 @@ final class CloudAdminController throw new CloudError('具体地区需为 1 至 64 字符,例如 HongKong 或 Tokyo'); } $body['region'] = trim($region); - $countryCode = $body['country_code'] ?? null; - if (!is_string($countryCode) || !in_array($countryCode, self::HOST_COUNTRY_CODES, true)) { - throw new CloudError('国家代码无效,请选择中国、日本、美国或新加坡'); - } + $countryCode = CloudCountryCatalog::normalize($body['country_code'] ?? null); $privateKey = $body['private_key'] ?? ''; if (!is_string($privateKey)) { throw new CloudError('SSH 私钥格式无效'); diff --git a/web/app/Services/Cloud/CloudAdminResourceService.php b/web/app/Services/Cloud/CloudAdminResourceService.php index 72593e51..bedf8773 100644 --- a/web/app/Services/Cloud/CloudAdminResourceService.php +++ b/web/app/Services/Cloud/CloudAdminResourceService.php @@ -37,6 +37,7 @@ final class CloudAdminResourceService return array_merge($catalog, [ 'view' => $view, 'line_feature_enabled' => CloudNetworkLineFeature::enabled(), + 'country_options' => CloudCountryCatalog::options(), 'reservations' => $reservationPage, 'generated_at' => time(), ]); @@ -122,14 +123,9 @@ final class CloudAdminResourceService $region = $id === null ? new CloudRegion() : self::findRegion($id); $code = self::code(self::value($body, 'code', $region, $id === null), 32, '地区代码'); $name = self::name(self::value($body, 'name', $region, $id === null), 80, '地区名称'); - $countryValue = self::value($body, 'country_code', $region, $id === null); - if (!is_string($countryValue)) { - throw new CloudError('国家代码必须为两位 ISO 字母'); - } - $countryCode = strtoupper(self::safeText($countryValue, 2)); - if (preg_match('/^[A-Z]{2}$/D', $countryCode) !== 1) { - throw new CloudError('国家代码必须为两位 ISO 字母'); - } + $countryCode = CloudCountryCatalog::normalize( + self::value($body, 'country_code', $region, $id === null) + ); $status = self::choice(self::value($body, 'status', $region, $id === null), self::REGION_STATUSES, '地区状态'); $sort = self::sort(self::value($body, 'sort', $region, false, 0)); @@ -592,11 +588,14 @@ final class CloudAdminResourceService $regionRows = []; foreach ($regions as $region) { $regionId = (int) $region->id; + $country = CloudCountryCatalog::option((string) $region->country_code); $regionRows[] = [ 'id' => $regionId, 'code' => (string) $region->code, 'name' => (string) $region->name, - 'country_code' => (string) $region->country_code, + 'country_code' => $country['code'], + 'country_name' => $country['name'], + 'country_flag' => $country['flag'], 'status' => (string) $region->status, 'sort' => (int) $region->sort, 'line_count' => $regionLineCounts[$regionId] ?? 0, diff --git a/web/app/Services/Cloud/CloudCountryCatalog.php b/web/app/Services/Cloud/CloudCountryCatalog.php new file mode 100644 index 00000000..9ab6fa82 --- /dev/null +++ b/web/app/Services/Cloud/CloudCountryCatalog.php @@ -0,0 +1,57 @@ + ['name' => '中国', 'flag' => '🇨🇳'], + 'HK' => ['name' => '中国香港', 'flag' => '🇭🇰'], + 'JP' => ['name' => '日本', 'flag' => '🇯🇵'], + 'US' => ['name' => '美国', 'flag' => '🇺🇸'], + 'SG' => ['name' => '新加坡', 'flag' => '🇸🇬'], + ]; + + public static function options(): array + { + $options = []; + foreach (self::COUNTRIES as $code => $country) { + $options[] = [ + 'code' => $code, + 'name' => $country['name'], + 'flag' => $country['flag'], + ]; + } + return $options; + } + + public static function normalize($value): string + { + if (!is_string($value)) { + throw new CloudError('国家/地区无效,请从列表中选择'); + } + $code = strtoupper(trim($value)); + if (!array_key_exists($code, self::COUNTRIES)) { + throw new CloudError('国家/地区无效,请从列表中选择'); + } + return $code; + } + + public static function option($value): array + { + $code = is_string($value) ? strtoupper(trim($value)) : ''; + if (!array_key_exists($code, self::COUNTRIES)) { + return [ + 'code' => $code, + 'name' => '未支持的国家 / 地区', + 'flag' => '🌐', + ]; + } + return [ + 'code' => $code, + 'name' => self::COUNTRIES[$code]['name'], + 'flag' => self::COUNTRIES[$code]['flag'], + ]; + } +} diff --git a/web/public/cloud/admin-resources.js b/web/public/cloud/admin-resources.js index 82f3cd8b..e08a6392 100644 --- a/web/public/cloud/admin-resources.js +++ b/web/public/cloud/admin-resources.js @@ -109,6 +109,28 @@ function configureEditor(kind, item = null) { ? '排空会阻止新实例进入,但已有实例继续运行;停用用于长期下线。' : '停用会阻止新销售和新调度,但不会删除历史记录。'; + const codePresentation = { + region: { + label: '地区标识', + placeholder: '例如 jp-tokyo', + help: '地区的内部唯一标识,仅使用英文字母、数字和中划线;保存时转换为小写,创建后不建议修改。', + }, + line: { + label: '线路标识', + placeholder: '例如 standard', + help: '线路的内部唯一标识,仅使用英文字母、数字和中划线;保存时转换为小写,创建后不建议修改。', + }, + pool: { + label: '资源池标识', + placeholder: '例如 jp-standard-1', + help: '资源池的内部唯一标识,仅使用英文字母、数字和中划线;保存时转换为小写,创建后不建议修改。', + }, + }[kind]; + const codeInput = form.elements.namedItem('code'); + byId('resource-code-label').textContent = codePresentation.label; + byId('resource-code-help').textContent = codePresentation.help; + codeInput.placeholder = codePresentation.placeholder; + const visible = new Set({ region: ['country_code'], line: ['region_id', 'is_default'], pool: ['line_id', 'scheduling_policy'] }[kind]); for (const field of form.querySelectorAll('[data-resource-field]')) { const enabled = visible.has(field.dataset.resourceField); @@ -159,7 +181,7 @@ byId('resource-editor-form')?.addEventListener('submit', async event => { if (!meta) return setEditorNotice('资源类型无效,请刷新页面。', 'error'); const id = form.elements.namedItem('id').value; const body = { - code: form.elements.namedItem('code').value.trim(), + code: form.elements.namedItem('code').value.trim().toLowerCase(), name: form.elements.namedItem('name').value.trim(), status: form.elements.namedItem('status').value, sort: Number(form.elements.namedItem('sort').value), diff --git a/web/resources/views/venix/admin/cloud/resources.tpl b/web/resources/views/venix/admin/cloud/resources.tpl index 7973e23f..7b576b1b 100644 --- a/web/resources/views/venix/admin/cloud/resources.tpl +++ b/web/resources/views/venix/admin/cloud/resources.tpl @@ -2,7 +2,7 @@ {assign var=admin_active value='resources'} {include file='admin/static_header.tpl'} {if !$resource_page} - {assign var=resource_page value=['view'=>'overview','line_feature_enabled'=>false,'summary'=>['regions'=>0,'active_regions'=>0,'lines'=>0,'active_pools'=>0,'hosts'=>0,'schedulable_hosts'=>0,'held_reservations'=>0,'uncertain_reservations'=>0,'capacity'=>['cpu'=>['configured'=>0,'allocated'=>0,'reserved'=>0,'free'=>0],'memory'=>['configured'=>0,'allocated'=>0,'reserved'=>0,'free'=>0],'disk'=>['configured'=>0,'allocated'=>0,'reserved'=>0,'free'=>0],'ip'=>['total'=>0,'allocated'=>0,'reserved'=>0,'free'=>0]]],'regions'=>[],'lines'=>[],'pools'=>[],'reservations'=>['items'=>[],'filters'=>['state'=>'','pool_id'=>'','q'=>''],'pagination'=>['page'=>1,'per_page'=>20,'total'=>0,'total_pages'=>1,'from'=>0,'to'=>0]]]} + {assign var=resource_page value=['view'=>'overview','line_feature_enabled'=>false,'summary'=>['regions'=>0,'active_regions'=>0,'lines'=>0,'active_pools'=>0,'hosts'=>0,'schedulable_hosts'=>0,'held_reservations'=>0,'uncertain_reservations'=>0,'capacity'=>['cpu'=>['configured'=>0,'allocated'=>0,'reserved'=>0,'free'=>0],'memory'=>['configured'=>0,'allocated'=>0,'reserved'=>0,'free'=>0],'disk'=>['configured'=>0,'allocated'=>0,'reserved'=>0,'free'=>0],'ip'=>['total'=>0,'allocated'=>0,'reserved'=>0,'free'=>0]]],'country_options'=>[],'regions'=>[],'lines'=>[],'pools'=>[],'reservations'=>['items'=>[],'filters'=>['state'=>'','pool_id'=>'','q'=>''],'pagination'=>['page'=>1,'per_page'=>20,'total'=>0,'total_pages'=>1,'from'=>0,'to'=>0]]]} {/if}
@@ -38,9 +38,9 @@ {elseif $resource_page.view == 'regions'}
-

地区

地区是用户选择的销售位置;代码创建后应保持稳定。
{if $admin_can_manage|default:false}{/if}
-
{if $admin_can_manage|default:false}{/if} - {foreach $resource_page.regions as $region}{if $admin_can_manage|default:false}{/if}{foreachelse}{/foreach} +

地区

地区是用户选择的销售位置;地区标识创建后应保持稳定。
{if $admin_can_manage|default:false}{/if}
+
名称代码国家线路资源池销售方案状态操作
{$region.name|escape}{$region.code|escape}{$region.country_code|escape}{$region.line_count|escape}{if $region.active_default_line_count != 1}默认线路配置待修正{/if}{$region.pool_count|escape}{$region.offer_count|escape}{if $region.status == 'active'}启用{else}停用{/if}
尚未建立地区。
{if $admin_can_manage|default:false}{/if} + {foreach $resource_page.regions as $region}{if $admin_can_manage|default:false}{/if}{foreachelse}{/foreach}
显示名称地区标识国家 / 地区线路资源池销售方案状态操作
{$region.name|escape}{$region.code|escape}{$region.country_flag|default:'🌐'|escape} {$region.country_name|default:'未识别'|escape} ({$region.country_code|escape}){$region.line_count|escape}{if $region.active_default_line_count != 1}默认线路配置待修正{/if}{$region.pool_count|escape}{$region.offer_count|escape}{if $region.status == 'active'}启用{else}停用{/if}
尚未建立地区。
{elseif $resource_page.view == 'lines'} @@ -76,9 +76,9 @@
- + - + diff --git a/web/resources/views/venix/admin/hosts/index.tpl b/web/resources/views/venix/admin/hosts/index.tpl index 68d1f03c..fbdc000e 100644 --- a/web/resources/views/venix/admin/hosts/index.tpl +++ b/web/resources/views/venix/admin/hosts/index.tpl @@ -83,7 +83,7 @@
-
+
diff --git a/web/resources/views/venix/layout_assets.tpl b/web/resources/views/venix/layout_assets.tpl index 287678f6..4f766883 100644 --- a/web/resources/views/venix/layout_assets.tpl +++ b/web/resources/views/venix/layout_assets.tpl @@ -1,2 +1,2 @@ {* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *} -{assign var=venix_asset_version value='2026100313' scope='global'} +{assign var=venix_asset_version value='2026100314' scope='global'} diff --git a/web/tests/AdminResourceSchedulingContractTest.php b/web/tests/AdminResourceSchedulingContractTest.php index e4e39165..186f4475 100644 --- a/web/tests/AdminResourceSchedulingContractTest.php +++ b/web/tests/AdminResourceSchedulingContractTest.php @@ -20,6 +20,7 @@ $middleware = $read($root . '/app/Middleware/CloudApi.php'); $pageController = $read($root . '/app/Controllers/Admin/CloudPageController.php'); $apiController = $read($root . '/app/Controllers/CloudAdminResourceController.php'); $service = $read($root . '/app/Services/Cloud/CloudAdminResourceService.php'); +$countryCatalog = $read($root . '/app/Services/Cloud/CloudCountryCatalog.php'); $placement = $read($root . '/app/Services/Cloud/CloudPlacementService.php'); $worker = $read($root . '/app/Services/Cloud/CloudWorkerService.php'); $feature = $read($root . '/app/Services/Cloud/CloudNetworkLineFeature.php'); @@ -59,6 +60,13 @@ $assert(str_contains($feature, "CONFIG_KEY = 'cloud_network_lines_enabled'") && str_contains($service, 'CloudHostCapacitySnapshot') && !str_contains($service, 'CloudEngineClient'), 'Resource projections must be DB-only, reserve capacity explicitly and retain a disabled-by-default line feature.'); +$assert(str_contains($service, "'country_options' => CloudCountryCatalog::options()") + && str_contains($service, 'CloudCountryCatalog::normalize(') + && str_contains($service, "'country_name' => \$country['name']") + && str_contains($service, "'country_flag' => \$country['flag']") + && str_contains($countryCatalog, '$code = strtoupper(trim($value));') + && !str_contains($service, 'strtoupper(self::safeText($countryValue, 2))'), + 'Regions must use the shared country catalog and reject rather than truncate invalid input.'); $assert(str_contains($placement, "->whereIn('state', ['held', 'uncertain', 'committed'])") && str_contains($placement, "->orderBy('id')->lockForUpdate()->get([") @@ -111,6 +119,16 @@ $assert(str_contains($script, 'window.VenixDrawerMotion') && !str_contains($script, 'is_super_admin') && !str_contains($script, 'data-can-manage'), 'Resource JavaScript must use shared motion and CSRF without duplicating authorization rules.'); +$assert(str_contains($template, '国家 / 地区') + && str_contains($template, '{foreach $resource_page.country_options as $country}') + && str_contains($template, 'id="resource-code-label"') + && str_contains($template, 'id="resource-code-help"') + && str_contains($template, 'pattern="[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?"') + && str_contains($script, "label: '地区标识'") + && str_contains($script, "label: '线路标识'") + && str_contains($script, "label: '资源池标识'") + && str_contains($script, '.value.trim().toLowerCase()'), + 'Resource forms must explain identifiers, provide readable countries and normalize codes consistently.'); $assert(str_contains($styles, '[data-admin-resources] .admin-resource-tabs') && str_contains($styles, '[data-admin-resource-drawer] .admin-resource-form-fields') diff --git a/web/tests/CloudCountryCatalogTest.php b/web/tests/CloudCountryCatalogTest.php new file mode 100644 index 00000000..51169b65 --- /dev/null +++ b/web/tests/CloudCountryCatalogTest.php @@ -0,0 +1,40 @@ +httpStatus === 422, + 'Invalid country input must remain a validation error.'); + } +} + +$known = CloudCountryCatalog::option('hk'); +$assert($known['code'] === 'HK' && $known['name'] === '中国香港' + && $known['flag'] === '🇭🇰', + 'Known country metadata did not round-trip through the shared catalog.'); +$unsupported = CloudCountryCatalog::option('de'); +$assert($unsupported['code'] === 'DE' && $unsupported['flag'] === '🌐', + 'Historical unsupported codes must remain readable without crashing the resource page.'); + +echo "Cloud country catalog checks passed.\n"; diff --git a/web/tests/CloudHostRegionDefinitionTest.php b/web/tests/CloudHostRegionDefinitionTest.php index 3e6a2869..a825af25 100644 --- a/web/tests/CloudHostRegionDefinitionTest.php +++ b/web/tests/CloudHostRegionDefinitionTest.php @@ -4,6 +4,9 @@ $root = dirname(__DIR__); $schema = file_get_contents($root . '/sql/cloud.sql'); $controller = file_get_contents($root . '/app/Controllers/CloudAdminController.php'); +$pageController = file_get_contents($root . '/app/Controllers/Admin/CloudPageController.php'); +$legacyPageController = file_get_contents($root . '/app/Controllers/Admin/HostController.php'); +$catalog = file_get_contents($root . '/app/Services/Cloud/CloudCountryCatalog.php'); $template = file_get_contents($root . '/resources/views/venix/admin/hosts/index.tpl'); $client = file_get_contents($root . '/public/cloud/admin.js'); @@ -17,9 +20,10 @@ $assert(str_contains($schema, "country_code CHAR(2) NOT NULL DEFAULT ''") && str_contains($schema, "ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS\n country_code CHAR(2) NOT NULL DEFAULT '' AFTER region"), 'Fresh and upgraded cloud_hosts schemas must contain the structured country code.'); -$assert(str_contains($controller, - "private const HOST_COUNTRY_CODES = ['CN', 'JP', 'US', 'SG'];") - && str_contains($controller, "'region', 'country_code'") +$assert(str_contains($controller, "'region', 'country_code'") + && str_contains($controller, + "CloudCountryCatalog::normalize(\$body['country_code'] ?? null)") + && !str_contains($controller, 'HOST_COUNTRY_CODES') && str_contains($controller, "unset(\$body['name'], \$body['enabled'], \$body['country_code']);") && str_contains($controller, '$host->country_code = $countryCode;'), @@ -29,10 +33,22 @@ $unsetAt = strpos($controller, $enginePutAt = strpos($controller, "'PUT', 'host', \$actor, \$body"); $assert($unsetAt !== false && $enginePutAt !== false && $unsetAt < $enginePutAt, 'country_code must be removed before the strict engine HostBody receives the request.'); -foreach (['CN', 'JP', 'US', 'SG'] as $code) { - $assert(str_contains($template, '