|
|
|
|
@ -3,7 +3,7 @@ import { toast } from './toast.js?v=2026100102';
|
|
|
|
|
// PHP owns authentication, authorisation, ownership and purchasing. No engine
|
|
|
|
|
// address, token, host credential or unrestricted create API exists in this UI.
|
|
|
|
|
const byId = (id) => document.getElementById(id);
|
|
|
|
|
const state = { csrfToken: '', userId: '', username: '', admin: false, vms: [], plans: [], regions: [], sshKeys: [], templates: [], templateCatalogUnavailable: false, resources: null, selectedRegionHostId: '', view: 'list', selectedVmId: null, detailTab: 'operations', metricsRange: '24h', metricsPayload: null, metricsRequest: null, metricsPollTimer: null, metricsResizeTimer: null, trafficDailyPayload: null, trafficDailyVmId: '', trafficDailyRequest: null, trafficDailyResizeTimer: null, trafficDailyLoadedAt: 0, credentialPassword: '', credentialVisible: false, credentialTimer: null, reinstallPassword: '', reinstallPasswordDismissed: false, rfb: null, consoleToken: 0, loadingVMs: false, selectedKeys: { create: '', reinstall: '' }, resourceController: null, mutationBusy: false, provisionIdempotency: '', provisionFingerprint: '', rescueActive: false, rescueStaged: false };
|
|
|
|
|
const state = { csrfToken: '', userId: '', username: '', admin: false, vms: [], offers: [], regions: [], lines: [], linesEnabled: false, sshKeys: [], templates: [], templateCatalogUnavailable: false, resources: null, selectedRegionId: '', selectedLineId: '', view: 'list', selectedVmId: null, detailTab: 'operations', metricsRange: '24h', metricsPayload: null, metricsRequest: null, metricsPollTimer: null, metricsResizeTimer: null, trafficDailyPayload: null, trafficDailyVmId: '', trafficDailyRequest: null, trafficDailyResizeTimer: null, trafficDailyLoadedAt: 0, credentialPassword: '', credentialVisible: false, credentialTimer: null, reinstallPassword: '', reinstallPasswordDismissed: false, rfb: null, consoleToken: 0, loadingVMs: false, selectedKeys: { create: '', reinstall: '' }, resourceController: null, mutationBusy: false, provisionIdempotency: '', provisionFingerprint: '', rescueActive: false, rescueStaged: false };
|
|
|
|
|
// Keep the historical storage key so an interrupted deployment made just
|
|
|
|
|
// before an online update can still reconcile to the same opening invoice.
|
|
|
|
|
const PROVISION_INVOICE_STORAGE_PREFIX = 'virt-cloud:pending-order:';
|
|
|
|
|
@ -16,7 +16,7 @@ const CLOUD_PATHS = Object.freeze({
|
|
|
|
|
const vmPagePath = (id, child = '') => `${CLOUD_PATHS.instances}/${encodeURIComponent(String(id))}${child ? `/${child}` : ''}`;
|
|
|
|
|
const navigate = (path) => location.assign(path);
|
|
|
|
|
const text = (id, value) => { byId(id).textContent = value ?? '—'; };
|
|
|
|
|
const items = (payload) => Array.isArray(payload) ? payload : Array.isArray(payload?.items) ? payload.items : Array.isArray(payload?.plans) ? payload.plans : Array.isArray(payload?.templates) ? payload.templates : Array.isArray(payload?.keys) ? payload.keys : Array.isArray(payload?.vms) ? payload.vms : [];
|
|
|
|
|
const items = (payload) => Array.isArray(payload) ? payload : Array.isArray(payload?.items) ? payload.items : Array.isArray(payload?.templates) ? payload.templates : Array.isArray(payload?.keys) ? payload.keys : Array.isArray(payload?.vms) ? payload.vms : [];
|
|
|
|
|
function readBootstrap(id) {
|
|
|
|
|
const source = byId(id);
|
|
|
|
|
if (!source) return null;
|
|
|
|
|
@ -392,26 +392,60 @@ function flagForCountry(code) {
|
|
|
|
|
return String.fromCodePoint(...Array.from(normalized, character => 127397 + character.charCodeAt(0)));
|
|
|
|
|
}
|
|
|
|
|
function saleRegions() {
|
|
|
|
|
const regions = new Map();
|
|
|
|
|
const regions = [];
|
|
|
|
|
const seen = new Set();
|
|
|
|
|
for (const region of state.regions) {
|
|
|
|
|
const hostId = String(region?.host_id ?? region?.id ?? '');
|
|
|
|
|
if (!/^\d+$/.test(hostId) || regions.has(hostId)) continue;
|
|
|
|
|
regions.set(hostId, {
|
|
|
|
|
hostId,
|
|
|
|
|
const id = String(region?.id ?? '');
|
|
|
|
|
if (!/^[1-9]\d*$/.test(id) || seen.has(id)) continue;
|
|
|
|
|
seen.add(id);
|
|
|
|
|
regions.push({
|
|
|
|
|
id,
|
|
|
|
|
countryCode: String(region.country_code || '').trim().toUpperCase(),
|
|
|
|
|
region: String(region.region || region.name || '地区未设置').trim() || '地区未设置',
|
|
|
|
|
name: String(region.name || '地区未设置').trim() || '地区未设置',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
for (const plan of state.plans) {
|
|
|
|
|
const hostId = String(plan?.host_id || '');
|
|
|
|
|
if (!/^\d+$/.test(hostId) || regions.has(hostId)) continue;
|
|
|
|
|
regions.set(hostId, {
|
|
|
|
|
hostId,
|
|
|
|
|
countryCode: String(plan.country_code || '').trim().toUpperCase(),
|
|
|
|
|
region: String(plan.region || '地区未设置').trim() || '地区未设置',
|
|
|
|
|
return regions;
|
|
|
|
|
}
|
|
|
|
|
function saleLines(regionId = state.selectedRegionId) {
|
|
|
|
|
const lines = [];
|
|
|
|
|
const seen = new Set();
|
|
|
|
|
for (const line of state.lines) {
|
|
|
|
|
const id = String(line?.id ?? '');
|
|
|
|
|
if (!/^[1-9]\d*$/.test(id) || seen.has(id)
|
|
|
|
|
|| String(line?.region_id ?? '') !== String(regionId)
|
|
|
|
|
|| (!state.linesEnabled && line?.is_default !== true)) continue;
|
|
|
|
|
seen.add(id);
|
|
|
|
|
lines.push({
|
|
|
|
|
id,
|
|
|
|
|
regionId: String(line.region_id),
|
|
|
|
|
name: String(line.name || '线路未设置').trim() || '线路未设置',
|
|
|
|
|
isDefault: line.is_default === true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return Array.from(regions.values());
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
function saleOffers(regionId = state.selectedRegionId, lineId = state.selectedLineId) {
|
|
|
|
|
return state.offers.filter(offer => /^[1-9]\d*$/.test(String(offer?.id ?? ''))
|
|
|
|
|
&& String(offer?.region_id ?? '') === String(regionId)
|
|
|
|
|
&& String(offer?.line_id ?? '') === String(lineId)
|
|
|
|
|
&& String(offer?.currency || '').toUpperCase() === 'USD');
|
|
|
|
|
}
|
|
|
|
|
function defaultSaleLine(lines) { return lines.find(line => line.isDefault) || lines[0] || null; }
|
|
|
|
|
function applySaleCatalog(catalog) {
|
|
|
|
|
state.regions = bootstrapItems(catalog, 'regions');
|
|
|
|
|
state.lines = bootstrapItems(catalog, 'lines');
|
|
|
|
|
state.offers = bootstrapItems(catalog, 'offers');
|
|
|
|
|
state.linesEnabled = catalog?.lines_enabled === true;
|
|
|
|
|
const regions = saleRegions();
|
|
|
|
|
const requestedRegionId = String(catalog?.default_region_id ?? '');
|
|
|
|
|
state.selectedRegionId = regions.some(region => region.id === requestedRegionId)
|
|
|
|
|
? requestedRegionId : (regions[0]?.id || '');
|
|
|
|
|
const lines = saleLines(state.selectedRegionId);
|
|
|
|
|
const requestedLineId = String(catalog?.default_line_id ?? '');
|
|
|
|
|
state.selectedLineId = lines.some(line => line.id === requestedLineId)
|
|
|
|
|
? requestedLineId : (defaultSaleLine(lines)?.id || '');
|
|
|
|
|
byId('vm-region').value = state.selectedRegionId;
|
|
|
|
|
byId('vm-line').value = state.selectedLineId;
|
|
|
|
|
}
|
|
|
|
|
function setDeploymentStepEnabled(id, enabled) {
|
|
|
|
|
const step = byId(id);
|
|
|
|
|
@ -420,36 +454,35 @@ function setDeploymentStepEnabled(id, enabled) {
|
|
|
|
|
step.setAttribute('aria-disabled', String(!enabled));
|
|
|
|
|
}
|
|
|
|
|
function updateCreateAvailability() {
|
|
|
|
|
const plan = state.plans.find(item => String(item.id) === byId('vm-plan')?.value);
|
|
|
|
|
const validHost = plan && String(plan.host_id) === state.selectedRegionHostId;
|
|
|
|
|
const offer = saleOffers().find(item => String(item.id) === byId('vm-offer')?.value);
|
|
|
|
|
const validMedia = Boolean(byId('vm-template')?.value);
|
|
|
|
|
if (byId('create-submit')) byId('create-submit').disabled = state.mutationBusy || !validHost || !validMedia;
|
|
|
|
|
if (byId('create-submit')) byId('create-submit').disabled = state.mutationBusy || !offer || !validMedia;
|
|
|
|
|
}
|
|
|
|
|
function strongPassword(value) { return !value || (value.length >= 12 && value.length <= 128 && /^[\x21-\x7e]+$/.test(value) && /[A-Z]/.test(value) && /[a-z]/.test(value) && /[0-9]/.test(value) && /[^A-Za-z0-9]/.test(value)); }
|
|
|
|
|
function formatUsd(value) {
|
|
|
|
|
const amount = Number(value);
|
|
|
|
|
if (!Number.isFinite(amount) || amount < 0) return '$—';
|
|
|
|
|
return `$${amount.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
|
|
|
|
|
if (!Number.isFinite(amount) || amount < 0) return '$— USD';
|
|
|
|
|
return `$${amount.toFixed(2)} USD`;
|
|
|
|
|
}
|
|
|
|
|
function formatMemoryGiB(memoryMib) {
|
|
|
|
|
const amount = Number(memoryMib);
|
|
|
|
|
if (!Number.isFinite(amount) || amount < 0) return '— GB';
|
|
|
|
|
return `${(amount / 1024).toLocaleString('en-US', { maximumFractionDigits: 1 })} GB`;
|
|
|
|
|
}
|
|
|
|
|
function formatTrafficTiBFromGiB(trafficGiB) {
|
|
|
|
|
const amount = Number(trafficGiB);
|
|
|
|
|
function formatTrafficTiB(trafficTiB) {
|
|
|
|
|
const amount = Number(trafficTiB);
|
|
|
|
|
if (!Number.isFinite(amount) || amount < 0) return '—';
|
|
|
|
|
if (amount === 0) return '不限';
|
|
|
|
|
return `${(amount / 1024).toLocaleString('en-US', { maximumFractionDigits: 10 })} TB`;
|
|
|
|
|
return `${amount.toFixed(10).replace(/\.?0+$/, '')} TB`;
|
|
|
|
|
}
|
|
|
|
|
function planValidityDays(plan) { return firstNumber(plan?.validity_days, plan?.service_days, plan?.period_days); }
|
|
|
|
|
function planTrafficBytes(plan) {
|
|
|
|
|
const gib = firstNumber(plan?.traffic_gib);
|
|
|
|
|
return firstNumber(plan?.traffic_quota_bytes, gib === null ? null : gib * 1024 ** 3);
|
|
|
|
|
function offerValidityDays(offer) { return firstNumber(offer?.validity_days, offer?.service_days, offer?.period_days); }
|
|
|
|
|
function offerTrafficBytes(offer) {
|
|
|
|
|
const gib = firstNumber(offer?.traffic_gib);
|
|
|
|
|
return firstNumber(offer?.traffic_quota_bytes, gib === null ? null : gib * 1024 ** 3);
|
|
|
|
|
}
|
|
|
|
|
function planBillingLabel(plan) {
|
|
|
|
|
const days = planValidityDays(plan);
|
|
|
|
|
const traffic = planTrafficBytes(plan);
|
|
|
|
|
function offerBillingLabel(offer) {
|
|
|
|
|
const days = offerValidityDays(offer);
|
|
|
|
|
const traffic = offerTrafficBytes(offer);
|
|
|
|
|
return `${days === null ? '账期未提供' : `${Math.floor(days)} 天/期`} · ${traffic === null ? '每期流量未提供' : `每期流量 ${formatTrafficQuota(traffic)}`}`;
|
|
|
|
|
}
|
|
|
|
|
function resetInstallChoices() {
|
|
|
|
|
@ -474,109 +507,151 @@ function renderRegionPicker() {
|
|
|
|
|
const cards = saleRegions().map(region => {
|
|
|
|
|
const button = makeElement('button', 'region-option');
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
button.dataset.hostId = region.hostId;
|
|
|
|
|
button.setAttribute('aria-pressed', String(state.selectedRegionHostId === region.hostId));
|
|
|
|
|
button.setAttribute('aria-label', `选择地区 ${region.region}`);
|
|
|
|
|
button.append(makeElement('span', 'region-flag', flagForCountry(region.countryCode)), makeElement('strong', '', region.region));
|
|
|
|
|
button.addEventListener('click', () => void selectRegion(region.hostId));
|
|
|
|
|
button.dataset.regionId = region.id;
|
|
|
|
|
button.setAttribute('aria-pressed', String(state.selectedRegionId === region.id));
|
|
|
|
|
button.setAttribute('aria-label', `选择地区 ${region.name}`);
|
|
|
|
|
button.append(makeElement('span', 'region-flag', flagForCountry(region.countryCode)), makeElement('strong', '', region.name));
|
|
|
|
|
button.addEventListener('click', () => void selectRegion(region.id));
|
|
|
|
|
return button;
|
|
|
|
|
});
|
|
|
|
|
if (!cards.length) cards.push(makeElement('p', 'template-catalog-empty', '暂无可部署地区,请联系管理员配置在售商品。'));
|
|
|
|
|
container.replaceChildren(...cards);
|
|
|
|
|
byId('vm-region-host').value = state.selectedRegionHostId;
|
|
|
|
|
}
|
|
|
|
|
function renderPlanPicker() {
|
|
|
|
|
const container = byId('vm-plan-picker');
|
|
|
|
|
const selectedPlanId = byId('vm-plan').value;
|
|
|
|
|
const plans = state.plans.filter(plan => String(plan.host_id) === state.selectedRegionHostId);
|
|
|
|
|
if (!plans.length) {
|
|
|
|
|
container.replaceChildren(makeElement('p', 'template-catalog-empty', '当前地区暂无可用实例规格。'));
|
|
|
|
|
byId('vm-region').value = state.selectedRegionId;
|
|
|
|
|
}
|
|
|
|
|
function renderLinePicker() {
|
|
|
|
|
const container = byId('vm-line-picker');
|
|
|
|
|
if (!container) return;
|
|
|
|
|
const cards = saleLines().map(line => {
|
|
|
|
|
const button = makeElement('button', 'line-option');
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
button.dataset.lineId = line.id;
|
|
|
|
|
button.setAttribute('aria-pressed', String(state.selectedLineId === line.id));
|
|
|
|
|
button.setAttribute('aria-label', `选择线路 ${line.name}`);
|
|
|
|
|
button.append(makeElement('strong', '', line.name));
|
|
|
|
|
button.addEventListener('click', () => void selectLine(line.id));
|
|
|
|
|
return button;
|
|
|
|
|
});
|
|
|
|
|
if (!cards.length) cards.push(makeElement('p', 'template-catalog-empty', '当前地区暂无可用线路。'));
|
|
|
|
|
container.replaceChildren(...cards);
|
|
|
|
|
}
|
|
|
|
|
function renderOfferPicker() {
|
|
|
|
|
const container = byId('vm-offer-picker');
|
|
|
|
|
const selectedOfferId = byId('vm-offer').value;
|
|
|
|
|
const offers = saleOffers();
|
|
|
|
|
if (!offers.length) {
|
|
|
|
|
container.replaceChildren(makeElement('p', 'template-catalog-empty', '当前选择暂无可用实例规格。'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const header = makeElement('div', 'plan-table-head');
|
|
|
|
|
['名称', 'CPU', '内存', '硬盘', '每月流量', '价格'].forEach(label => header.append(makeElement('span', '', label)));
|
|
|
|
|
const rows = plans.map(plan => {
|
|
|
|
|
const button = makeElement('button', 'plan-option');
|
|
|
|
|
const memory = formatMemoryGiB(plan.memory_mib);
|
|
|
|
|
const traffic = formatTrafficTiBFromGiB(plan.traffic_gib);
|
|
|
|
|
const price = formatUsd(plan.price);
|
|
|
|
|
const header = makeElement('div', 'offer-table-head');
|
|
|
|
|
['名称', 'CPU', '内存', '硬盘', '每期流量', '价格'].forEach(label => header.append(makeElement('span', '', label)));
|
|
|
|
|
const rows = offers.map(offer => {
|
|
|
|
|
const button = makeElement('button', 'offer-option');
|
|
|
|
|
const memory = formatMemoryGiB(offer.memory_mib);
|
|
|
|
|
const traffic = formatTrafficTiB(offer.traffic_tib);
|
|
|
|
|
const price = formatUsd(offer.price);
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
button.dataset.planId = String(plan.id);
|
|
|
|
|
button.setAttribute('aria-pressed', String(selectedPlanId === String(plan.id)));
|
|
|
|
|
button.setAttribute('aria-label', `选择实例规格 ${plan.name},${plan.cpu_cores} vCPU,内存 ${memory},硬盘 ${plan.disk_gib} GB,每月流量 ${traffic},价格 ${price}`);
|
|
|
|
|
button.dataset.offerId = String(offer.id);
|
|
|
|
|
button.setAttribute('aria-pressed', String(selectedOfferId === String(offer.id)));
|
|
|
|
|
button.setAttribute('aria-label', `选择实例规格 ${offer.name},${offer.cpu_cores} vCPU,内存 ${memory},硬盘 ${offer.disk_gib} GB,每期流量 ${traffic},价格 ${price}`);
|
|
|
|
|
button.append(
|
|
|
|
|
makeElement('strong', 'plan-option-name', plan.name),
|
|
|
|
|
makeElement('span', 'plan-option-value', `${plan.cpu_cores} vCPU`),
|
|
|
|
|
makeElement('span', 'plan-option-value', memory),
|
|
|
|
|
makeElement('span', 'plan-option-value', `${plan.disk_gib} GB`),
|
|
|
|
|
makeElement('span', 'plan-option-value', traffic),
|
|
|
|
|
makeElement('strong', 'plan-option-price', price),
|
|
|
|
|
makeElement('strong', 'offer-option-name', offer.name),
|
|
|
|
|
makeElement('span', 'offer-option-value', `${offer.cpu_cores} vCPU`),
|
|
|
|
|
makeElement('span', 'offer-option-value', memory),
|
|
|
|
|
makeElement('span', 'offer-option-value', `${offer.disk_gib} GB`),
|
|
|
|
|
makeElement('span', 'offer-option-value', traffic),
|
|
|
|
|
makeElement('strong', 'offer-option-price', price),
|
|
|
|
|
);
|
|
|
|
|
button.addEventListener('click', () => void selectPlan(String(plan.id)));
|
|
|
|
|
button.addEventListener('click', () => void selectOffer(String(offer.id)));
|
|
|
|
|
return button;
|
|
|
|
|
});
|
|
|
|
|
container.replaceChildren(header, ...rows);
|
|
|
|
|
}
|
|
|
|
|
async function selectRegion(hostId) {
|
|
|
|
|
async function selectRegion(regionId) {
|
|
|
|
|
if (state.mutationBusy) return;
|
|
|
|
|
const normalized = String(hostId || '');
|
|
|
|
|
if (!saleRegions().some(region => region.hostId === normalized)) return;
|
|
|
|
|
if (state.selectedRegionHostId === normalized && byId('vm-plan').value) return;
|
|
|
|
|
state.selectedRegionHostId = normalized;
|
|
|
|
|
byId('vm-plan').value = '';
|
|
|
|
|
const normalized = String(regionId || '');
|
|
|
|
|
if (!saleRegions().some(region => region.id === normalized)) return;
|
|
|
|
|
if (state.selectedRegionId === normalized) return;
|
|
|
|
|
state.selectedRegionId = normalized;
|
|
|
|
|
state.selectedLineId = defaultSaleLine(saleLines(normalized))?.id || '';
|
|
|
|
|
byId('vm-region').value = state.selectedRegionId;
|
|
|
|
|
byId('vm-line').value = state.selectedLineId;
|
|
|
|
|
byId('vm-offer').value = '';
|
|
|
|
|
renderRegionPicker();
|
|
|
|
|
renderPlanPicker();
|
|
|
|
|
setDeploymentStepEnabled('vm-plan-step', true);
|
|
|
|
|
renderLinePicker();
|
|
|
|
|
renderOfferPicker();
|
|
|
|
|
setDeploymentStepEnabled('vm-line-step', state.linesEnabled);
|
|
|
|
|
setDeploymentStepEnabled('vm-offer-step', Boolean(state.selectedLineId));
|
|
|
|
|
showError('create-error', '');
|
|
|
|
|
updateCreateAvailability();
|
|
|
|
|
}
|
|
|
|
|
async function selectLine(lineId) {
|
|
|
|
|
if (state.mutationBusy) return;
|
|
|
|
|
const line = saleLines().find(item => item.id === String(lineId));
|
|
|
|
|
if (!line || state.selectedLineId === line.id) return;
|
|
|
|
|
state.selectedLineId = line.id;
|
|
|
|
|
byId('vm-line').value = line.id;
|
|
|
|
|
byId('vm-offer').value = '';
|
|
|
|
|
byId('vm-line-picker')?.querySelectorAll('.line-option').forEach(button => {
|
|
|
|
|
button.setAttribute('aria-pressed', String(button.dataset.lineId === line.id));
|
|
|
|
|
});
|
|
|
|
|
renderOfferPicker();
|
|
|
|
|
setDeploymentStepEnabled('vm-offer-step', true);
|
|
|
|
|
showError('create-error', '');
|
|
|
|
|
updateCreateAvailability();
|
|
|
|
|
}
|
|
|
|
|
async function selectPlan(planId) {
|
|
|
|
|
async function selectOffer(offerId) {
|
|
|
|
|
if (state.mutationBusy) return;
|
|
|
|
|
const plan = state.plans.find(item => String(item.id) === String(planId) && String(item.host_id) === state.selectedRegionHostId);
|
|
|
|
|
if (!plan) return;
|
|
|
|
|
byId('vm-plan').value = String(plan.id);
|
|
|
|
|
byId('vm-plan-picker').querySelectorAll('.plan-option').forEach(button => {
|
|
|
|
|
button.setAttribute('aria-pressed', String(button.dataset.planId === String(plan.id)));
|
|
|
|
|
const offer = saleOffers().find(item => String(item.id) === String(offerId));
|
|
|
|
|
if (!offer) return;
|
|
|
|
|
byId('vm-offer').value = String(offer.id);
|
|
|
|
|
byId('vm-offer-picker').querySelectorAll('.offer-option').forEach(button => {
|
|
|
|
|
button.setAttribute('aria-pressed', String(button.dataset.offerId === String(offer.id)));
|
|
|
|
|
});
|
|
|
|
|
showError('create-error', '');
|
|
|
|
|
updateCreateAvailability();
|
|
|
|
|
}
|
|
|
|
|
async function openCreate(bootstrap = null) {
|
|
|
|
|
state.view = 'create'; state.selectedVmId = null; showError('create-error', '');
|
|
|
|
|
byId('vm-plan').value = '';
|
|
|
|
|
if (bootstrap && Array.isArray(bootstrap.plans) && Array.isArray(bootstrap.templates)) {
|
|
|
|
|
state.regions = bootstrapItems(bootstrap, 'regions');
|
|
|
|
|
state.plans = bootstrapItems(bootstrap, 'plans');
|
|
|
|
|
if (!state.plans.length) state.plans = state.regions.flatMap(region => Array.isArray(region?.plans) ? region.plans : []);
|
|
|
|
|
byId('vm-offer').value = '';
|
|
|
|
|
if (bootstrap && Array.isArray(bootstrap.offers) && Array.isArray(bootstrap.regions)
|
|
|
|
|
&& Array.isArray(bootstrap.lines) && Array.isArray(bootstrap.templates)) {
|
|
|
|
|
applySaleCatalog(bootstrap);
|
|
|
|
|
state.templates = bootstrapItems(bootstrap, 'templates', 'cloud_templates');
|
|
|
|
|
state.templateCatalogUnavailable = bootstrap.catalog_unavailable === true;
|
|
|
|
|
state.sshKeys = bootstrapItems(bootstrap, 'ssh_keys', 'keys');
|
|
|
|
|
state.resources = { templates: state.templates };
|
|
|
|
|
state.selectedKeys.create = '';
|
|
|
|
|
const regions = saleRegions();
|
|
|
|
|
const requestedDefault = bootstrap.default_host_id ?? bootstrap.default_region?.host_id ?? bootstrap.default_region;
|
|
|
|
|
const requestedHostId = String(requestedDefault ?? '');
|
|
|
|
|
state.selectedRegionHostId = regions.some(region => region.hostId === requestedHostId)
|
|
|
|
|
? requestedHostId : (regions[0]?.hostId || '');
|
|
|
|
|
byId('vm-region-host').value = state.selectedRegionHostId;
|
|
|
|
|
renderRegionPicker();
|
|
|
|
|
renderPlanPicker();
|
|
|
|
|
renderLinePicker();
|
|
|
|
|
renderOfferPicker();
|
|
|
|
|
renderTemplateCatalog('vm-template');
|
|
|
|
|
renderKeyCards('create');
|
|
|
|
|
byId('vm-template').disabled = !state.templates.some(item => item?.id);
|
|
|
|
|
byId('vm-name').disabled = false;
|
|
|
|
|
byId('vm-root-password').disabled = false;
|
|
|
|
|
setDeploymentStepEnabled('vm-plan-step', Boolean(state.selectedRegionHostId));
|
|
|
|
|
setDeploymentStepEnabled('vm-line-step', state.linesEnabled && Boolean(state.selectedRegionId));
|
|
|
|
|
setDeploymentStepEnabled('vm-offer-step', Boolean(state.selectedLineId));
|
|
|
|
|
setDeploymentStepEnabled('vm-source-step', true);
|
|
|
|
|
setDeploymentStepEnabled('vm-config-step', true);
|
|
|
|
|
updateCreateAvailability();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
state.regions = [];
|
|
|
|
|
state.selectedRegionHostId = ''; byId('vm-region-host').value = '';
|
|
|
|
|
setDeploymentStepEnabled('vm-plan-step', false); resetInstallChoices();
|
|
|
|
|
state.regions = []; state.lines = []; state.offers = []; state.linesEnabled = false;
|
|
|
|
|
state.selectedRegionId = ''; state.selectedLineId = '';
|
|
|
|
|
byId('vm-region').value = ''; byId('vm-line').value = '';
|
|
|
|
|
setDeploymentStepEnabled('vm-line-step', false);
|
|
|
|
|
setDeploymentStepEnabled('vm-offer-step', false);
|
|
|
|
|
resetInstallChoices();
|
|
|
|
|
const results = await Promise.allSettled([
|
|
|
|
|
api('/api/v1/plans').then(plans => { state.plans = items(plans); renderRegionPicker(); }),
|
|
|
|
|
api('/api/v1/catalog').then(catalog => {
|
|
|
|
|
applySaleCatalog(catalog);
|
|
|
|
|
renderRegionPicker();
|
|
|
|
|
renderLinePicker();
|
|
|
|
|
renderOfferPicker();
|
|
|
|
|
setDeploymentStepEnabled('vm-line-step', state.linesEnabled && Boolean(state.selectedRegionId));
|
|
|
|
|
setDeploymentStepEnabled('vm-offer-step', Boolean(state.selectedLineId));
|
|
|
|
|
updateCreateAvailability();
|
|
|
|
|
}),
|
|
|
|
|
loadKeys(),
|
|
|
|
|
loadGlobalTemplates(),
|
|
|
|
|
]);
|
|
|
|
|
@ -646,16 +721,16 @@ async function buy(event) {
|
|
|
|
|
const password = byId('vm-root-password').value;
|
|
|
|
|
if (!strongPassword(password)) return showError('create-error', '密码需为 12 至 128 位英文可打印字符且不能包含空格,并包含大写、小写、数字和符号。');
|
|
|
|
|
if (!byId('vm-template').value) return showError('create-error', '请选择系统镜像版本。');
|
|
|
|
|
if (!byId('vm-plan').value) return showError('create-error', '请先选择可用套餐。');
|
|
|
|
|
const selectedPlan = state.plans.find(plan => String(plan.id) === byId('vm-plan').value);
|
|
|
|
|
if (!selectedPlan || String(selectedPlan.host_id) !== state.selectedRegionHostId) return showError('create-error', '地区与实例规格不匹配,请重新选择。');
|
|
|
|
|
const purchaseSummary = selectedPlan ? `${formatUsd(selectedPlan.price)},${planBillingLabel(selectedPlan)}` : '所选套餐';
|
|
|
|
|
if (!byId('vm-offer').value) return showError('create-error', '请先选择可用实例规格。');
|
|
|
|
|
const selectedOffer = saleOffers().find(offer => String(offer.id) === byId('vm-offer').value);
|
|
|
|
|
if (!selectedOffer) return showError('create-error', '地区、线路与实例规格不匹配,请重新选择。');
|
|
|
|
|
const purchaseSummary = `${formatUsd(selectedOffer.price)},${offerBillingLabel(selectedOffer)}`;
|
|
|
|
|
if (!confirm(`确认创建此实例?将先生成 ${purchaseSummary} 的开通账单,支付完成后才会开始创建。`)) return;
|
|
|
|
|
state.mutationBusy = true;
|
|
|
|
|
const formBody = byId('create-form')?.querySelector('.workspace-form-body');
|
|
|
|
|
if (formBody) { formBody.inert = true; formBody.setAttribute('aria-busy', 'true'); }
|
|
|
|
|
byId('create-submit').disabled = true; showError('create-error', '');
|
|
|
|
|
const body = { plan_id: Number(byId('vm-plan').value), name: byId('vm-name').value.trim(), source: 'cloud', template_id: byId('vm-template').value };
|
|
|
|
|
const body = { offer_id: Number(byId('vm-offer').value), name: byId('vm-name').value.trim(), source: 'cloud', template_id: byId('vm-template').value };
|
|
|
|
|
if (state.selectedKeys.create) body.guest_ssh_key_id = state.selectedKeys.create;
|
|
|
|
|
if (password) body.root_password = password;
|
|
|
|
|
try {
|
|
|
|
|
|