You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

176 lines
5.5 KiB

document.addEventListener('DOMContentLoaded', function() {
const links = document.querySelectorAll('.nav-pills .nav-link');
// Get the current page path
const currentPath = window.location.pathname;
links.forEach(link => {
// Compare the link's href with the current path
if (link.href.endsWith(currentPath)) {
link.classList.add('active');
} else {
link.classList.remove('active');
}
});
});
function showModal(message) {
// 设置 Modal 中的消息
document.getElementById('modalMessage').innerText = message;
// 显示 Modal
const responseModal = new bootstrap.Modal(document.getElementById('responseModal'));
responseModal.show();
}
//动态填充select控件
function set_select_data(select_ele_id,datas){
const select_Ele = document.getElementById(select_ele_id);
//清空老数据
select_Ele.innerHTML = '';
//添加列表
datas.forEach(option => {
const optionElement = document.createElement('option');
optionElement.textContent = option;
select_Ele.appendChild(optionElement);
});
}
//设定选项选中状态---下拉框
function set_select_selct(select_ele_id,option_str){
let bfind = false;
const select_Ele = document.getElementById(select_ele_id);
for(let i=0;i< select_Ele.options.length;i++){
if(select_Ele.options[i].value === option_str){
select_Ele.options[i].selected = true;
bfind = true;
break;
}
}
return bfind;
}
//设定单选按钮选中状态 ----单选按钮
function set_radio_selection(groupName, targetValue) {
// 获取所有同名的 radio 按钮
const radios = document.getElementsByName(groupName);
// 遍历 radio 按钮
for (const radio of radios) {
if (radio.value === targetValue) {
radio.checked = true; // 选中匹配项
return true; // 返回成功
}
}
console.error(`未找到值为 ${targetValue} 的 radio 按钮`);
return false; // 未找到匹配项
}
// 获取选中值--返回的value值 ----单选按钮
function get_radio_value(groupName) {
return document.querySelector(`input[name="${groupName}"]:checked`)?.value;
}
//将输入框内容转换为单精度型,若转换失败则返回0 -- 若需要整型,parseInt
function getInputValueAsFloat(id) {
var value = document.getElementById(id).value;
return value ? parseFloat(value) : 0;
}
//正则匹配IP是否合法 return true,false
function isValidIP(ip) {
const ipRegex = /^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/;
return ipRegex.test(ip);
}
//post数据
async function postJSON(url, payload) {
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type':'application/json' },
body: JSON.stringify(payload)
});
if (!res.ok) {
const errorData = await res.json();
throw new Error(errorData.error || `HTTP错误 ${res.status}`);
}
return res.json();
}
/* ---------- 简易 Toast ---------- */
function showToast(msg, type='info') {
const toastEl = document.createElement('div');
toastEl.className = `toast align-items-center text-white bg-${type} border-0 position-fixed bottom-0 end-0 m-3`;
toastEl.role = 'alert';
toastEl.innerHTML = `
<div class="d-flex">
<div class="toast-body">${msg}</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
</div>`;
document.body.appendChild(toastEl);
const t = new bootstrap.Toast(toastEl, { delay: 3000 });
t.show();
toastEl.addEventListener('hidden.bs.toast', () => toastEl.remove());
}
//post提交From数据 -- 返回数据是JSON
function postFrom(url,data){
fetch(url, {
method: 'POST',
body: data,
})
.then(response => response.json())
.then(data => {
const istatus = data.status;
alert(data.msg);
if(istatus == 1 ){
fetchModelData();
$('#updateMM').modal('hide');
}
})
.catch(error => {
console.error('Error:', error);
alert('升级失败,请重试。');
btn.disabled = false;
});
}
//get请求数据
function getDATA(url){
}
//导出csv表格
function downloadCSV(text, filename) {
const blob = new Blob([text], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
URL.revokeObjectURL(link.href);
}
// 工具:格式化单元格内容,遇到“纯数字-数字”形式时自动做公式化处理
function fmtCell(val) {
// 如果是数字-数字,比如 "2-4"、"10-12" 等
if (/^\d+-\d+$/.test(val)) {
return '="' + val + '"';
}
// 如果里面有中文或逗号,就双引号包裹
if (/[,\u4e00-\u9fa5]/.test(val)) {
return `"${val.replace(/"/g, '""')}"`;
}
return val;
}
function formatDateToLocalString(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}