VPSHUB/static/config.js
2026-05-29 23:09:58 +08:00

206 lines
5.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* VPS Hub 配置管理页面交互脚本
*/
/**
* 切换网站URL输入框的提示和默认值
* @param {string} siteType - 网站类型
*/
function toggleSiteUrl(siteType) {
const urlGroup = document.getElementById('site_url_group');
const urlInput = document.getElementById('site_url_input');
if (!urlInput) return;
switch(siteType) {
case 'mofang':
urlInput.placeholder = '留空使用默认值: https://www.heyunidc.cn/v1';
urlInput.required = false;
break;
case 'aliyun':
urlInput.placeholder = '例如: https://ecs.aliyuncs.com';
urlInput.required = true;
break;
case 'tencent':
urlInput.value = 'https://cvm.tencentcloudapi.com/';
urlInput.placeholder = '腾讯云API地址';
urlInput.required = true;
break;
default:
urlInput.placeholder = '输入API地址';
urlInput.required = true;
}
}
/**
* 验证密码格式
* @returns {boolean} 是否通过验证
*/
function validatePassword() {
const passwordInput = document.getElementById('api_pass');
if (!passwordInput) return true;
const password = passwordInput.value;
if (password.length < 8) {
alert('密码至少需要8位');
return false;
}
if (!/^[a-zA-Z0-9]+$/.test(password)) {
alert('密码只能包含字母和数字!');
return false;
}
return true;
}
/**
* 确认删除操作
* @param {string} message - 确认消息
* @returns {boolean} 是否确认
*/
function confirmDelete(message) {
return confirm(message || '确认删除此配置?');
}
/**
* 显示加载状态
* @param {HTMLElement} button - 按钮元素
*/
function showLoading(button) {
if (!button) return;
const originalText = button.textContent;
button.disabled = true;
button.textContent = '加载中...';
button.dataset.originalText = originalText;
}
/**
* 隐藏加载状态
* @param {HTMLElement} button - 按钮元素
*/
function hideLoading(button) {
if (!button) return;
button.disabled = false;
button.textContent = button.dataset.originalText || '提交';
}
/**
* AJAX提交表单
* @param {HTMLFormElement} form - 表单元素
* @param {Function} successCallback - 成功回调
* @param {Function} errorCallback - 失败回调
*/
function submitFormAjax(form, successCallback, errorCallback) {
const formData = new FormData(form);
const submitButton = form.querySelector('button[type="submit"]');
showLoading(submitButton);
fetch(form.action || window.location.href, {
method: form.method || 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
hideLoading(submitButton);
if (data.status === 200 || data.success) {
if (successCallback) successCallback(data);
} else {
if (errorCallback) errorCallback(data);
else alert('操作失败: ' + (data.msg || '未知错误'));
}
})
.catch(error => {
hideLoading(submitButton);
console.error('请求失败:', error);
if (errorCallback) errorCallback(error);
else alert('网络错误,请稍后重试');
});
}
/**
* 刷新VPS列表
*/
function refreshVpsList() {
const button = document.querySelector('.btn-refresh');
if (!button) return;
showLoading(button);
// 创建临时表单提交
const form = document.createElement('form');
form.method = 'POST';
form.action = window.location.href;
const actionInput = document.createElement('input');
actionInput.type = 'hidden';
actionInput.name = 'action';
actionInput.value = 'refresh_vps_list';
form.appendChild(actionInput);
document.body.appendChild(form);
form.submit();
}
/**
* 初始化页面
*/
document.addEventListener('DOMContentLoaded', function() {
// 自动聚焦第一个输入框
const firstInput = document.querySelector('input:not([type="hidden"]):not([type="checkbox"])');
if (firstInput && !firstInput.value) {
firstInput.focus();
}
// 为所有删除按钮添加确认对话框
const deleteButtons = document.querySelectorAll('.btn-delete');
deleteButtons.forEach(button => {
button.addEventListener('click', function(e) {
if (!confirmDelete('确认删除此配置?此操作不可恢复!')) {
e.preventDefault();
}
});
});
// 为所有操作按钮添加确认对话框
const actionButtons = document.querySelectorAll('.btn-success, .btn-danger, .btn-warning');
actionButtons.forEach(button => {
if (button.type === 'submit' && button.name === 'action') {
button.addEventListener('click', function(e) {
let message = '';
switch(this.value) {
case 'on':
message = '确认开机?';
break;
case 'off':
message = '确认关机?';
break;
case 'reboot':
message = '确认硬重启?';
break;
}
if (message && !confirm(message)) {
e.preventDefault();
}
});
}
});
console.log('VPS Hub 页面已加载');
});
// 导出函数供全局使用
window.VPSHub = {
toggleSiteUrl,
validatePassword,
confirmDelete,
refreshVpsList,
showLoading,
hideLoading
};