```
feat(scheduler): 更新定时任务配置以支持中标与招标分别设置阈值 将原先单一的 threshold 配置项拆分为 winningThreshold 和 bidThreshold, 分别用于控制中标公示和招标公告的金额筛选条件。同时调整了默认值及描述信息, 使配置更清晰灵活。 此外,更新了定时任务状态展示逻辑,支持显示两个独立的阈值及其单位转换(万元/亿元)。 当阈值为 0 时显示“不筛选”,提高用户理解度。 配置文件 config.json 中相关字段已同步修改,并调整了时间范围字段 timeRange 的默认值。 ```
This commit is contained in:
@@ -21,7 +21,7 @@ export async function sendReportEmail(emailConfig, report) {
|
||||
const info = await transporter.sendMail({
|
||||
from: `"公告采集系统" <${emailConfig.smtpUser}>`,
|
||||
to: emailConfig.recipients,
|
||||
subject: `采购公告分析报告 - ${new Date().toLocaleDateString('zh-CN')}`,
|
||||
subject: `交通水务中标结果报告 - ${new Date().toLocaleDateString('zh-CN')}`,
|
||||
html: htmlContent,
|
||||
});
|
||||
|
||||
@@ -35,6 +35,523 @@ export async function sendReportEmail(emailConfig, report) {
|
||||
}
|
||||
}
|
||||
|
||||
// 发送招标公告报告邮件
|
||||
export async function sendBidAnnounceReportEmail(emailConfig, report) {
|
||||
try {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: emailConfig.smtpHost,
|
||||
port: emailConfig.smtpPort || 587,
|
||||
secure: emailConfig.smtpPort === 465,
|
||||
auth: {
|
||||
user: emailConfig.smtpUser,
|
||||
pass: emailConfig.smtpPass,
|
||||
},
|
||||
});
|
||||
|
||||
const htmlContent = generateBidAnnounceReportHtml(report);
|
||||
|
||||
const info = await transporter.sendMail({
|
||||
from: `"公告采集系统" <${emailConfig.smtpUser}>`,
|
||||
to: emailConfig.recipients,
|
||||
subject: `交通水务招标公告报告 - ${new Date().toLocaleDateString('zh-CN')}`,
|
||||
html: htmlContent,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
messageId: info.messageId,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('发送招标公告邮件失败:', error);
|
||||
throw new Error(`邮件发送失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成招标公告报告HTML
|
||||
function generateBidAnnounceReportHtml(report) {
|
||||
const { summary, projects } = report;
|
||||
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>交通水务招标公告报告</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
.container {
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
padding: 30px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
h1 {
|
||||
color: #e67e22;
|
||||
border-bottom: 3px solid #e67e22;
|
||||
padding-bottom: 10px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.summary {
|
||||
background: linear-gradient(135deg, #e67e22 0%, #d35400 100%);
|
||||
color: white;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.summary h2 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 15px;
|
||||
font-size: 18px;
|
||||
}
|
||||
.stat-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
|
||||
gap: 15px;
|
||||
}
|
||||
.stat {
|
||||
background: rgba(255,255,255,0.15);
|
||||
padding: 12px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
.stat-label {
|
||||
font-size: 13px;
|
||||
opacity: 0.9;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
.stat-value {
|
||||
font-size: 20px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.project-list {
|
||||
margin-top: 20px;
|
||||
}
|
||||
.project-item {
|
||||
background: #f9f9f9;
|
||||
border-left: 4px solid #e67e22;
|
||||
padding: 15px;
|
||||
margin-bottom: 15px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
.project-item h3 {
|
||||
color: #333;
|
||||
margin: 0 0 10px 0;
|
||||
font-size: 15px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.project-meta {
|
||||
color: #666;
|
||||
font-size: 13px;
|
||||
margin: 5px 0;
|
||||
}
|
||||
.amount {
|
||||
display: inline-block;
|
||||
background: #e67e22;
|
||||
color: white;
|
||||
padding: 4px 12px;
|
||||
border-radius: 4px;
|
||||
font-weight: bold;
|
||||
margin-top: 8px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.project-link {
|
||||
color: #e67e22;
|
||||
text-decoration: none;
|
||||
font-size: 12px;
|
||||
word-break: break-all;
|
||||
}
|
||||
.footer {
|
||||
margin-top: 30px;
|
||||
padding-top: 20px;
|
||||
border-top: 1px solid #e0e0e0;
|
||||
color: #999;
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>南京公共资源交易平台 - 交通水务招标公告报告</h1>
|
||||
|
||||
<div class="summary">
|
||||
<h2>报告摘要</h2>
|
||||
<div class="stat-grid">
|
||||
<div class="stat">
|
||||
<div class="stat-label">总公告数量</div>
|
||||
<div class="stat-value">${summary.total_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">有金额信息</div>
|
||||
<div class="stat-value">${summary.has_amount_count || summary.filtered_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">符合筛选</div>
|
||||
<div class="stat-value">${summary.filtered_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">金额阈值</div>
|
||||
<div class="stat-value">${summary.threshold || '无'}</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">合同估算总额</div>
|
||||
<div class="stat-value">${summary.total_amount}</div>
|
||||
</div>
|
||||
</div>
|
||||
${summary.date_range ? `
|
||||
<div style="margin-top: 15px; padding-top: 15px; border-top: 1px solid rgba(255,255,255,0.2);">
|
||||
<div class="stat-label">时间范围</div>
|
||||
<div style="font-size: 14px; margin-top: 5px;">
|
||||
${summary.date_range.startDate || '不限'} 至 ${summary.date_range.endDate || '不限'}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
|
||||
<h2>招标项目详情</h2>
|
||||
<div class="project-list">
|
||||
${projects.length === 0 ? '<p style="color: #999; text-align: center; padding: 20px;">暂无符合条件的项目</p>' : ''}
|
||||
${projects.map((project, index) => `
|
||||
<div class="project-item">
|
||||
<h3>${index + 1}. ${project.title}</h3>
|
||||
<div class="project-meta">
|
||||
<strong>发布日期:</strong> ${project.date}
|
||||
${project.bidCode ? ` | <strong>标段编码:</strong> ${project.bidCode}` : ''}
|
||||
</div>
|
||||
${project.tenderee ? `
|
||||
<div class="project-meta">
|
||||
<strong>招标人:</strong> ${project.tenderee}
|
||||
${project.duration ? ` | <strong>工期:</strong> ${project.duration}日历天` : ''}
|
||||
</div>
|
||||
` : ''}
|
||||
${project.estimatedAmount ? `
|
||||
<div class="amount">
|
||||
合同估算价: ${project.estimatedAmount.amountWan} 万元
|
||||
</div>
|
||||
` : ''}
|
||||
<div style="margin-top: 10px;">
|
||||
<a href="${project.url}" class="project-link" target="_blank">${project.url}</a>
|
||||
</div>
|
||||
</div>
|
||||
`).join('')}
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<p>报告生成时间: ${new Date(summary.generated_at).toLocaleString('zh-CN')}</p>
|
||||
<p>本报告由公告采集系统自动生成</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
}
|
||||
|
||||
// 发送综合报告邮件(中标+招标)
|
||||
export async function sendCombinedReportEmail(emailConfig, winningReport, bidReport) {
|
||||
try {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: emailConfig.smtpHost,
|
||||
port: emailConfig.smtpPort || 587,
|
||||
secure: emailConfig.smtpPort === 465,
|
||||
auth: {
|
||||
user: emailConfig.smtpUser,
|
||||
pass: emailConfig.smtpPass,
|
||||
},
|
||||
});
|
||||
|
||||
const htmlContent = generateCombinedReportHtml(winningReport, bidReport);
|
||||
|
||||
const info = await transporter.sendMail({
|
||||
from: `"公告采集系统" <${emailConfig.smtpUser}>`,
|
||||
to: emailConfig.recipients,
|
||||
subject: `交通水务综合报告 - ${new Date().toLocaleDateString('zh-CN')}`,
|
||||
html: htmlContent,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
messageId: info.messageId,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('发送综合邮件失败:', error);
|
||||
throw new Error(`邮件发送失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成综合报告HTML(中标+招标)
|
||||
function generateCombinedReportHtml(winningReport, bidReport) {
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>交通水务综合报告</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
.container {
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
padding: 30px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
border-bottom: 3px solid #667eea;
|
||||
padding-bottom: 10px;
|
||||
margin-bottom: 30px;
|
||||
text-align: center;
|
||||
}
|
||||
.section {
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
.section-title {
|
||||
font-size: 20px;
|
||||
font-weight: bold;
|
||||
margin-bottom: 15px;
|
||||
padding: 10px 15px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
.section-title.winning {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
.section-title.bid {
|
||||
background: linear-gradient(135deg, #e67e22 0%, #d35400 100%);
|
||||
color: white;
|
||||
}
|
||||
.summary {
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.summary.winning {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
.summary.bid {
|
||||
background: linear-gradient(135deg, #e67e22 0%, #d35400 100%);
|
||||
color: white;
|
||||
}
|
||||
.stat-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 15px;
|
||||
}
|
||||
.stat {
|
||||
background: rgba(255,255,255,0.15);
|
||||
padding: 10px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
.stat-label {
|
||||
font-size: 12px;
|
||||
opacity: 0.9;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
.stat-value {
|
||||
font-size: 18px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.project-list {
|
||||
margin-top: 15px;
|
||||
}
|
||||
.project-item {
|
||||
background: #f9f9f9;
|
||||
padding: 12px;
|
||||
margin-bottom: 10px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
.project-item.winning {
|
||||
border-left: 4px solid #667eea;
|
||||
}
|
||||
.project-item.bid {
|
||||
border-left: 4px solid #e67e22;
|
||||
}
|
||||
.project-item h3 {
|
||||
color: #333;
|
||||
margin: 0 0 8px 0;
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.project-meta {
|
||||
color: #666;
|
||||
font-size: 12px;
|
||||
margin: 3px 0;
|
||||
}
|
||||
.amount {
|
||||
display: inline-block;
|
||||
color: white;
|
||||
padding: 3px 10px;
|
||||
border-radius: 4px;
|
||||
font-weight: bold;
|
||||
margin-top: 6px;
|
||||
font-size: 13px;
|
||||
}
|
||||
.amount.winning {
|
||||
background: #667eea;
|
||||
}
|
||||
.amount.bid {
|
||||
background: #e67e22;
|
||||
}
|
||||
.project-link {
|
||||
text-decoration: none;
|
||||
font-size: 11px;
|
||||
word-break: break-all;
|
||||
}
|
||||
.project-link.winning {
|
||||
color: #667eea;
|
||||
}
|
||||
.project-link.bid {
|
||||
color: #e67e22;
|
||||
}
|
||||
.footer {
|
||||
margin-top: 30px;
|
||||
padding-top: 20px;
|
||||
border-top: 1px solid #e0e0e0;
|
||||
color: #999;
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
.no-data {
|
||||
color: #999;
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
font-style: italic;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>南京公共资源交易平台 - 交通水务综合报告</h1>
|
||||
|
||||
${bidReport ? `
|
||||
<!-- 招标公告部分 -->
|
||||
<div class="section">
|
||||
<div class="section-title bid">招标公告</div>
|
||||
<div class="summary bid">
|
||||
<div class="stat-grid">
|
||||
<div class="stat">
|
||||
<div class="stat-label">总公告数量</div>
|
||||
<div class="stat-value">${bidReport.summary.total_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">有金额信息</div>
|
||||
<div class="stat-value">${bidReport.summary.has_amount_count || bidReport.summary.filtered_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">符合筛选</div>
|
||||
<div class="stat-value">${bidReport.summary.filtered_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">金额阈值</div>
|
||||
<div class="stat-value">${bidReport.summary.threshold || '无'}</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">合同估算总额</div>
|
||||
<div class="stat-value">${bidReport.summary.total_amount}</div>
|
||||
</div>
|
||||
</div>
|
||||
${bidReport.summary.date_range ? `
|
||||
<div style="margin-top: 10px; padding-top: 10px; border-top: 1px solid rgba(255,255,255,0.2); font-size: 13px;">
|
||||
时间范围: ${bidReport.summary.date_range.startDate || '不限'} 至 ${bidReport.summary.date_range.endDate || '不限'}
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
<div class="project-list">
|
||||
${bidReport.projects.length === 0 ? '<p class="no-data">暂无符合条件的招标项目</p>' : ''}
|
||||
${bidReport.projects.map((project, index) => `
|
||||
<div class="project-item bid">
|
||||
<h3>${index + 1}. ${project.title}</h3>
|
||||
<div class="project-meta"><strong>发布日期:</strong> ${project.date}</div>
|
||||
${project.bidCode ? `<div class="project-meta"><strong>标段编码:</strong> ${project.bidCode}</div>` : ''}
|
||||
${project.tenderee ? `<div class="project-meta"><strong>招标人:</strong> ${project.tenderee}</div>` : ''}
|
||||
${project.duration ? `<div class="project-meta"><strong>工期:</strong> ${project.duration}日历天</div>` : ''}
|
||||
${project.estimatedAmount ? `
|
||||
<div class="amount bid">合同估算价: ${project.estimatedAmount.amountWan} 万元</div>
|
||||
` : ''}
|
||||
<div style="margin-top: 8px;">
|
||||
<a href="${project.url}" class="project-link bid" target="_blank">${project.url}</a>
|
||||
</div>
|
||||
</div>
|
||||
`).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
${winningReport ? `
|
||||
<!-- 中标公示部分 -->
|
||||
<div class="section">
|
||||
<div class="section-title winning">中标结果公示</div>
|
||||
<div class="summary winning">
|
||||
<div class="stat-grid">
|
||||
<div class="stat">
|
||||
<div class="stat-label">总项目数</div>
|
||||
<div class="stat-value">${winningReport.summary.total_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">符合条件</div>
|
||||
<div class="stat-value">${winningReport.summary.filtered_count} 条</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">金额阈值</div>
|
||||
<div class="stat-value">${winningReport.summary.threshold}</div>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<div class="stat-label">总金额</div>
|
||||
<div class="stat-value">${winningReport.summary.total_amount}</div>
|
||||
</div>
|
||||
</div>
|
||||
${winningReport.summary.date_range ? `
|
||||
<div style="margin-top: 10px; padding-top: 10px; border-top: 1px solid rgba(255,255,255,0.2); font-size: 13px;">
|
||||
时间范围: ${winningReport.summary.date_range.startDate || '不限'} 至 ${winningReport.summary.date_range.endDate || '不限'}
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
<div class="project-list">
|
||||
${winningReport.projects.length === 0 ? '<p class="no-data">暂无符合条件的中标项目</p>' : ''}
|
||||
${winningReport.projects.map((project, index) => `
|
||||
<div class="project-item winning">
|
||||
<h3>${index + 1}. ${project.title}</h3>
|
||||
<div class="project-meta"><strong>中标日期:</strong> ${project.date}</div>
|
||||
${project.bidNo ? `<div class="project-meta"><strong>标段编号:</strong> ${project.bidNo}</div>` : ''}
|
||||
${project.winningBid ? `
|
||||
<div class="amount winning">中标金额: ${project.winningBid.amount.toFixed(2)} ${project.winningBid.unit}</div>
|
||||
` : ''}
|
||||
<div style="margin-top: 8px;">
|
||||
<a href="${project.url}" class="project-link winning" target="_blank">${project.url}</a>
|
||||
</div>
|
||||
</div>
|
||||
`).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
<div class="footer">
|
||||
<p>报告生成时间: ${new Date().toLocaleString('zh-CN')}</p>
|
||||
<p>本报告由公告采集系统自动生成</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
}
|
||||
|
||||
// 生成HTML格式的报告
|
||||
function generateReportHtml(report) {
|
||||
const { summary, projects } = report;
|
||||
@@ -45,7 +562,7 @@ function generateReportHtml(report) {
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>采购公告分析报告</title>
|
||||
<title>交通水务中标结果报告</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
|
||||
@@ -147,7 +664,7 @@ function generateReportHtml(report) {
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>南京公共工程建设中心 - 采购公告分析报告</h1>
|
||||
<h1>南京公共资源交易平台 - 交通水务中标结果报告</h1>
|
||||
|
||||
<div class="summary">
|
||||
<h2>报告摘要</h2>
|
||||
@@ -186,15 +703,17 @@ function generateReportHtml(report) {
|
||||
<div class="project-item">
|
||||
<h3>${index + 1}. ${project.title}</h3>
|
||||
<div class="project-meta">
|
||||
<strong>发布日期:</strong> ${project.date}
|
||||
${project.publish_time ? ` | <strong>发布时间:</strong> ${project.publish_time}` : ''}
|
||||
<strong>中标日期:</strong> ${project.date}
|
||||
</div>
|
||||
${project.budget ? `
|
||||
${project.winningBid ? `
|
||||
<div class="budget">
|
||||
中标金额: ${project.winningBid.amount.toFixed(2)} ${project.winningBid.unit}
|
||||
</div>
|
||||
` : (project.budget ? `
|
||||
<div class="budget">
|
||||
预算金额: ${project.budget.amount.toFixed(2)} ${project.budget.unit}
|
||||
${project.budget.originalUnit !== project.budget.unit ? ` (原始: ${project.budget.originalUnit})` : ''}
|
||||
</div>
|
||||
` : ''}
|
||||
` : '')}
|
||||
<div style="margin-top: 10px;">
|
||||
<a href="${project.url}" class="project-link" target="_blank">${project.url}</a>
|
||||
</div>
|
||||
|
||||
308
src/scheduler.js
308
src/scheduler.js
@@ -5,7 +5,7 @@ import { dirname, join } from 'path';
|
||||
import axios from 'axios';
|
||||
import * as cheerio from 'cheerio';
|
||||
import iconv from 'iconv-lite';
|
||||
import { sendReportEmail } from './emailService.js';
|
||||
import { sendCombinedReportEmail } from './emailService.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
@@ -62,8 +62,11 @@ function getDateRangeByType(timeRange) {
|
||||
return { startDate, endDate };
|
||||
}
|
||||
|
||||
// 南京市公共资源交易平台 - 房建市政招标公告
|
||||
const BASE_URL = 'https://njggzy.nanjing.gov.cn/njweb/fjsz/068001/068001002/';
|
||||
// 南京市公共资源交易平台 - 交通水务中标结果公示
|
||||
const BASE_URL = 'https://njggzy.nanjing.gov.cn/njweb/jtsw/069008/';
|
||||
|
||||
// 南京市公共资源交易平台 - 交通水务招标公告
|
||||
const BID_ANNOUNCE_BASE_URL = 'https://njggzy.nanjing.gov.cn/njweb/jtsw/069001/';
|
||||
|
||||
const http = axios.create({
|
||||
responseType: 'arraybuffer',
|
||||
@@ -90,12 +93,12 @@ async function fetchHtml(url) {
|
||||
|
||||
function getPageUrl(pageIndex) {
|
||||
if (pageIndex === 1) {
|
||||
return `${BASE_URL}moreinfo.html`;
|
||||
return `${BASE_URL}moreinfosl3.html`;
|
||||
}
|
||||
return `${BASE_URL}${pageIndex}.html`;
|
||||
}
|
||||
|
||||
// 解析列表页HTML,提取招标信息
|
||||
// 解析列表页HTML,提取中标结果信息
|
||||
function parseList(html) {
|
||||
const $ = cheerio.load(html);
|
||||
const items = [];
|
||||
@@ -108,8 +111,8 @@ function parseList(html) {
|
||||
const bidNo = $(cells[0]).find('p').attr('title') || $(cells[0]).find('p').text().trim();
|
||||
const projectName = $(cells[1]).find('p').attr('title') || $(cells[1]).find('p').text().trim();
|
||||
const bidName = $(cells[2]).find('p').attr('title') || $(cells[2]).find('p').text().trim();
|
||||
const estimatedPrice = $(cells[3]).find('p').text().trim();
|
||||
const publishDate = $(cells[4]).find('p').text().trim();
|
||||
const winningPrice = $(cells[3]).find('p').text().trim(); // 中标价格
|
||||
const winningDate = $(cells[4]).find('p').text().trim(); // 中标日期
|
||||
|
||||
const onclick = $row.attr('onclick') || '';
|
||||
const hrefMatch = onclick.match(/window\.open\(['"]([^'"]+)['"]\)/);
|
||||
@@ -121,20 +124,20 @@ function parseList(html) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(publishDate)) return;
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(winningDate)) return;
|
||||
|
||||
const price = parseFloat(estimatedPrice);
|
||||
const price = parseFloat(winningPrice);
|
||||
if (isNaN(price)) return;
|
||||
|
||||
items.push({
|
||||
bidNo,
|
||||
title: projectName,
|
||||
bidName,
|
||||
budget: {
|
||||
winningBid: { // 中标金额
|
||||
amount: price,
|
||||
unit: '万元'
|
||||
},
|
||||
date: publishDate,
|
||||
date: winningDate,
|
||||
href
|
||||
});
|
||||
}
|
||||
@@ -210,17 +213,177 @@ async function fetchListByDateRange(startDate, endDate, maxPages = 50) {
|
||||
return allItems;
|
||||
}
|
||||
|
||||
// ========== 招标公告采集函数 ==========
|
||||
|
||||
// 获取招标公告分页URL
|
||||
function getBidAnnouncePageUrl(pageIndex) {
|
||||
if (pageIndex === 1) {
|
||||
return `${BID_ANNOUNCE_BASE_URL}moreinfo5dc.html`;
|
||||
}
|
||||
return `${BID_ANNOUNCE_BASE_URL}${pageIndex}.html`;
|
||||
}
|
||||
|
||||
// 解析招标公告列表页HTML
|
||||
function parseBidAnnounceList(html) {
|
||||
const $ = cheerio.load(html);
|
||||
const items = [];
|
||||
|
||||
$('li.ewb-info-item2').each((_, row) => {
|
||||
const $row = $(row);
|
||||
const onclick = $row.attr('onclick') || '';
|
||||
|
||||
const hrefMatch = onclick.match(/window\.open\(['"]([^'"]+)['"]\)/);
|
||||
if (!hrefMatch) return;
|
||||
|
||||
let href = hrefMatch[1];
|
||||
if (href.startsWith('/')) {
|
||||
href = `https://njggzy.nanjing.gov.cn${href}`;
|
||||
}
|
||||
|
||||
const $titleP = $row.find('.ewb-info-num2').first().find('p');
|
||||
const title = $titleP.attr('title') || $titleP.text().trim();
|
||||
|
||||
const $dateP = $row.find('.ewb-info-num2').last().find('p');
|
||||
const dateText = $dateP.text().trim();
|
||||
const dateMatch = dateText.match(/\d{4}-\d{2}-\d{2}/);
|
||||
const date = dateMatch ? dateMatch[0] : '';
|
||||
|
||||
if (title && date) {
|
||||
items.push({
|
||||
title,
|
||||
date,
|
||||
href,
|
||||
estimatedAmount: null
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
// 解析招标公告详情页,获取合同估算价
|
||||
async function fetchBidAnnounceDetail(url) {
|
||||
try {
|
||||
const html = await fetchHtml(url);
|
||||
const $ = cheerio.load(html);
|
||||
const bodyText = $('body').text();
|
||||
|
||||
const amountMatch = bodyText.match(/合同估算价[::]\s*([\d,]+\.?\d*)\s*元/);
|
||||
let estimatedAmount = null;
|
||||
if (amountMatch) {
|
||||
const amountStr = amountMatch[1].replace(/,/g, '');
|
||||
estimatedAmount = parseFloat(amountStr);
|
||||
}
|
||||
|
||||
const bidCodeMatch = bodyText.match(/标段编码[::]\s*([A-Za-z0-9\-]+)/);
|
||||
const bidCode = bidCodeMatch ? bidCodeMatch[1] : null;
|
||||
|
||||
const tendereeMatch = bodyText.match(/招标人[为是][::]?\s*([^\s,,。]+)/);
|
||||
const tenderee = tendereeMatch ? tendereeMatch[1] : null;
|
||||
|
||||
const durationMatch = bodyText.match(/计划工期[::]\s*(\d+)\s*日历天/);
|
||||
const duration = durationMatch ? parseInt(durationMatch[1]) : null;
|
||||
|
||||
return { estimatedAmount, bidCode, tenderee, duration, url };
|
||||
} catch (error) {
|
||||
console.error(`获取招标详情失败 ${url}: ${error.message}`);
|
||||
return { estimatedAmount: null, url };
|
||||
}
|
||||
}
|
||||
|
||||
// 按时间范围采集招标公告
|
||||
async function fetchBidAnnounceByDateRange(startDate, endDate, maxPages = 20) {
|
||||
const allItems = [];
|
||||
let shouldContinue = true;
|
||||
let pageIndex = 1;
|
||||
|
||||
console.log(`开始采集招标公告: ${startDate || '不限'} 至 ${endDate || '不限'}`);
|
||||
|
||||
while (shouldContinue && pageIndex <= maxPages) {
|
||||
const pageUrl = getBidAnnouncePageUrl(pageIndex);
|
||||
console.log(`正在采集招标公告第 ${pageIndex} 页: ${pageUrl}`);
|
||||
|
||||
try {
|
||||
const html = await fetchHtml(pageUrl);
|
||||
const items = parseBidAnnounceList(html);
|
||||
|
||||
if (items.length === 0) {
|
||||
console.log(`第 ${pageIndex} 页没有数据,停止采集`);
|
||||
break;
|
||||
}
|
||||
|
||||
let hasItemsInRange = false;
|
||||
let allItemsBeforeRange = true;
|
||||
|
||||
for (const item of items) {
|
||||
if (isDateInRange(item.date, startDate, endDate)) {
|
||||
allItems.push(item);
|
||||
hasItemsInRange = true;
|
||||
allItemsBeforeRange = false;
|
||||
} else if (startDate && new Date(item.date) < new Date(startDate)) {
|
||||
allItemsBeforeRange = allItemsBeforeRange && true;
|
||||
} else {
|
||||
allItemsBeforeRange = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (allItemsBeforeRange && startDate) {
|
||||
console.log(`第 ${pageIndex} 页所有项目都早于起始日期,停止采集`);
|
||||
shouldContinue = false;
|
||||
}
|
||||
|
||||
console.log(`第 ${pageIndex} 页找到 ${items.length} 条,符合条件 ${hasItemsInRange ? '有' : '无'}`);
|
||||
|
||||
pageIndex++;
|
||||
|
||||
if (shouldContinue && pageIndex <= maxPages) {
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`采集第 ${pageIndex} 页失败: ${err.message}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`总共采集了 ${pageIndex - 1} 页,找到 ${allItems.length} 条符合条件的招标公告`);
|
||||
|
||||
// 获取详情(合同估算价)
|
||||
if (allItems.length > 0) {
|
||||
console.log(`开始获取 ${allItems.length} 条招标公告的详情...`);
|
||||
|
||||
for (let i = 0; i < allItems.length; i++) {
|
||||
const item = allItems[i];
|
||||
console.log(`获取详情 ${i + 1}/${allItems.length}: ${item.title.substring(0, 30)}...`);
|
||||
|
||||
const detail = await fetchBidAnnounceDetail(item.href);
|
||||
item.estimatedAmount = detail.estimatedAmount;
|
||||
item.bidCode = detail.bidCode;
|
||||
item.tenderee = detail.tenderee;
|
||||
item.duration = detail.duration;
|
||||
|
||||
if (i < allItems.length - 1) {
|
||||
await new Promise(resolve => setTimeout(resolve, 300));
|
||||
}
|
||||
}
|
||||
|
||||
console.log('招标公告详情获取完成');
|
||||
}
|
||||
|
||||
return allItems;
|
||||
}
|
||||
|
||||
// 定时任务执行函数
|
||||
async function executeScheduledTask(config) {
|
||||
try {
|
||||
console.log('========================================');
|
||||
console.log('定时任务开始执行');
|
||||
console.log('定时任务开始执行(综合采集)');
|
||||
console.log('执行时间:', new Date().toLocaleString('zh-CN'));
|
||||
console.log('========================================');
|
||||
|
||||
const timeRange = config.scheduler.timeRange || 'thisMonth';
|
||||
const { startDate, endDate } = getDateRangeByType(timeRange);
|
||||
const threshold = config.scheduler.threshold || 10000; // 默认1亿(10000万元)
|
||||
const winningThreshold = config.scheduler.winningThreshold !== undefined ? config.scheduler.winningThreshold : 10000; // 中标阈值,默认1亿(10000万元)
|
||||
const bidThreshold = config.scheduler.bidThreshold !== undefined ? config.scheduler.bidThreshold : 0; // 招标阈值,默认0(不筛选)
|
||||
|
||||
const timeRangeNames = {
|
||||
'today': '今日',
|
||||
@@ -229,65 +392,110 @@ async function executeScheduledTask(config) {
|
||||
};
|
||||
console.log(`采集时间段: ${timeRangeNames[timeRange] || '本月'}`);
|
||||
console.log(`采集时间范围: ${startDate} 至 ${endDate}`);
|
||||
console.log(`金额阈值: ${threshold}万元 (${(threshold / 10000).toFixed(2)}亿元)`);
|
||||
console.log(`中标金额阈值: ${winningThreshold}万元 (${(winningThreshold / 10000).toFixed(2)}亿元)`);
|
||||
console.log(`招标金额阈值: ${bidThreshold}万元 ${bidThreshold === 0 ? '(不筛选)' : `(${(bidThreshold / 10000).toFixed(2)}亿元)`}`);
|
||||
|
||||
// 采集列表(直接包含合同估算价)
|
||||
const items = await fetchListByDateRange(startDate, endDate, 50);
|
||||
// ========== 1. 采集中标公示 ==========
|
||||
console.log('\n========== 采集中标公示 ==========');
|
||||
const winningItems = await fetchListByDateRange(startDate, endDate, 50);
|
||||
|
||||
if (items.length === 0) {
|
||||
console.log('暂无公告数据');
|
||||
return;
|
||||
}
|
||||
|
||||
// 筛选大于阈值的项目
|
||||
const filtered = items.filter((item) => {
|
||||
return item.budget && item.budget.amount > threshold;
|
||||
// 筛选大于阈值的中标项目
|
||||
const winningFiltered = winningItems.filter((item) => {
|
||||
return item.winningBid && item.winningBid.amount > winningThreshold;
|
||||
});
|
||||
|
||||
console.log('========================================');
|
||||
console.log(`筛选结果: 找到 ${filtered.length} 个大于 ${threshold}万元 的项目`);
|
||||
|
||||
if (filtered.length === 0) {
|
||||
console.log('暂无符合条件的大额项目');
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算总金额
|
||||
const total = filtered.reduce(
|
||||
(sum, item) => sum + (item.budget?.amount || 0),
|
||||
const winningTotal = winningFiltered.reduce(
|
||||
(sum, item) => sum + (item.winningBid?.amount || 0),
|
||||
0
|
||||
);
|
||||
|
||||
// 生成报告
|
||||
const report = {
|
||||
console.log(`中标公示: 采集 ${winningItems.length} 条,符合阈值 ${winningFiltered.length} 条`);
|
||||
|
||||
// 生成中标报告
|
||||
const winningReport = {
|
||||
summary: {
|
||||
total_count: items.length,
|
||||
filtered_count: filtered.length,
|
||||
threshold: `${threshold}万元`,
|
||||
total_amount: `${total.toFixed(2)}万元`,
|
||||
total_count: winningItems.length,
|
||||
filtered_count: winningFiltered.length,
|
||||
threshold: `${winningThreshold}万元`,
|
||||
total_amount: `${winningTotal.toFixed(2)}万元`,
|
||||
generated_at: new Date().toISOString(),
|
||||
date_range: { startDate, endDate },
|
||||
},
|
||||
projects: filtered.map((item) => ({
|
||||
projects: winningFiltered.map((item) => ({
|
||||
bidNo: item.bidNo,
|
||||
title: item.title,
|
||||
bidName: item.bidName,
|
||||
date: item.date,
|
||||
budget: item.budget,
|
||||
winningBid: item.winningBid,
|
||||
url: item.href,
|
||||
})),
|
||||
};
|
||||
|
||||
// 发送邮件
|
||||
console.log('========================================');
|
||||
console.log('正在发送邮件报告...');
|
||||
// ========== 2. 采集招标公告 ==========
|
||||
console.log('\n========== 采集招标公告 ==========');
|
||||
const bidItems = await fetchBidAnnounceByDateRange(startDate, endDate, 20);
|
||||
|
||||
// 筛选招标项目(根据阈值筛选,阈值为0时不筛选只要求有金额)
|
||||
const bidFiltered = bidItems.filter(item => {
|
||||
if (!item.estimatedAmount) return false;
|
||||
if (bidThreshold === 0) return true; // 阈值为0时不筛选
|
||||
return item.estimatedAmount / 10000 > bidThreshold; // 估算价是元,阈值是万元,需要转换
|
||||
});
|
||||
|
||||
const bidTotal = bidFiltered.reduce(
|
||||
(sum, item) => sum + (item.estimatedAmount || 0),
|
||||
0
|
||||
);
|
||||
|
||||
console.log(`招标公告: 采集 ${bidItems.length} 条,有金额 ${bidFiltered.length} 条`);
|
||||
|
||||
// 生成招标报告
|
||||
const bidReport = {
|
||||
summary: {
|
||||
total_count: bidItems.length,
|
||||
filtered_count: bidFiltered.length,
|
||||
has_amount_count: bidFiltered.length,
|
||||
threshold: bidThreshold === 0 ? '无' : `${bidThreshold}万元`,
|
||||
total_amount: `${(bidTotal / 10000).toFixed(2)}万元`,
|
||||
total_amount_yuan: bidTotal,
|
||||
generated_at: new Date().toISOString(),
|
||||
date_range: { startDate, endDate },
|
||||
report_type: '招标公告'
|
||||
},
|
||||
projects: bidFiltered.map((item) => ({
|
||||
title: item.title,
|
||||
bidCode: item.bidCode,
|
||||
tenderee: item.tenderee,
|
||||
date: item.date,
|
||||
duration: item.duration,
|
||||
estimatedAmount: item.estimatedAmount ? {
|
||||
amount: item.estimatedAmount,
|
||||
amountWan: (item.estimatedAmount / 10000).toFixed(2),
|
||||
unit: '元'
|
||||
} : null,
|
||||
url: item.href,
|
||||
})),
|
||||
};
|
||||
|
||||
// ========== 3. 检查是否有数据需要发送 ==========
|
||||
if (winningFiltered.length === 0 && bidFiltered.length === 0) {
|
||||
console.log('\n========================================');
|
||||
console.log('暂无符合条件的项目,不发送邮件');
|
||||
console.log('========================================');
|
||||
return;
|
||||
}
|
||||
|
||||
// ========== 4. 发送综合邮件 ==========
|
||||
console.log('\n========================================');
|
||||
console.log('正在发送综合报告邮件...');
|
||||
const emailConfig = config.email;
|
||||
|
||||
const result = await sendReportEmail(emailConfig, report);
|
||||
const result = await sendCombinedReportEmail(emailConfig, winningReport, bidReport);
|
||||
|
||||
console.log('邮件发送成功!');
|
||||
console.log('收件人:', emailConfig.recipients);
|
||||
console.log('MessageId:', result.messageId);
|
||||
console.log(`内容: 中标公示 ${winningFiltered.length} 条,招标公告 ${bidFiltered.length} 条`);
|
||||
console.log('========================================');
|
||||
console.log('定时任务执行完成');
|
||||
console.log('========================================');
|
||||
@@ -328,7 +536,8 @@ export function initScheduler() {
|
||||
console.log('========================================');
|
||||
console.log('定时任务已启动');
|
||||
console.log('执行计划:', cronTime);
|
||||
console.log('金额阈值:', config.scheduler.threshold, '万元');
|
||||
console.log('中标阈值:', config.scheduler.winningThreshold, '万元');
|
||||
console.log('招标阈值:', config.scheduler.bidThreshold, '万元', config.scheduler.bidThreshold === 0 ? '(不筛选)' : '');
|
||||
console.log('收件人:', config.email.recipients);
|
||||
console.log('========================================');
|
||||
|
||||
@@ -380,7 +589,8 @@ export function getSchedulerStatus() {
|
||||
config: config ? {
|
||||
enabled: config.scheduler?.enabled || false,
|
||||
cronTime: config.scheduler?.cronTime || '0 9 * * *',
|
||||
threshold: config.scheduler?.threshold || 10000,
|
||||
winningThreshold: config.scheduler?.winningThreshold !== undefined ? config.scheduler.winningThreshold : 10000,
|
||||
bidThreshold: config.scheduler?.bidThreshold !== undefined ? config.scheduler.bidThreshold : 0,
|
||||
timeRange: config.scheduler?.timeRange || 'thisMonth',
|
||||
} : null,
|
||||
};
|
||||
|
||||
407
src/server.js
407
src/server.js
@@ -4,8 +4,9 @@ import cors from 'cors';
|
||||
import axios from 'axios';
|
||||
import * as cheerio from 'cheerio';
|
||||
import iconv from 'iconv-lite';
|
||||
import { sendReportEmail } from './emailService.js';
|
||||
import { sendReportEmail, sendBidAnnounceReportEmail, sendCombinedReportEmail } from './emailService.js';
|
||||
import { initScheduler, runTaskNow, reloadScheduler, getSchedulerStatus } from './scheduler.js';
|
||||
import { log } from 'console';
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 5000;
|
||||
@@ -14,17 +15,29 @@ app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(express.static('public'));
|
||||
|
||||
// 南京市公共资源交易平台 - 房建市政招标公告
|
||||
const BASE_URL = 'https://njggzy.nanjing.gov.cn/njweb/fjsz/068001/068001002/';
|
||||
// 南京市公共资源交易平台 - 交通水务中标结果公示
|
||||
const BASE_URL = 'https://njggzy.nanjing.gov.cn/njweb/jtsw/069008/';
|
||||
|
||||
// 获取分页URL
|
||||
// 南京市公共资源交易平台 - 交通水务招标公告
|
||||
const BID_ANNOUNCE_BASE_URL = 'https://njggzy.nanjing.gov.cn/njweb/jtsw/069001/';
|
||||
|
||||
// 获取分页URL (实际数据在moreinfosl3.html中,分页为2.html, 3.html...)
|
||||
function getPageUrl(pageIndex) {
|
||||
if (pageIndex === 1) {
|
||||
return `${BASE_URL}moreinfo.html`;
|
||||
return `${BASE_URL}moreinfosl3.html`;
|
||||
}
|
||||
return `${BASE_URL}${pageIndex}.html`;
|
||||
}
|
||||
|
||||
// 获取招标公告分页URL
|
||||
// 数据通过AJAX加载,第1页是 moreinfo5dc.html,第2页起是 2.html, 3.html...
|
||||
function getBidAnnouncePageUrl(pageIndex) {
|
||||
if (pageIndex === 1) {
|
||||
return `${BID_ANNOUNCE_BASE_URL}moreinfo5dc.html`;
|
||||
}
|
||||
return `${BID_ANNOUNCE_BASE_URL}${pageIndex}.html`;
|
||||
}
|
||||
|
||||
// 检查日期是否在范围内
|
||||
function isDateInRange(dateStr, startDate, endDate) {
|
||||
if (!dateStr) return false;
|
||||
@@ -117,12 +130,12 @@ async function fetchHtml(url) {
|
||||
return html;
|
||||
}
|
||||
|
||||
// 解析列表页HTML,提取招标信息
|
||||
// 解析列表页HTML,提取中标结果信息
|
||||
function parseList(html) {
|
||||
const $ = cheerio.load(html);
|
||||
const items = [];
|
||||
|
||||
// 解析南京公共资源交易平台的列表结构
|
||||
// 解析南京公共资源交易平台的列表结构(交通水务中标结果公示)
|
||||
// <li class="ewb-info-item2 clearfix" onclick="window.open('详情URL');">
|
||||
$('li.ewb-info-item2').each((_, row) => {
|
||||
const $row = $(row);
|
||||
@@ -133,8 +146,8 @@ function parseList(html) {
|
||||
const bidNo = $(cells[0]).find('p').attr('title') || $(cells[0]).find('p').text().trim();
|
||||
const projectName = $(cells[1]).find('p').attr('title') || $(cells[1]).find('p').text().trim();
|
||||
const bidName = $(cells[2]).find('p').attr('title') || $(cells[2]).find('p').text().trim();
|
||||
const estimatedPrice = $(cells[3]).find('p').text().trim();
|
||||
const publishDate = $(cells[4]).find('p').text().trim();
|
||||
const winningPrice = $(cells[3]).find('p').text().trim(); // 中标价格
|
||||
const winningDate = $(cells[4]).find('p').text().trim(); // 中标日期
|
||||
|
||||
// 从onclick提取详情链接
|
||||
const onclick = $row.attr('onclick') || '';
|
||||
@@ -149,21 +162,21 @@ function parseList(html) {
|
||||
}
|
||||
|
||||
// 验证日期格式 (YYYY-MM-DD)
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(publishDate)) return;
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(winningDate)) return;
|
||||
|
||||
// 解析合同估算价
|
||||
const price = parseFloat(estimatedPrice);
|
||||
// 解析中标价格
|
||||
const price = parseFloat(winningPrice);
|
||||
if (isNaN(price)) return;
|
||||
|
||||
items.push({
|
||||
bidNo, // 标段编号
|
||||
bidNo, // 标段编号
|
||||
title: projectName, // 项目名称
|
||||
bidName, // 标段名称
|
||||
budget: {
|
||||
bidName, // 标段名称
|
||||
winningBid: { // 中标金额
|
||||
amount: price,
|
||||
unit: '万元'
|
||||
},
|
||||
date: publishDate,
|
||||
date: winningDate, // 中标日期
|
||||
href
|
||||
});
|
||||
}
|
||||
@@ -172,6 +185,175 @@ function parseList(html) {
|
||||
return items;
|
||||
}
|
||||
|
||||
// 解析招标公告列表页HTML
|
||||
function parseBidAnnounceList(html) {
|
||||
const $ = cheerio.load(html);
|
||||
const items = [];
|
||||
|
||||
// 解析南京公共资源交易平台的招标公告列表结构
|
||||
// <li class="ewb-info-item2 clearfix" onclick="window.open('...');">
|
||||
$('li.ewb-info-item2').each((_, row) => {
|
||||
const $row = $(row);
|
||||
const onclick = $row.attr('onclick') || '';
|
||||
|
||||
// 提取详情链接
|
||||
const hrefMatch = onclick.match(/window\.open\(['"]([^'"]+)['"]\)/);
|
||||
if (!hrefMatch) return;
|
||||
|
||||
let href = hrefMatch[1];
|
||||
if (href.startsWith('/')) {
|
||||
href = `https://njggzy.nanjing.gov.cn${href}`;
|
||||
}
|
||||
|
||||
// 获取标题(从p标签的title属性或文本)
|
||||
const $titleP = $row.find('.ewb-info-num2').first().find('p');
|
||||
const title = $titleP.attr('title') || $titleP.text().trim();
|
||||
|
||||
// 获取日期
|
||||
const $dateP = $row.find('.ewb-info-num2').last().find('p');
|
||||
const dateText = $dateP.text().trim();
|
||||
const dateMatch = dateText.match(/\d{4}-\d{2}-\d{2}/);
|
||||
const date = dateMatch ? dateMatch[0] : '';
|
||||
|
||||
if (title && date) {
|
||||
items.push({
|
||||
title,
|
||||
date,
|
||||
href,
|
||||
estimatedAmount: null
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
// 解析招标公告详情页,获取合同估算价
|
||||
async function fetchBidAnnounceDetail(url) {
|
||||
try {
|
||||
const html = await fetchHtml(url);
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// 获取页面全部文本
|
||||
const bodyText = $('body').text();
|
||||
|
||||
// 查找合同估算价 (格式如: 合同估算价:4,300,000.00 元)
|
||||
const amountMatch = bodyText.match(/合同估算价[::]\s*([\d,]+\.?\d*)\s*元/);
|
||||
|
||||
let estimatedAmount = null;
|
||||
if (amountMatch) {
|
||||
// 去掉逗号,转换为数字(单位:元)
|
||||
const amountStr = amountMatch[1].replace(/,/g, '');
|
||||
estimatedAmount = parseFloat(amountStr);
|
||||
}
|
||||
|
||||
// 获取标段编码
|
||||
const bidCodeMatch = bodyText.match(/标段编码[::]\s*([A-Za-z0-9\-]+)/);
|
||||
const bidCode = bidCodeMatch ? bidCodeMatch[1] : null;
|
||||
|
||||
// 获取招标人
|
||||
const tendereeMatch = bodyText.match(/招标人[为是][::]?\s*([^\s,,。]+)/);
|
||||
const tenderee = tendereeMatch ? tendereeMatch[1] : null;
|
||||
|
||||
// 获取计划工期
|
||||
const durationMatch = bodyText.match(/计划工期[::]\s*(\d+)\s*日历天/);
|
||||
const duration = durationMatch ? parseInt(durationMatch[1]) : null;
|
||||
|
||||
return {
|
||||
estimatedAmount,
|
||||
bidCode,
|
||||
tenderee,
|
||||
duration,
|
||||
url
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`获取招标详情失败 ${url}: ${error.message}`);
|
||||
return { estimatedAmount: null, url };
|
||||
}
|
||||
}
|
||||
|
||||
// 按时间范围采集招标公告
|
||||
async function fetchBidAnnounceByDateRange(startDate, endDate, maxPages = 20, fetchDetails = true) {
|
||||
const allItems = [];
|
||||
let shouldContinue = true;
|
||||
let pageIndex = 1;
|
||||
|
||||
console.log(`开始采集招标公告: ${startDate || '不限'} 至 ${endDate || '不限'}`);
|
||||
|
||||
while (shouldContinue && pageIndex <= maxPages) {
|
||||
const pageUrl = getBidAnnouncePageUrl(pageIndex);
|
||||
console.log(`正在采集招标公告第 ${pageIndex} 页: ${pageUrl}`);
|
||||
|
||||
try {
|
||||
const html = await fetchHtml(pageUrl);
|
||||
const items = parseBidAnnounceList(html);
|
||||
|
||||
if (items.length === 0) {
|
||||
console.log(`第 ${pageIndex} 页没有数据,停止采集`);
|
||||
break;
|
||||
}
|
||||
|
||||
let hasItemsInRange = false;
|
||||
let allItemsBeforeRange = true;
|
||||
|
||||
for (const item of items) {
|
||||
if (isDateInRange(item.date, startDate, endDate)) {
|
||||
allItems.push(item);
|
||||
hasItemsInRange = true;
|
||||
allItemsBeforeRange = false;
|
||||
} else if (startDate && new Date(item.date) < new Date(startDate)) {
|
||||
allItemsBeforeRange = allItemsBeforeRange && true;
|
||||
} else {
|
||||
allItemsBeforeRange = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (allItemsBeforeRange && startDate) {
|
||||
console.log(`第 ${pageIndex} 页所有项目都早于起始日期,停止采集`);
|
||||
shouldContinue = false;
|
||||
}
|
||||
|
||||
console.log(`第 ${pageIndex} 页找到 ${items.length} 条,符合条件 ${hasItemsInRange ? '有' : '无'}`);
|
||||
|
||||
pageIndex++;
|
||||
|
||||
if (shouldContinue && pageIndex <= maxPages) {
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`采集第 ${pageIndex} 页失败: ${err.message}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`总共采集了 ${pageIndex - 1} 页,找到 ${allItems.length} 条符合条件的招标公告`);
|
||||
|
||||
// 如果需要获取详情(合同估算价)
|
||||
if (fetchDetails && allItems.length > 0) {
|
||||
console.log(`开始获取 ${allItems.length} 条招标公告的详情...`);
|
||||
|
||||
for (let i = 0; i < allItems.length; i++) {
|
||||
const item = allItems[i];
|
||||
console.log(`获取详情 ${i + 1}/${allItems.length}: ${item.title.substring(0, 30)}...`);
|
||||
|
||||
const detail = await fetchBidAnnounceDetail(item.href);
|
||||
item.estimatedAmount = detail.estimatedAmount;
|
||||
item.bidCode = detail.bidCode;
|
||||
item.tenderee = detail.tenderee;
|
||||
item.duration = detail.duration;
|
||||
|
||||
// 添加延迟避免请求过快
|
||||
if (i < allItems.length - 1) {
|
||||
await new Promise(resolve => setTimeout(resolve, 300));
|
||||
}
|
||||
}
|
||||
|
||||
console.log('详情获取完成');
|
||||
}
|
||||
|
||||
return allItems;
|
||||
}
|
||||
|
||||
// API 路由
|
||||
|
||||
// 获取列表
|
||||
@@ -180,6 +362,7 @@ app.get('/api/list', async (req, res) => {
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const pageUrl = getPageUrl(page);
|
||||
|
||||
|
||||
const html = await fetchHtml(pageUrl);
|
||||
const items = parseList(html);
|
||||
res.json({ success: true, data: items, page });
|
||||
@@ -238,11 +421,11 @@ app.post('/api/report', async (req, res) => {
|
||||
|
||||
// 按阈值筛选
|
||||
const filtered = results.filter((item) => {
|
||||
return item.budget && item.budget.amount > threshold;
|
||||
return item.winningBid && item.winningBid.amount > threshold;
|
||||
});
|
||||
|
||||
const total = filtered.reduce(
|
||||
(sum, item) => sum + (item.budget?.amount || 0),
|
||||
(sum, item) => sum + (item.winningBid?.amount || 0),
|
||||
0
|
||||
);
|
||||
|
||||
@@ -259,7 +442,7 @@ app.post('/api/report', async (req, res) => {
|
||||
title: item.title,
|
||||
bidName: item.bidName,
|
||||
date: item.date,
|
||||
budget: item.budget,
|
||||
winningBid: item.winningBid,
|
||||
url: item.href,
|
||||
})),
|
||||
};
|
||||
@@ -297,11 +480,11 @@ app.post('/api/report-daterange', async (req, res) => {
|
||||
|
||||
// 按阈值筛选
|
||||
const filtered = items.filter((item) => {
|
||||
return item.budget && item.budget.amount > threshold;
|
||||
return item.winningBid && item.winningBid.amount > threshold;
|
||||
});
|
||||
|
||||
const total = filtered.reduce(
|
||||
(sum, item) => sum + (item.budget?.amount || 0),
|
||||
(sum, item) => sum + (item.winningBid?.amount || 0),
|
||||
0
|
||||
);
|
||||
|
||||
@@ -319,7 +502,7 @@ app.post('/api/report-daterange', async (req, res) => {
|
||||
title: item.title,
|
||||
bidName: item.bidName,
|
||||
date: item.date,
|
||||
budget: item.budget,
|
||||
winningBid: item.winningBid,
|
||||
url: item.href,
|
||||
})),
|
||||
};
|
||||
@@ -330,6 +513,186 @@ app.post('/api/report-daterange', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// ========== 招标公告相关API ==========
|
||||
|
||||
// 获取招标公告列表(简单列表,按页码)
|
||||
app.get('/api/bid-announce/list', async (req, res) => {
|
||||
try {
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const pageUrl = getBidAnnouncePageUrl(page);
|
||||
|
||||
const html = await fetchHtml(pageUrl);
|
||||
const items = parseBidAnnounceList(html);
|
||||
res.json({ success: true, data: items, page });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取招标公告列表(按时间范围)
|
||||
app.post('/api/bid-announce/list', async (req, res) => {
|
||||
try {
|
||||
const { startDate, endDate, maxPages = 20, fetchDetails = false } = req.body;
|
||||
const items = await fetchBidAnnounceByDateRange(startDate, endDate, maxPages, fetchDetails);
|
||||
res.json({ success: true, data: items });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 生成招标公告报告(含金额统计)
|
||||
app.post('/api/bid-announce/report', async (req, res) => {
|
||||
try {
|
||||
const { startDate, endDate, threshold = 0, maxPages = 20 } = req.body;
|
||||
|
||||
// 采集招标公告(包含详情)
|
||||
const items = await fetchBidAnnounceByDateRange(startDate, endDate, maxPages, true);
|
||||
|
||||
if (items.length === 0) {
|
||||
return res.json({
|
||||
success: true,
|
||||
data: {
|
||||
summary: {
|
||||
total_count: 0,
|
||||
filtered_count: 0,
|
||||
threshold: threshold > 0 ? `${threshold}元` : '无',
|
||||
total_amount: '0.00元',
|
||||
generated_at: new Date().toISOString(),
|
||||
date_range: { startDate, endDate },
|
||||
report_type: '招标公告'
|
||||
},
|
||||
projects: [],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 按阈值筛选(阈值单位为元)
|
||||
const filtered = threshold > 0
|
||||
? items.filter(item => item.estimatedAmount && item.estimatedAmount >= threshold)
|
||||
: items.filter(item => item.estimatedAmount);
|
||||
|
||||
// 计算总金额
|
||||
const total = filtered.reduce((sum, item) => sum + (item.estimatedAmount || 0), 0);
|
||||
|
||||
const report = {
|
||||
summary: {
|
||||
total_count: items.length,
|
||||
filtered_count: filtered.length,
|
||||
has_amount_count: items.filter(i => i.estimatedAmount).length,
|
||||
threshold: threshold > 0 ? `${(threshold / 10000).toFixed(2)}万元` : '无',
|
||||
total_amount: `${(total / 10000).toFixed(2)}万元`,
|
||||
total_amount_yuan: total,
|
||||
generated_at: new Date().toISOString(),
|
||||
date_range: { startDate, endDate },
|
||||
report_type: '招标公告'
|
||||
},
|
||||
projects: filtered.map((item) => ({
|
||||
title: item.title,
|
||||
bidCode: item.bidCode,
|
||||
tenderee: item.tenderee,
|
||||
date: item.date,
|
||||
duration: item.duration,
|
||||
estimatedAmount: item.estimatedAmount ? {
|
||||
amount: item.estimatedAmount,
|
||||
amountWan: (item.estimatedAmount / 10000).toFixed(2),
|
||||
unit: '元'
|
||||
} : null,
|
||||
url: item.href,
|
||||
})),
|
||||
};
|
||||
|
||||
res.json({ success: true, data: report });
|
||||
} catch (error) {
|
||||
res.status(500).json({ success: false, error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 发送招标公告报告邮件
|
||||
app.post('/api/bid-announce/send-email', async (req, res) => {
|
||||
try {
|
||||
const { emailConfig, report } = req.body;
|
||||
|
||||
if (!emailConfig || !emailConfig.smtpHost || !emailConfig.smtpUser || !emailConfig.smtpPass) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '邮件配置不完整,请填写SMTP服务器、用户名和密码',
|
||||
});
|
||||
}
|
||||
|
||||
if (!emailConfig.recipients || emailConfig.recipients.trim() === '') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '请至少指定一个收件人',
|
||||
});
|
||||
}
|
||||
|
||||
if (!report) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '没有可发送的报告数据',
|
||||
});
|
||||
}
|
||||
|
||||
// 使用招标公告专用的邮件发送
|
||||
const result = await sendBidAnnounceReportEmail(emailConfig, report);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '招标公告报告邮件发送成功',
|
||||
messageId: result.messageId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('发送招标公告邮件API错误:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 发送综合报告邮件(中标+招标)
|
||||
app.post('/api/send-combined-email', async (req, res) => {
|
||||
try {
|
||||
const { emailConfig, winningReport, bidReport } = req.body;
|
||||
|
||||
if (!emailConfig || !emailConfig.smtpHost || !emailConfig.smtpUser || !emailConfig.smtpPass) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '邮件配置不完整,请填写SMTP服务器、用户名和密码',
|
||||
});
|
||||
}
|
||||
|
||||
if (!emailConfig.recipients || emailConfig.recipients.trim() === '') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '请至少指定一个收件人',
|
||||
});
|
||||
}
|
||||
|
||||
if (!winningReport && !bidReport) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '没有可发送的报告数据',
|
||||
});
|
||||
}
|
||||
|
||||
// 发送综合邮件
|
||||
const result = await sendCombinedReportEmail(emailConfig, winningReport, bidReport);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '综合报告邮件发送成功',
|
||||
messageId: result.messageId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('发送综合邮件API错误:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 发送报告邮件
|
||||
app.post('/api/send-email', async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user