// utils.js — 全局工具函数与共享状态
const state = {
active: "home",
data: null,
tenant: "科普·无界",
opFilter: "all",
finFilter: "已签约",
selectedProject: null,
taskQuery: "",
taskView: localStorage.getItem("opc-task-view") || "detail",
finView: localStorage.getItem("opc-fin-view") || "rev",
proposalTab: "standard",
chart: null,
chart2: null,
chart3: null,
productPlatform: "all",
uploadTasks: [],
};
const money = (value) => `${Number(value || 0).toLocaleString("zh-CN")} 元`;
const text = (value) => value === undefined || value === null || value === "" ? "—" : esc(value);
function escapeHtml(str) { return String(str || "").replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); }
const html = escapeHtml;
const esc = escapeHtml;
// Toast 通知
function toast(message, type = "info", duration = 3000) {
let container = document.querySelector(".toast-container");
if (!container) {
container = document.createElement("div");
container.className = "toast-container";
document.body.appendChild(container);
}
const el = document.createElement("div");
el.className = `toast toast-${type}`;
const icon = type === "success" ? "check-circle" : type === "error" ? "alert-circle" : "info";
el.innerHTML = `${esc(message)}`;
container.appendChild(el);
if (window.lucide) window.lucide.createIcons();
setTimeout(() => {
el.classList.add("fade-out");
setTimeout(() => el.remove(), 250);
}, duration);
}
window.toast = toast;
function monthOptions(selected = '') {
const now = new Date();
const startYear = now.getFullYear() - 1;
const endYear = now.getFullYear() + 1;
let options = selected ? '' : '';
for (let y = startYear; y <= endYear; y++) {
for (const m of ["01","02","03","04","05","06","07","08","09","10","11","12"]) {
const val = y + "-" + m;
const sel = val === selected ? " selected" : "";
options += ``;
}
}
return options;
}
async function api(path, options = {}) {
const response = await fetch(path, {
headers: options.body instanceof FormData ? undefined : { "Content-Type": "application/json" },
...options,
});
const data = await response.json();
if (!response.ok) throw new Error(data.error || "请求失败");
return data;
}
async function logActivity(targetType, targetId, content) {
try {
await api(`/api/followups/${targetType}/${targetId}`, {
method: "POST",
body: JSON.stringify({ data: { content, tenant: state.tenant } }),
});
} catch (e) { /* non-critical */ }
}
function badge(value) {
const val = String(value || "—");
let cls = "badge-slate";
if (["P0", "有风险", "已丢单", "已延期"].includes(val)) cls = "badge-red";
if (["P1", "方案中", "方案已提交", "商务谈判", "待客户确认"].includes(val)) cls = "badge-amber";
if (["已签约", "已上线", "已完成", "已归档"].includes(val)) cls = "badge-green";
if (["execution", "已签约执行项目"].includes(val)) cls = "badge-blue";
return `${val === "execution" ? "已签约执行项目" : val === "opportunity" ? "业务机会项目" : val}`;
}
function card(content, cls = "") {
return `
| ${h} | `).join("")}
|---|
| ${c} | `).join("")}