Compare commits
7 Commits
3aee6af9ae
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fc9748009 | |||
| f797ed9a61 | |||
| caeac76ca3 | |||
| aa572b54cb | |||
| f35d4575c8 | |||
| 00658a3445 | |||
| b4afc1ce5a |
11
.env.example
Normal file
11
.env.example
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# 服务器端口配置
|
||||||
|
PORT=5000
|
||||||
|
|
||||||
|
# 环境说明:
|
||||||
|
# - 开发环境:通常使用 5000
|
||||||
|
# - 生产环境:可以使用 80、8080 等
|
||||||
|
#
|
||||||
|
# 使用方法:
|
||||||
|
# 1. 复制此文件为 .env
|
||||||
|
# 2. 修改端口号
|
||||||
|
# 3. 启动服务时会自动读取
|
||||||
23
.gitea/workflows/deploy.yaml
Normal file
23
.gitea/workflows/deploy.yaml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
name: Deploy Vue App
|
||||||
|
|
||||||
|
# 触发条件:推送到 master 分支时执行
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ['master']
|
||||||
|
|
||||||
|
env:
|
||||||
|
IP: 106.15.181.192
|
||||||
|
REMOTE_DIR: /www/wwwroot/tb.test.haizhiyustc.com/
|
||||||
|
jobs:
|
||||||
|
build-and-deploy:
|
||||||
|
# 运行在已注册的 runner 上(确保 runner 标签匹配,默认无标签可省略)
|
||||||
|
runs-on: [test]
|
||||||
|
steps:
|
||||||
|
# 步骤 4:部署到服务器
|
||||||
|
- name: Deploy to server
|
||||||
|
run: |
|
||||||
|
cd D:\tools\tool-node
|
||||||
|
npm install
|
||||||
|
git pull
|
||||||
|
net stop tool-node
|
||||||
|
net start tool-node
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,6 +12,9 @@ pnpm-debug.log*
|
|||||||
.env.local
|
.env.local
|
||||||
.env.*.local
|
.env.*.local
|
||||||
|
|
||||||
|
# 配置文件(包含敏感信息)
|
||||||
|
config.json
|
||||||
|
|
||||||
# 编辑器目录和文件
|
# 编辑器目录和文件
|
||||||
.vscode/
|
.vscode/
|
||||||
.idea/
|
.idea/
|
||||||
|
|||||||
333
README.md
333
README.md
@@ -1,6 +1,6 @@
|
|||||||
# 南京公共工程建设中心 - 公告采集工具
|
# 南京公共工程建设中心 - 公告采集工具
|
||||||
|
|
||||||
一个用于采集南京公共工程建设中心公告信息的 Web 可视化工具。
|
一个用于采集南京公共工程建设中心公告信息的 Web 可视化工具,支持定时任务和邮件推送。
|
||||||
|
|
||||||
## 功能特性
|
## 功能特性
|
||||||
|
|
||||||
@@ -12,13 +12,36 @@
|
|||||||
- ✅ Web 可视化界面
|
- ✅ Web 可视化界面
|
||||||
- ✅ 导出 Word/Markdown 报告
|
- ✅ 导出 Word/Markdown 报告
|
||||||
- ✅ RESTful API 支持
|
- ✅ RESTful API 支持
|
||||||
|
- ✅ **定时任务自动采集**
|
||||||
|
- ✅ **邮件推送 HTML 报告**
|
||||||
|
- ✅ **Web 配置界面**
|
||||||
|
- ✅ **无需数据库,轻量部署**
|
||||||
|
|
||||||
## 安装
|
## 快速开始
|
||||||
|
|
||||||
|
### 1. 安装依赖
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install
|
npm install
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 2. 配置文件
|
||||||
|
|
||||||
|
首次使用需要创建配置文件:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 复制示例配置文件
|
||||||
|
cp config.example.json config.json
|
||||||
|
|
||||||
|
# 编辑配置文件(或通过 Web 界面配置)
|
||||||
|
# 填写邮件服务器信息和定时任务设置
|
||||||
|
```
|
||||||
|
|
||||||
|
**配置文件说明:**
|
||||||
|
|
||||||
|
- `config.example.json` - 配置模板(不含敏感信息,可提交到 Git)
|
||||||
|
- `config.json` - 实际配置(包含密码等敏感信息,已在 .gitignore 中忽略)
|
||||||
|
|
||||||
## 使用方法
|
## 使用方法
|
||||||
|
|
||||||
### 1. 启动服务器
|
### 1. 启动服务器
|
||||||
@@ -29,7 +52,7 @@ npm start
|
|||||||
|
|
||||||
### 2. 访问界面
|
### 2. 访问界面
|
||||||
|
|
||||||
打开浏览器访问: **http://localhost:3000**
|
打开浏览器访问: **http://localhost:5000** (或您配置的端口)
|
||||||
|
|
||||||
### 3. 功能介绍
|
### 3. 功能介绍
|
||||||
|
|
||||||
@@ -53,6 +76,23 @@ npm start
|
|||||||
- 实时统计项目信息
|
- 实时统计项目信息
|
||||||
- 一键导出 Word/Markdown 报告
|
- 一键导出 Word/Markdown 报告
|
||||||
|
|
||||||
|
**定时任务标签** ⭐ 新增
|
||||||
|
|
||||||
|
- Web 界面配置定时任务
|
||||||
|
- 支持 Cron 表达式自定义执行时间
|
||||||
|
- 可选时间范围(今日/本周/本月)
|
||||||
|
- 设置金额阈值自动筛选
|
||||||
|
- 实时查看任务运行状态
|
||||||
|
- 立即测试运行功能
|
||||||
|
|
||||||
|
**邮件配置标签** ⭐ 新增
|
||||||
|
|
||||||
|
- Web 界面配置 SMTP 邮件服务
|
||||||
|
- 支持主流邮箱(QQ、163、Gmail 等)
|
||||||
|
- 测试连接功能验证配置
|
||||||
|
- 支持多个收件人
|
||||||
|
- 自动发送精美 HTML 报告
|
||||||
|
|
||||||
## 报告示例
|
## 报告示例
|
||||||
|
|
||||||
```markdown
|
```markdown
|
||||||
@@ -143,35 +183,233 @@ Content-Type: application/json
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 6. 获取配置
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/config
|
||||||
|
```
|
||||||
|
|
||||||
|
返回当前配置信息(密码会被隐藏)
|
||||||
|
|
||||||
|
### 7. 更新配置
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /api/config
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"scheduler": {
|
||||||
|
"enabled": true,
|
||||||
|
"cronTime": "0 9 * * *",
|
||||||
|
"threshold": 100000,
|
||||||
|
"timeRange": "today"
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"smtpHost": "smtp.qq.com",
|
||||||
|
"smtpPort": 587,
|
||||||
|
"smtpUser": "your-email@qq.com",
|
||||||
|
"smtpPass": "your-password",
|
||||||
|
"recipients": "recipient@example.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8. 获取定时任务状态
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/scheduler/status
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9. 手动触发定时任务
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /api/run-scheduled-task
|
||||||
|
```
|
||||||
|
|
||||||
|
## 定时任务配置
|
||||||
|
|
||||||
|
### 通过 Web 界面配置(推荐)
|
||||||
|
|
||||||
|
1. 访问 `http://localhost:5000` (或您配置的端口)
|
||||||
|
2. 切换到 **"定时任务"** 标签
|
||||||
|
3. 配置以下选项:
|
||||||
|
- **启用定时任务**:勾选启用
|
||||||
|
- **执行时间**:选择预设时间或自定义 Cron 表达式
|
||||||
|
- **时间范围**:今日/本周/本月
|
||||||
|
- **金额阈值**:设置最低金额(单位:万元)
|
||||||
|
4. 切换到 **"邮件配置"** 标签
|
||||||
|
5. 配置 SMTP 邮件服务器信息
|
||||||
|
6. 点击"测试连接"验证配置
|
||||||
|
7. 点击"保存配置",定时任务自动生效
|
||||||
|
|
||||||
|
### 通过配置文件
|
||||||
|
|
||||||
|
编辑 `config.json`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"scheduler": {
|
||||||
|
"enabled": true, // 是否启用
|
||||||
|
"cronTime": "0 9 * * *", // Cron表达式:每天9点
|
||||||
|
"threshold": 100000, // 金额阈值(万元,10亿)
|
||||||
|
"description": "每天9点采集大于10亿的项目",
|
||||||
|
"timeRange": "today" // 采集范围: today/thisWeek/thisMonth
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"smtpHost": "smtp.qq.com", // SMTP服务器
|
||||||
|
"smtpPort": 587, // 端口
|
||||||
|
"smtpUser": "your@qq.com", // 发件邮箱
|
||||||
|
"smtpPass": "授权码", // QQ邮箱授权码
|
||||||
|
"recipients": "to@qq.com" // 收件人(多个用逗号分隔)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cron 表达式说明
|
||||||
|
|
||||||
|
格式:`分 时 日 月 周`
|
||||||
|
|
||||||
|
常用示例:
|
||||||
|
- `0 9 * * *` - 每天 9:00
|
||||||
|
- `0 9,18 * * *` - 每天 9:00 和 18:00
|
||||||
|
- `0 9 * * 1` - 每周一 9:00
|
||||||
|
- `0 9 1 * *` - 每月1号 9:00
|
||||||
|
- `0 */6 * * *` - 每 6 小时
|
||||||
|
|
||||||
|
### 邮件服务配置参考
|
||||||
|
|
||||||
|
**QQ 邮箱:**
|
||||||
|
- SMTP: `smtp.qq.com`
|
||||||
|
- 端口: `587`
|
||||||
|
- 密码: 使用授权码(在 QQ 邮箱设置中生成)
|
||||||
|
|
||||||
|
**163 邮箱:**
|
||||||
|
- SMTP: `smtp.163.com`
|
||||||
|
- 端口: `465`
|
||||||
|
- 密码: 使用授权码
|
||||||
|
|
||||||
|
**Gmail:**
|
||||||
|
- SMTP: `smtp.gmail.com`
|
||||||
|
- 端口: `587`
|
||||||
|
- 需要开启"不够安全的应用访问"
|
||||||
|
|
||||||
|
## 服务器部署
|
||||||
|
|
||||||
|
### 方法 1:使用 PM2(推荐)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 安装 PM2
|
||||||
|
npm install -g pm2
|
||||||
|
|
||||||
|
# 启动服务
|
||||||
|
pm2 start src/server.js --name gjzx-scraper
|
||||||
|
|
||||||
|
# 查看状态
|
||||||
|
pm2 status
|
||||||
|
|
||||||
|
# 查看日志
|
||||||
|
pm2 logs gjzx-scraper
|
||||||
|
|
||||||
|
# 设置开机自启
|
||||||
|
pm2 startup
|
||||||
|
pm2 save
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方法 2:使用 systemd
|
||||||
|
|
||||||
|
创建服务文件 `/etc/systemd/system/gjzx-scraper.service`:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[Unit]
|
||||||
|
Description=GJZX Scraper Service
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=your-user
|
||||||
|
WorkingDirectory=/path/to/tool-node
|
||||||
|
ExecStart=/usr/bin/node src/server.js
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
```
|
||||||
|
|
||||||
|
启动服务:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo systemctl enable gjzx-scraper
|
||||||
|
sudo systemctl start gjzx-scraper
|
||||||
|
sudo systemctl status gjzx-scraper
|
||||||
|
```
|
||||||
|
|
||||||
|
### 注意事项
|
||||||
|
|
||||||
|
- ✅ **无需数据库**:项目采用无数据库架构,轻量部署
|
||||||
|
- ✅ **配置持久化**:所有配置保存在 `config.json` 文件中
|
||||||
|
- ✅ **进程保活**:使用 PM2 或 systemd 确保进程持续运行
|
||||||
|
- ⚠️ **防火墙**:确保配置的端口可访问(默认 5000)
|
||||||
|
- ⚠️ **配置安全**:不要将 `config.json` 提交到公开仓库
|
||||||
|
|
||||||
## 技术栈
|
## 技术栈
|
||||||
|
|
||||||
- **后端**: Node.js + Express
|
- **后端**: Node.js + Express
|
||||||
- **爬虫**: Axios + Cheerio
|
- **爬虫**: Axios + Cheerio
|
||||||
|
- **定时任务**: node-cron
|
||||||
|
- **邮件服务**: nodemailer
|
||||||
- **前端**: 原生 HTML/CSS/JavaScript
|
- **前端**: 原生 HTML/CSS/JavaScript
|
||||||
- **编码处理**: iconv-lite (支持 GBK/UTF-8)
|
- **编码处理**: iconv-lite (支持 GBK/UTF-8)
|
||||||
- **文档导出**: docx.js
|
- **文档导出**: docx.js
|
||||||
|
- **架构**: 无数据库设计
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
|
|
||||||
```
|
```
|
||||||
.
|
.
|
||||||
├── src/
|
├── src/
|
||||||
│ └── server.js # Web服务器及API
|
│ ├── server.js # Web服务器及API
|
||||||
|
│ ├── scheduler.js # 定时任务调度器
|
||||||
|
│ └── emailService.js # 邮件发送服务
|
||||||
├── public/
|
├── public/
|
||||||
│ ├── index.html # Web界面
|
│ ├── index.html # Web界面
|
||||||
│ └── app.js # 前端逻辑
|
│ └── app.js # 前端逻辑
|
||||||
|
├── config.json # 配置文件(不提交到Git)
|
||||||
|
├── config.example.json # 配置示例
|
||||||
├── package.json
|
├── package.json
|
||||||
└── README.md
|
└── README.md
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 架构特点
|
||||||
|
|
||||||
|
### 无数据库设计
|
||||||
|
|
||||||
|
本项目采用**无数据库架构**,具有以下特点:
|
||||||
|
|
||||||
|
- ✅ **轻量部署**:无需安装和配置数据库
|
||||||
|
- ✅ **实时数据**:每次从源站实时抓取最新数据
|
||||||
|
- ✅ **配置简单**:只需配置 config.json 文件
|
||||||
|
- ✅ **邮件归档**:报告通过邮件发送,邮箱即为历史记录
|
||||||
|
- ✅ **低资源消耗**:内存占用小,适合小型服务器
|
||||||
|
|
||||||
|
### 数据流程
|
||||||
|
|
||||||
|
```
|
||||||
|
定时触发 → 抓取网站数据 → 解析提取 → 筛选过滤 → 生成报告 → 发送邮件
|
||||||
|
↑ ↓
|
||||||
|
└──────────────────── 配置文件 ──────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
## 注意事项
|
## 注意事项
|
||||||
|
|
||||||
1. 采集速度已限制为每条延迟 500ms-1s,避免请求过快
|
1. **采集速度**:已限制为每条延迟 500ms-1s,避免请求过快
|
||||||
2. 仅支持 gjzx.nanjing.gov.cn 域名的详情页解析
|
2. **域名支持**:仅支持 gjzx.nanjing.gov.cn 域名的详情页解析
|
||||||
3. 金额提取基于正则匹配,支持多种格式(预算金额、最高限价等)
|
3. **金额提取**:基于正则匹配,支持多种格式(预算金额、最高限价等)
|
||||||
4. Web 服务器默认端口 3000,可在 server.js 中修改
|
4. **端口配置**:Web 服务器默认端口 5000,支持通过环境变量 PORT 修改
|
||||||
5. 按时间范围采集会在检测到所有公告早于起始日期时自动停止
|
5. **智能停止**:按时间范围采集会在检测到所有公告早于起始日期时自动停止
|
||||||
6. 编码自动识别,支持 GBK 和 UTF-8 网页
|
6. **编码处理**:自动识别,支持 GBK 和 UTF-8 网页
|
||||||
|
7. **配置安全**:config.json 包含敏感信息,已加入 .gitignore,不要提交到公开仓库
|
||||||
|
8. **进程保活**:部署时使用 PM2 或 systemd 确保定时任务持续运行
|
||||||
|
|
||||||
## 核心功能说明
|
## 核心功能说明
|
||||||
|
|
||||||
@@ -204,24 +442,85 @@ Content-Type: application/json
|
|||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
|
||||||
### Q: 为什么采集速度比较慢?
|
### Q: 为什么采集速度比较慢?
|
||||||
|
|
||||||
A: 为了避免对服务器造成过大压力,程序限制了请求频率(每条延迟 500ms-1s)。这是一个负责任的爬虫设计。
|
A: 为了避免对服务器造成过大压力,程序限制了请求频率(每条延迟 500ms-1s)。这是一个负责任的爬虫设计。
|
||||||
|
|
||||||
### Q: 如何采集指定日期范围的公告?
|
### Q: 如何采集指定日期范围的公告?
|
||||||
|
|
||||||
A: 在 Web 界面的"详情采集"和"生成报告"标签中勾选"按时间范围采集",然后输入起始和结束日期即可。
|
A: 在 Web 界面的"详情采集"和"生成报告"标签中勾选"按时间范围采集",然后输入起始和结束日期即可。
|
||||||
|
|
||||||
### Q: 导出的报告在哪里?
|
### Q: 导出的报告在哪里?
|
||||||
|
|
||||||
A: 点击"导出 Word"或"导出 Markdown"按钮后会自动下载到浏览器的默认下载目录。
|
A: 点击"导出 Word"或"导出 Markdown"按钮后会自动下载到浏览器的默认下载目录。
|
||||||
|
|
||||||
### Q: 可以采集其他网站吗?
|
### Q: 可以采集其他网站吗?
|
||||||
|
|
||||||
A: 需要修改 server.js 中的 BASE_URL 和相应的解析函数,因为不同网站的 HTML 结构不同。
|
A: 需要修改 server.js 中的 BASE_URL 和相应的解析函数,因为不同网站的 HTML 结构不同。
|
||||||
|
|
||||||
|
### Q: 需要安装数据库吗?
|
||||||
|
|
||||||
|
A: **不需要!** 本项目采用无数据库架构,只需安装 Node.js 依赖即可运行。所有配置保存在 config.json 文件中,报告通过邮件发送。
|
||||||
|
|
||||||
|
### Q: 定时任务配置后不生效怎么办?
|
||||||
|
|
||||||
|
A: 请检查以下几点:
|
||||||
|
1. `config.json` 中 `scheduler.enabled` 是否为 `true`
|
||||||
|
2. 邮件配置是否正确(SMTP 服务器、用户名、密码)
|
||||||
|
3. Node.js 进程是否持续运行(使用 PM2 查看状态)
|
||||||
|
4. 查看服务器日志是否有错误信息
|
||||||
|
|
||||||
|
### Q: 如何修改定时任务的执行时间?
|
||||||
|
|
||||||
|
A: 有两种方式:
|
||||||
|
1. **Web 界面**(推荐):访问网页,切换到"定时任务"标签,选择预设时间或自定义 Cron 表达式,点击保存
|
||||||
|
2. **配置文件**:编辑 `config.json` 中的 `scheduler.cronTime` 字段
|
||||||
|
|
||||||
|
### Q: 邮件发送失败怎么办?
|
||||||
|
|
||||||
|
A: 常见原因:
|
||||||
|
1. **QQ 邮箱**:需要使用授权码,不是登录密码(在 QQ 邮箱设置 → 账户 → POP3/SMTP 服务中生成)
|
||||||
|
2. **端口问题**:QQ 邮箱使用 587,某些邮箱可能需要 465(SSL)
|
||||||
|
3. **防火墙**:确保服务器可以访问 SMTP 端口
|
||||||
|
4. **测试连接**:在"邮件配置"标签中使用"测试连接"功能验证配置
|
||||||
|
|
||||||
|
### Q: 如何查看定时任务运行日志?
|
||||||
|
|
||||||
|
A: 如果使用 PM2 部署:
|
||||||
|
```bash
|
||||||
|
pm2 logs gjzx-scraper
|
||||||
|
```
|
||||||
|
|
||||||
|
如果使用 systemd:
|
||||||
|
```bash
|
||||||
|
sudo journalctl -u gjzx-scraper -f
|
||||||
|
```
|
||||||
|
|
||||||
|
### Q: 服务器重启后定时任务还会运行吗?
|
||||||
|
|
||||||
|
A: 需要设置开机自启:
|
||||||
|
- **PM2 方式**:执行 `pm2 startup` 和 `pm2 save`
|
||||||
|
- **systemd 方式**:执行 `sudo systemctl enable gjzx-scraper`
|
||||||
|
|
||||||
|
### Q: 可以设置多个定时任务吗?
|
||||||
|
|
||||||
|
A: 当前版本只支持一个定时任务。如需多个任务,可以:
|
||||||
|
1. 使用 Cron 表达式设置多个时间点(如 `0 9,18 * * *` 表示每天 9:00 和 18:00)
|
||||||
|
2. 部署多个实例,使用不同的配置文件和端口
|
||||||
|
|
||||||
## 更新日志
|
## 更新日志
|
||||||
|
|
||||||
|
### v1.1.0 (2025-12-15)
|
||||||
|
|
||||||
|
- ✨ 新增定时任务功能(node-cron)
|
||||||
|
- ✨ 新增邮件推送服务(nodemailer)
|
||||||
|
- ✨ 新增 Web 配置界面(定时任务 + 邮件配置)
|
||||||
|
- ✨ 支持多时间范围(今日/本周/本月)
|
||||||
|
- ✨ 配置 API(读取/更新配置)
|
||||||
|
- ✨ 定时任务状态查询
|
||||||
|
- ✨ 手动触发任务功能
|
||||||
|
- 📝 完善文档,新增部署指南
|
||||||
|
|
||||||
### v1.0.0 (2025-12-12)
|
### v1.0.0 (2025-12-12)
|
||||||
|
|
||||||
- Web 可视化界面
|
- Web 可视化界面
|
||||||
|
|||||||
27
config.example.json
Normal file
27
config.example.json
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"scheduler": {
|
||||||
|
"enabled": true,
|
||||||
|
"cronTime": "0 9 * * *",
|
||||||
|
"threshold": 100000,
|
||||||
|
"description": "每天9点采集大于10亿的项目",
|
||||||
|
"timeRange": "thisMonth",
|
||||||
|
"pushRules": {
|
||||||
|
"urgentThreshold": 500000,
|
||||||
|
"urgentPush": false,
|
||||||
|
"summaryPush": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"smtpHost": "smtp.example.com",
|
||||||
|
"smtpPort": 587,
|
||||||
|
"smtpUser": "your-email@example.com",
|
||||||
|
"smtpPass": "your-password",
|
||||||
|
"recipients": "recipient1@example.com,recipient2@example.com"
|
||||||
|
},
|
||||||
|
"llm": {
|
||||||
|
"enabled": false,
|
||||||
|
"apiKey": "",
|
||||||
|
"baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
|
"model": "qwen-turbo"
|
||||||
|
}
|
||||||
|
}
|
||||||
22
config.json
Normal file
22
config.json
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"scheduler": {
|
||||||
|
"enabled": false,
|
||||||
|
"cronTime": "0 9 * * *",
|
||||||
|
"threshold": 100000,
|
||||||
|
"description": "每天9点采集大于10亿的项目",
|
||||||
|
"timeRange": "today"
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"smtpHost": "smtp.qq.com",
|
||||||
|
"smtpPort": 587,
|
||||||
|
"smtpUser": "1076597680@qq.com",
|
||||||
|
"smtpPass": "nfrjdiraqddsjeeh",
|
||||||
|
"recipients": "5482498@qq.com"
|
||||||
|
},
|
||||||
|
"llm": {
|
||||||
|
"enabled": false,
|
||||||
|
"apiKey": "sk-c9b41f5fd02a495fb5c3f3497076aae8",
|
||||||
|
"baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
|
"model": "qwen-plus"
|
||||||
|
}
|
||||||
|
}
|
||||||
21
node_modules/.package-lock.json
generated
vendored
21
node_modules/.package-lock.json
generated
vendored
@@ -440,6 +440,18 @@
|
|||||||
"url": "https://github.com/fb55/domutils?sponsor=1"
|
"url": "https://github.com/fb55/domutils?sponsor=1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/dotenv": {
|
||||||
|
"version": "17.2.3",
|
||||||
|
"resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-17.2.3.tgz",
|
||||||
|
"integrity": "sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://dotenvx.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/dunder-proto": {
|
"node_modules/dunder-proto": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
"resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
||||||
@@ -1011,6 +1023,15 @@
|
|||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/node-cron": {
|
||||||
|
"version": "4.2.1",
|
||||||
|
"resolved": "https://registry.npmmirror.com/node-cron/-/node-cron-4.2.1.tgz",
|
||||||
|
"integrity": "sha512-lgimEHPE/QDgFlywTd8yTR61ptugX3Qer29efeyWw2rv259HtGBNn1vZVmp8lB9uo9wC0t/AT4iGqXxia+CJFg==",
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/nodemailer": {
|
"node_modules/nodemailer": {
|
||||||
"version": "7.0.11",
|
"version": "7.0.11",
|
||||||
"resolved": "https://registry.npmmirror.com/nodemailer/-/nodemailer-7.0.11.tgz",
|
"resolved": "https://registry.npmmirror.com/nodemailer/-/nodemailer-7.0.11.tgz",
|
||||||
|
|||||||
23
package-lock.json
generated
23
package-lock.json
generated
@@ -12,8 +12,10 @@
|
|||||||
"cheerio": "^1.0.0-rc.12",
|
"cheerio": "^1.0.0-rc.12",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"docx": "^9.5.1",
|
"docx": "^9.5.1",
|
||||||
|
"dotenv": "^17.2.3",
|
||||||
"express": "^5.2.1",
|
"express": "^5.2.1",
|
||||||
"iconv-lite": "^0.6.3",
|
"iconv-lite": "^0.6.3",
|
||||||
|
"node-cron": "^4.2.1",
|
||||||
"nodemailer": "^7.0.11",
|
"nodemailer": "^7.0.11",
|
||||||
"pdf-parse": "^2.4.5"
|
"pdf-parse": "^2.4.5"
|
||||||
}
|
}
|
||||||
@@ -598,6 +600,18 @@
|
|||||||
"url": "https://github.com/fb55/domutils?sponsor=1"
|
"url": "https://github.com/fb55/domutils?sponsor=1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/dotenv": {
|
||||||
|
"version": "17.2.3",
|
||||||
|
"resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-17.2.3.tgz",
|
||||||
|
"integrity": "sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://dotenvx.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/dunder-proto": {
|
"node_modules/dunder-proto": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
"resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
||||||
@@ -1169,6 +1183,15 @@
|
|||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/node-cron": {
|
||||||
|
"version": "4.2.1",
|
||||||
|
"resolved": "https://registry.npmmirror.com/node-cron/-/node-cron-4.2.1.tgz",
|
||||||
|
"integrity": "sha512-lgimEHPE/QDgFlywTd8yTR61ptugX3Qer29efeyWw2rv259HtGBNn1vZVmp8lB9uo9wC0t/AT4iGqXxia+CJFg==",
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/nodemailer": {
|
"node_modules/nodemailer": {
|
||||||
"version": "7.0.11",
|
"version": "7.0.11",
|
||||||
"resolved": "https://registry.npmmirror.com/nodemailer/-/nodemailer-7.0.11.tgz",
|
"resolved": "https://registry.npmmirror.com/nodemailer/-/nodemailer-7.0.11.tgz",
|
||||||
|
|||||||
@@ -12,8 +12,10 @@
|
|||||||
"cheerio": "^1.0.0-rc.12",
|
"cheerio": "^1.0.0-rc.12",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"docx": "^9.5.1",
|
"docx": "^9.5.1",
|
||||||
|
"dotenv": "^17.2.3",
|
||||||
"express": "^5.2.1",
|
"express": "^5.2.1",
|
||||||
"iconv-lite": "^0.6.3",
|
"iconv-lite": "^0.6.3",
|
||||||
|
"node-cron": "^4.2.1",
|
||||||
"nodemailer": "^7.0.11",
|
"nodemailer": "^7.0.11",
|
||||||
"pdf-parse": "^2.4.5"
|
"pdf-parse": "^2.4.5"
|
||||||
}
|
}
|
||||||
|
|||||||
461
public/app.js
461
public/app.js
@@ -1,4 +1,5 @@
|
|||||||
const API_BASE = 'http://localhost:3000/api';
|
// 自动检测当前域名和端口,支持不同环境
|
||||||
|
const API_BASE = `${window.location.origin}/api`;
|
||||||
let currentReport = null;
|
let currentReport = null;
|
||||||
let currentListPage = 1;
|
let currentListPage = 1;
|
||||||
|
|
||||||
@@ -479,11 +480,6 @@ async function exportReport() {
|
|||||||
|
|
||||||
// ========== 邮件功能 ==========
|
// ========== 邮件功能 ==========
|
||||||
|
|
||||||
// 页面加载时加载邮件配置
|
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
|
||||||
loadEmailConfig();
|
|
||||||
});
|
|
||||||
|
|
||||||
// 保存邮件配置到localStorage
|
// 保存邮件配置到localStorage
|
||||||
function saveEmailConfig() {
|
function saveEmailConfig() {
|
||||||
const config = {
|
const config = {
|
||||||
@@ -670,3 +666,456 @@ function showEmailStatus(message, type) {
|
|||||||
}, 3000);
|
}, 3000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========== 定时任务功能 ==========
|
||||||
|
|
||||||
|
// 将Cron表达式转换为友好的时间描述
|
||||||
|
function cronToFriendlyText(cronTime) {
|
||||||
|
// 常见的预设值映射
|
||||||
|
const cronMap = {
|
||||||
|
'0 9 * * *': '每天上午9点',
|
||||||
|
'0 6 * * *': '每天上午6点',
|
||||||
|
'0 12 * * *': '每天中午12点',
|
||||||
|
'0 18 * * *': '每天下午18点',
|
||||||
|
'0 9,18 * * *': '每天9点和18点',
|
||||||
|
'0 */6 * * *': '每6小时',
|
||||||
|
'0 */12 * * *': '每12小时',
|
||||||
|
'0 9 * * 1': '每周一上午9点',
|
||||||
|
'0 9 1 * *': '每月1日上午9点'
|
||||||
|
};
|
||||||
|
|
||||||
|
// 如果是预设值,直接返回
|
||||||
|
if (cronMap[cronTime]) {
|
||||||
|
return cronMap[cronTime];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试解析自定义时间 "分 时 * * *" 格式
|
||||||
|
const cronParts = cronTime.split(/\s+/);
|
||||||
|
if (cronParts.length === 5 && cronParts[2] === '*' && cronParts[3] === '*' && cronParts[4] === '*') {
|
||||||
|
const minute = cronParts[0];
|
||||||
|
const hour = cronParts[1];
|
||||||
|
|
||||||
|
// 检查是否是整点
|
||||||
|
if (minute === '0') {
|
||||||
|
return `每天${hour}点`;
|
||||||
|
} else {
|
||||||
|
return `每天${hour}点${minute}分`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果无法解析,返回原始值
|
||||||
|
return cronTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载定时任务配置
|
||||||
|
async function loadSchedulerConfig() {
|
||||||
|
try {
|
||||||
|
// 从服务器获取配置
|
||||||
|
const response = await fetch(`${API_BASE}/config`);
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success && data.data) {
|
||||||
|
const config = data.data;
|
||||||
|
|
||||||
|
// 填充表单
|
||||||
|
if (config.scheduler) {
|
||||||
|
document.getElementById('schedulerEnabled').checked = config.scheduler.enabled || false;
|
||||||
|
const cronTime = config.scheduler.cronTime || '0 9 * * *';
|
||||||
|
document.getElementById('schedulerCronInput').value = cronTime;
|
||||||
|
document.getElementById('schedulerThresholdInput').value = config.scheduler.threshold || 100000;
|
||||||
|
document.getElementById('schedulerDescription').value = config.scheduler.description || '';
|
||||||
|
|
||||||
|
// 时间段配置
|
||||||
|
document.getElementById('schedulerTimeRange').value = config.scheduler.timeRange || 'thisMonth';
|
||||||
|
|
||||||
|
// 反向映射Cron表达式到预设选择器
|
||||||
|
const presetSelector = document.getElementById('schedulerCronPreset');
|
||||||
|
const customGroup = document.getElementById('customCronGroup');
|
||||||
|
|
||||||
|
// 预设值列表
|
||||||
|
const presets = [
|
||||||
|
'0 9 * * *',
|
||||||
|
'0 6 * * *',
|
||||||
|
'0 12 * * *',
|
||||||
|
'0 18 * * *',
|
||||||
|
'0 9,18 * * *',
|
||||||
|
'0 */6 * * *',
|
||||||
|
'0 */12 * * *',
|
||||||
|
'0 9 * * 1',
|
||||||
|
'0 9 1 * *'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 检查是否匹配预设值
|
||||||
|
if (presets.includes(cronTime)) {
|
||||||
|
presetSelector.value = cronTime;
|
||||||
|
customGroup.style.display = 'none';
|
||||||
|
} else {
|
||||||
|
// 自定义时间 - 尝试解析为 "分 时 * * *" 格式
|
||||||
|
presetSelector.value = 'custom';
|
||||||
|
customGroup.style.display = 'block';
|
||||||
|
|
||||||
|
const cronParts = cronTime.split(/\s+/);
|
||||||
|
if (cronParts.length >= 2) {
|
||||||
|
document.getElementById('customMinute').value = cronParts[0];
|
||||||
|
document.getElementById('customHour').value = cronParts[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新状态显示
|
||||||
|
await updateSchedulerStatus();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('加载定时任务配置失败:', error);
|
||||||
|
showSchedulerStatus('加载配置失败: ' + error.message, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理Cron预设选择器变化
|
||||||
|
function handleCronPresetChange() {
|
||||||
|
const preset = document.getElementById('schedulerCronPreset').value;
|
||||||
|
const customGroup = document.getElementById('customCronGroup');
|
||||||
|
const cronInput = document.getElementById('schedulerCronInput');
|
||||||
|
|
||||||
|
if (preset === 'custom') {
|
||||||
|
// 显示自定义时间选择器
|
||||||
|
customGroup.style.display = 'block';
|
||||||
|
updateCustomCron(); // 根据自定义时间生成Cron表达式
|
||||||
|
} else {
|
||||||
|
// 隐藏自定义时间选择器,使用预设Cron表达式
|
||||||
|
customGroup.style.display = 'none';
|
||||||
|
cronInput.value = preset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据自定义小时和分钟生成Cron表达式
|
||||||
|
function updateCustomCron() {
|
||||||
|
const hour = document.getElementById('customHour').value;
|
||||||
|
const minute = document.getElementById('customMinute').value;
|
||||||
|
const cronInput = document.getElementById('schedulerCronInput');
|
||||||
|
|
||||||
|
// 生成格式: 分 时 * * * (每天指定时间执行)
|
||||||
|
cronInput.value = `${minute} ${hour} * * *`;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
loadEmailConfig();
|
||||||
|
loadSchedulerConfig();
|
||||||
|
loadLLMConfig();
|
||||||
|
|
||||||
|
// 添加自定义时间输入框的事件监听
|
||||||
|
const customHour = document.getElementById('customHour');
|
||||||
|
const customMinute = document.getElementById('customMinute');
|
||||||
|
if (customHour) {
|
||||||
|
customHour.addEventListener('change', updateCustomCron);
|
||||||
|
}
|
||||||
|
if (customMinute) {
|
||||||
|
customMinute.addEventListener('change', updateCustomCron);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 更新定时任务状态显示
|
||||||
|
async function updateSchedulerStatus() {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/scheduler/status`);
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success && data.data) {
|
||||||
|
const status = data.data;
|
||||||
|
|
||||||
|
// 更新运行状态
|
||||||
|
const statusText = status.isRunning ? '✓ 运行中' : '✗ 未运行';
|
||||||
|
const statusColor = status.isRunning ? '#28a745' : '#dc3545';
|
||||||
|
document.getElementById('schedulerRunningStatus').innerHTML = `<span style="color: ${statusColor}">${statusText}</span>`;
|
||||||
|
|
||||||
|
// 更新执行计划
|
||||||
|
if (status.config) {
|
||||||
|
document.getElementById('schedulerCronTime').textContent = cronToFriendlyText(status.config.cronTime);
|
||||||
|
const thresholdBillion = (status.config.threshold / 10000).toFixed(1);
|
||||||
|
document.getElementById('schedulerThreshold').textContent = `${status.config.threshold}万元 (${thresholdBillion}亿)`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取定时任务状态失败:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存定时任务配置
|
||||||
|
async function saveSchedulerConfig() {
|
||||||
|
const schedulerConfig = {
|
||||||
|
enabled: document.getElementById('schedulerEnabled').checked,
|
||||||
|
cronTime: document.getElementById('schedulerCronInput').value,
|
||||||
|
threshold: parseInt(document.getElementById('schedulerThresholdInput').value),
|
||||||
|
description: document.getElementById('schedulerDescription').value,
|
||||||
|
timeRange: document.getElementById('schedulerTimeRange').value
|
||||||
|
};
|
||||||
|
|
||||||
|
// 验证Cron表达式格式(简单验证)
|
||||||
|
const cronParts = schedulerConfig.cronTime.trim().split(/\s+/);
|
||||||
|
if (cronParts.length !== 5) {
|
||||||
|
showSchedulerStatus('Cron表达式格式错误,应为5个部分(分 时 日 月 周)', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从localStorage获取邮件配置
|
||||||
|
const emailConfigStr = localStorage.getItem('emailConfig');
|
||||||
|
let emailConfig = {};
|
||||||
|
|
||||||
|
if (emailConfigStr) {
|
||||||
|
try {
|
||||||
|
emailConfig = JSON.parse(emailConfigStr);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('解析邮件配置失败:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果邮件配置为空,提示用户
|
||||||
|
if (!emailConfig.smtpHost || !emailConfig.smtpUser) {
|
||||||
|
if (confirm('检测到邮件配置未完成,定时任务需要邮件配置才能发送报告。\n\n是否继续保存定时任务配置(不保存邮件配置)?')) {
|
||||||
|
// 继续保存,但不包含邮件配置
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建完整配置对象
|
||||||
|
const fullConfig = {
|
||||||
|
scheduler: schedulerConfig,
|
||||||
|
email: emailConfig
|
||||||
|
};
|
||||||
|
|
||||||
|
showSchedulerStatus('正在保存配置...', 'info');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/config`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(fullConfig)
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success) {
|
||||||
|
showSchedulerStatus('配置已保存,定时任务已重新加载!', 'success');
|
||||||
|
// 刷新状态显示
|
||||||
|
await updateSchedulerStatus();
|
||||||
|
} else {
|
||||||
|
showSchedulerStatus(`保存失败: ${data.error}`, 'error');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
showSchedulerStatus(`请求失败: ${error.message}`, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 立即测试运行定时任务
|
||||||
|
async function testSchedulerNow() {
|
||||||
|
if (!confirm('确定要立即执行定时任务吗?\n\n这将采集本月大于阈值的项目并发送邮件,可能需要几分钟时间。')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
showSchedulerStatus('正在后台执行定时任务,请稍候...', 'info');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/run-scheduled-task`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' }
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success) {
|
||||||
|
showSchedulerStatus('定时任务已在后台开始执行,完成后将发送邮件。请查看服务器控制台日志了解进度。', 'success');
|
||||||
|
} else {
|
||||||
|
showSchedulerStatus(`执行失败: ${data.error}`, 'error');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
showSchedulerStatus(`请求失败: ${error.message}`, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示定时任务配置状态
|
||||||
|
function showSchedulerStatus(message, type) {
|
||||||
|
const statusDiv = document.getElementById('schedulerConfigStatus');
|
||||||
|
const bgColors = {
|
||||||
|
success: '#d4edda',
|
||||||
|
error: '#f8d7da',
|
||||||
|
info: '#d1ecf1'
|
||||||
|
};
|
||||||
|
const textColors = {
|
||||||
|
success: '#155724',
|
||||||
|
error: '#721c24',
|
||||||
|
info: '#0c5460'
|
||||||
|
};
|
||||||
|
|
||||||
|
statusDiv.innerHTML = `
|
||||||
|
<div style="background: ${bgColors[type]}; color: ${textColors[type]}; padding: 15px; border-radius: 8px;">
|
||||||
|
${message}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
// 3秒后自动隐藏成功消息
|
||||||
|
if (type === 'success') {
|
||||||
|
setTimeout(() => {
|
||||||
|
statusDiv.innerHTML = '';
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== LLM 配置功能 ==========
|
||||||
|
|
||||||
|
// 加载 LLM 配置
|
||||||
|
async function loadLLMConfig() {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/config`);
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success && data.data && data.data.llm) {
|
||||||
|
const llmConfig = data.data.llm;
|
||||||
|
|
||||||
|
document.getElementById('llmEnabled').checked = llmConfig.enabled || false;
|
||||||
|
document.getElementById('llmApiKey').value = llmConfig.apiKey || '';
|
||||||
|
document.getElementById('llmBaseUrl').value = llmConfig.baseUrl || 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
||||||
|
document.getElementById('llmModel').value = llmConfig.model || 'qwen-turbo';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新状态显示
|
||||||
|
await updateLLMStatus();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('加载 LLM 配置失败:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新 LLM 状态显示
|
||||||
|
async function updateLLMStatus() {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/llm/status`);
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success && data.data) {
|
||||||
|
const status = data.data;
|
||||||
|
|
||||||
|
// 更新运行状态
|
||||||
|
let statusText, statusColor;
|
||||||
|
if (status.enabled) {
|
||||||
|
statusText = '✓ 已启用';
|
||||||
|
statusColor = '#28a745';
|
||||||
|
} else if (status.configured) {
|
||||||
|
statusText = '○ 已配置但未启用';
|
||||||
|
statusColor = '#ffc107';
|
||||||
|
} else {
|
||||||
|
statusText = '✗ 未配置';
|
||||||
|
statusColor = '#dc3545';
|
||||||
|
}
|
||||||
|
document.getElementById('llmRunningStatus').innerHTML = `<span style="color: ${statusColor}">${statusText}</span>`;
|
||||||
|
|
||||||
|
// 更新模型名称
|
||||||
|
document.getElementById('llmModelName').textContent = status.model || '-';
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取 LLM 状态失败:', error);
|
||||||
|
document.getElementById('llmRunningStatus').innerHTML = '<span style="color: #dc3545">获取状态失败</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存 LLM 配置
|
||||||
|
async function saveLLMConfig() {
|
||||||
|
const llmConfig = {
|
||||||
|
enabled: document.getElementById('llmEnabled').checked,
|
||||||
|
apiKey: document.getElementById('llmApiKey').value,
|
||||||
|
baseUrl: document.getElementById('llmBaseUrl').value || 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||||
|
model: document.getElementById('llmModel').value || 'qwen-turbo'
|
||||||
|
};
|
||||||
|
|
||||||
|
// 验证必填项
|
||||||
|
if (llmConfig.enabled && !llmConfig.apiKey) {
|
||||||
|
showLLMStatus('启用 AI 功能需要填写 API Key', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
showLLMStatus('正在保存配置...', 'info');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 先获取现有配置
|
||||||
|
const configResponse = await fetch(`${API_BASE}/config`);
|
||||||
|
const configData = await configResponse.json();
|
||||||
|
|
||||||
|
if (!configData.success) {
|
||||||
|
showLLMStatus('获取配置失败', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合并配置
|
||||||
|
const fullConfig = {
|
||||||
|
...configData.data,
|
||||||
|
llm: llmConfig
|
||||||
|
};
|
||||||
|
|
||||||
|
// 保存配置
|
||||||
|
const response = await fetch(`${API_BASE}/config`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(fullConfig)
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success) {
|
||||||
|
showLLMStatus('配置已保存!', 'success');
|
||||||
|
await updateLLMStatus();
|
||||||
|
} else {
|
||||||
|
showLLMStatus(`保存失败: ${data.error}`, 'error');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
showLLMStatus(`请求失败: ${error.message}`, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试 LLM 连接
|
||||||
|
async function testLLMConnection() {
|
||||||
|
showLLMStatus('正在测试连接...', 'info');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/llm/test`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' }
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success && data.data.success) {
|
||||||
|
showLLMStatus(`连接成功! 模型: ${data.data.model}, 响应: ${data.data.reply}`, 'success');
|
||||||
|
} else {
|
||||||
|
showLLMStatus(`连接失败: ${data.data?.error || data.error || '未知错误'}`, 'error');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
showLLMStatus(`请求失败: ${error.message}`, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示 LLM 配置状态
|
||||||
|
function showLLMStatus(message, type) {
|
||||||
|
const statusDiv = document.getElementById('llmConfigStatus');
|
||||||
|
const bgColors = {
|
||||||
|
success: '#d4edda',
|
||||||
|
error: '#f8d7da',
|
||||||
|
info: '#d1ecf1'
|
||||||
|
};
|
||||||
|
const textColors = {
|
||||||
|
success: '#155724',
|
||||||
|
error: '#721c24',
|
||||||
|
info: '#0c5460'
|
||||||
|
};
|
||||||
|
|
||||||
|
statusDiv.innerHTML = `
|
||||||
|
<div style="background: ${bgColors[type]}; color: ${textColors[type]}; padding: 15px; border-radius: 8px;">
|
||||||
|
${message}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
// 5秒后自动隐藏成功消息
|
||||||
|
if (type === 'success') {
|
||||||
|
setTimeout(() => {
|
||||||
|
statusDiv.innerHTML = '';
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -342,7 +342,9 @@
|
|||||||
<button class="tab active" onclick="switchTab('list')">公告列表</button>
|
<button class="tab active" onclick="switchTab('list')">公告列表</button>
|
||||||
<button class="tab" onclick="switchTab('detail')">详情采集</button>
|
<button class="tab" onclick="switchTab('detail')">详情采集</button>
|
||||||
<button class="tab" onclick="switchTab('report')">生成报告</button>
|
<button class="tab" onclick="switchTab('report')">生成报告</button>
|
||||||
|
<button class="tab" onclick="switchTab('scheduler')">定时任务</button>
|
||||||
<button class="tab" onclick="switchTab('email')">邮件配置</button>
|
<button class="tab" onclick="switchTab('email')">邮件配置</button>
|
||||||
|
<button class="tab" onclick="switchTab('llm')">AI配置</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@@ -470,6 +472,118 @@
|
|||||||
<div id="reportResults" class="results"></div>
|
<div id="reportResults" class="results"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- 定时任务 -->
|
||||||
|
<div id="scheduler" class="tab-content">
|
||||||
|
<h2 style="margin-bottom: 20px; color: #667eea;">定时任务配置</h2>
|
||||||
|
<p style="color: #666; margin-bottom: 20px;">配置定时任务自动采集本月大于指定金额的项目并发送邮件报告</p>
|
||||||
|
|
||||||
|
<!-- 任务状态 -->
|
||||||
|
<div id="schedulerStatus" style="margin-bottom: 30px; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 8px;">
|
||||||
|
<h3 style="margin-top: 0; margin-bottom: 15px;">任务状态</h3>
|
||||||
|
<div style="display: flex; gap: 30px; flex-wrap: wrap;">
|
||||||
|
<div>
|
||||||
|
<div style="opacity: 0.9; font-size: 14px;">运行状态</div>
|
||||||
|
<div style="font-size: 20px; font-weight: bold; margin-top: 5px;" id="schedulerRunningStatus">加载中...</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div style="opacity: 0.9; font-size: 14px;">执行时间</div>
|
||||||
|
<div style="font-size: 20px; font-weight: bold; margin-top: 5px;" id="schedulerCronTime">-</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div style="opacity: 0.9; font-size: 14px;">金额阈值</div>
|
||||||
|
<div style="font-size: 20px; font-weight: bold; margin-top: 5px;" id="schedulerThreshold">-</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 配置表单 -->
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="checkbox-wrapper" onclick="document.getElementById('schedulerEnabled').click();">
|
||||||
|
<input type="checkbox" id="schedulerEnabled" onclick="event.stopPropagation();">
|
||||||
|
<label for="schedulerEnabled">启用定时任务</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>执行计划</label>
|
||||||
|
<select id="schedulerCronPreset" style="width: 100%; padding: 12px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 14px;" onchange="handleCronPresetChange()">
|
||||||
|
<option value="0 9 * * *">每天上午9点</option>
|
||||||
|
<option value="0 6 * * *">每天上午6点</option>
|
||||||
|
<option value="0 12 * * *">每天中午12点</option>
|
||||||
|
<option value="0 18 * * *">每天下午18点</option>
|
||||||
|
<option value="0 9,18 * * *">每天9点和18点</option>
|
||||||
|
<option value="0 */6 * * *">每6小时</option>
|
||||||
|
<option value="0 */12 * * *">每12小时</option>
|
||||||
|
<option value="0 9 * * 1">每周一上午9点</option>
|
||||||
|
<option value="0 9 1 * *">每月1日上午9点</option>
|
||||||
|
<option value="custom">自定义时间...</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 自定义时间配置 -->
|
||||||
|
<div class="form-group" id="customCronGroup" style="display: none;">
|
||||||
|
<label>自定义执行时间</label>
|
||||||
|
<div style="display: flex; gap: 10px; align-items: center;">
|
||||||
|
<div style="flex: 1;">
|
||||||
|
<label style="font-size: 12px; color: #666;">小时 (0-23)</label>
|
||||||
|
<input type="number" id="customHour" min="0" max="23" value="9" style="width: 100%; padding: 12px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 14px;">
|
||||||
|
</div>
|
||||||
|
<div style="flex: 1;">
|
||||||
|
<label style="font-size: 12px; color: #666;">分钟 (0-59)</label>
|
||||||
|
<input type="number" id="customMinute" min="0" max="59" value="0" style="width: 100%; padding: 12px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 14px;">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<small style="color: #666; display: block; margin-top: 5px;">
|
||||||
|
将在每天指定的时间执行
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 隐藏的Cron表达式字段 -->
|
||||||
|
<input type="hidden" id="schedulerCronInput" value="0 9 * * *">
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>采集时间段</label>
|
||||||
|
<select id="schedulerTimeRange" style="width: 100%; padding: 12px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 14px;">
|
||||||
|
<option value="today">今日</option>
|
||||||
|
<option value="thisWeek">本周</option>
|
||||||
|
<option value="thisMonth" selected>本月</option>
|
||||||
|
</select>
|
||||||
|
<small style="color: #666; display: block; margin-top: 5px;">
|
||||||
|
今日:今天 | 本周:本周一至今 | 本月:本月1日至今
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>金额阈值 (万元)</label>
|
||||||
|
<input type="number" id="schedulerThresholdInput" value="100000" min="0" step="1000">
|
||||||
|
<small style="color: #666; display: block; margin-top: 5px;">
|
||||||
|
10亿 = 100000万元 | 5亿 = 50000万元 | 1亿 = 10000万元
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>任务描述 (可选)</label>
|
||||||
|
<input type="text" id="schedulerDescription" placeholder="例如: 每天9点采集大于10亿的项目">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button class="btn" onclick="saveSchedulerConfig()">保存配置</button>
|
||||||
|
<button class="btn" onclick="testSchedulerNow()" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">立即测试运行</button>
|
||||||
|
<button class="btn" onclick="loadSchedulerConfig()" style="background: #6c757d;">刷新状态</button>
|
||||||
|
|
||||||
|
<div id="schedulerConfigStatus" style="margin-top: 20px;"></div>
|
||||||
|
|
||||||
|
<div style="margin-top: 30px; padding: 20px; background: #fff3cd; border-radius: 8px; border-left: 4px solid #ffc107;">
|
||||||
|
<h3 style="margin-top: 0; color: #856404;">使用说明</h3>
|
||||||
|
<ul style="line-height: 1.8; color: #856404;">
|
||||||
|
<li><strong>定时任务功能:</strong> 自动采集选中时间段的所有公告</li>
|
||||||
|
<li><strong>筛选条件:</strong> 只保留预算金额大于设定阈值的项目</li>
|
||||||
|
<li><strong>邮件发送:</strong> 自动将筛选结果生成HTML报告并发送到配置的邮箱</li>
|
||||||
|
<li><strong>执行时间:</strong> 通过下拉菜单或自定义时间设置定时执行时间</li>
|
||||||
|
<li><strong>注意事项:</strong> 保存配置后会自动重启定时任务,无需重启服务器</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- 邮件配置 -->
|
<!-- 邮件配置 -->
|
||||||
<div id="email" class="tab-content">
|
<div id="email" class="tab-content">
|
||||||
<h2 style="margin-bottom: 20px; color: #667eea;">邮件配置</h2>
|
<h2 style="margin-bottom: 20px; color: #667eea;">邮件配置</h2>
|
||||||
@@ -519,6 +633,103 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- AI配置 -->
|
||||||
|
<div id="llm" class="tab-content">
|
||||||
|
<h2 style="margin-bottom: 20px; color: #667eea;">AI 智能分析配置</h2>
|
||||||
|
<p style="color: #666; margin-bottom: 20px;">使用大语言模型智能提取招标金额,提高金额识别的准确性</p>
|
||||||
|
|
||||||
|
<!-- AI 状态 -->
|
||||||
|
<div id="llmStatus" style="margin-bottom: 30px; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 8px;">
|
||||||
|
<h3 style="margin-top: 0; margin-bottom: 15px;">AI 服务状态</h3>
|
||||||
|
<div style="display: flex; gap: 30px; flex-wrap: wrap;">
|
||||||
|
<div>
|
||||||
|
<div style="opacity: 0.9; font-size: 14px;">服务状态</div>
|
||||||
|
<div style="font-size: 20px; font-weight: bold; margin-top: 5px;" id="llmRunningStatus">加载中...</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div style="opacity: 0.9; font-size: 14px;">当前模型</div>
|
||||||
|
<div style="font-size: 20px; font-weight: bold; margin-top: 5px;" id="llmModelName">-</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 配置表单 -->
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="checkbox-wrapper" onclick="document.getElementById('llmEnabled').click();">
|
||||||
|
<input type="checkbox" id="llmEnabled" onclick="event.stopPropagation();">
|
||||||
|
<label for="llmEnabled">启用 AI 金额提取</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>API Key *</label>
|
||||||
|
<input type="password" id="llmApiKey" placeholder="请输入阿里云 DashScope API Key">
|
||||||
|
<small style="color: #666; display: block; margin-top: 5px;">
|
||||||
|
<a href="https://dashscope.console.aliyun.com/apiKey" target="_blank" style="color: #667eea;">点击这里获取 API Key</a>
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>API 地址</label>
|
||||||
|
<input type="text" id="llmBaseUrl" value="https://dashscope.aliyuncs.com/compatible-mode/v1" placeholder="https://dashscope.aliyuncs.com/compatible-mode/v1">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>模型选择</label>
|
||||||
|
<select id="llmModel" style="width: 100%; padding: 12px; border: 2px solid #e0e0e0; border-radius: 8px; font-size: 14px;">
|
||||||
|
<option value="qwen-turbo">通义千问 Turbo (快速、低成本)</option>
|
||||||
|
<option value="qwen-plus">通义千问 Plus (更准确)</option>
|
||||||
|
<option value="qwen-max">通义千问 Max (最强)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button class="btn" onclick="saveLLMConfig()">保存配置</button>
|
||||||
|
<button class="btn" onclick="testLLMConnection()" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">测试连接</button>
|
||||||
|
<button class="btn" onclick="loadLLMConfig()" style="background: #6c757d;">刷新状态</button>
|
||||||
|
|
||||||
|
<div id="llmConfigStatus" style="margin-top: 20px;"></div>
|
||||||
|
|
||||||
|
<div style="margin-top: 30px; padding: 20px; background: #e8f5e9; border-radius: 8px; border-left: 4px solid #4caf50;">
|
||||||
|
<h3 style="margin-top: 0; color: #2e7d32;">功能说明</h3>
|
||||||
|
<ul style="line-height: 1.8; color: #2e7d32;">
|
||||||
|
<li><strong>智能提取:</strong> 使用大语言模型理解公告内容,准确提取预算金额</li>
|
||||||
|
<li><strong>自动降级:</strong> 当 AI 服务不可用时,自动使用正则表达式提取</li>
|
||||||
|
<li><strong>支持模型:</strong> 阿里云通义千问系列模型(qwen-turbo/plus/max)</li>
|
||||||
|
<li><strong>计费说明:</strong> 按实际调用量计费,qwen-turbo 约 0.0008元/千tokens</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style="margin-top: 20px; padding: 20px; background: #fff3cd; border-radius: 8px; border-left: 4px solid #ffc107;">
|
||||||
|
<h3 style="margin-top: 0; color: #856404;">模型对比</h3>
|
||||||
|
<table style="width: 100%; border-collapse: collapse; margin-top: 10px;">
|
||||||
|
<tr style="border-bottom: 1px solid #ddd;">
|
||||||
|
<th style="text-align: left; padding: 8px; color: #856404;">模型</th>
|
||||||
|
<th style="text-align: left; padding: 8px; color: #856404;">速度</th>
|
||||||
|
<th style="text-align: left; padding: 8px; color: #856404;">准确度</th>
|
||||||
|
<th style="text-align: left; padding: 8px; color: #856404;">成本</th>
|
||||||
|
</tr>
|
||||||
|
<tr style="border-bottom: 1px solid #ddd;">
|
||||||
|
<td style="padding: 8px;">qwen-turbo</td>
|
||||||
|
<td style="padding: 8px;">最快</td>
|
||||||
|
<td style="padding: 8px;">良好</td>
|
||||||
|
<td style="padding: 8px;">最低</td>
|
||||||
|
</tr>
|
||||||
|
<tr style="border-bottom: 1px solid #ddd;">
|
||||||
|
<td style="padding: 8px;">qwen-plus</td>
|
||||||
|
<td style="padding: 8px;">较快</td>
|
||||||
|
<td style="padding: 8px;">优秀</td>
|
||||||
|
<td style="padding: 8px;">中等</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="padding: 8px;">qwen-max</td>
|
||||||
|
<td style="padding: 8px;">较慢</td>
|
||||||
|
<td style="padding: 8px;">最佳</td>
|
||||||
|
<td style="padding: 8px;">较高</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
237
src/llmService.js
Normal file
237
src/llmService.js
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
/**
|
||||||
|
* LLM 服务模块 - 使用阿里云通义千问 API 提取招标金额
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { join, dirname } from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
|
|
||||||
|
// 获取 LLM 配置
|
||||||
|
function getLLMConfig() {
|
||||||
|
try {
|
||||||
|
const configPath = join(__dirname, '..', 'config.json');
|
||||||
|
const configContent = readFileSync(configPath, 'utf-8');
|
||||||
|
const config = JSON.parse(configContent);
|
||||||
|
return config.llm || null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('读取 LLM 配置失败:', err.message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查 LLM 是否已启用
|
||||||
|
export function isLLMEnabled() {
|
||||||
|
const config = getLLMConfig();
|
||||||
|
return config && config.enabled && config.apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 LLM 提取招标金额
|
||||||
|
export async function extractBudgetWithLLM(content) {
|
||||||
|
const config = getLLMConfig();
|
||||||
|
|
||||||
|
if (!config || !config.enabled || !config.apiKey) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 智能提取包含金额信息的段落,避免截断丢失关键信息
|
||||||
|
const maxContentLength = 4000;
|
||||||
|
let truncatedContent = content;
|
||||||
|
|
||||||
|
if (content.length > maxContentLength) {
|
||||||
|
// 查找金额关键词的位置,提取关键词周围的上下文
|
||||||
|
const budgetKeywords = ['预算金额', '项目预算', '采购预算', '控制价', '最高限价', '招标金额', '项目金额', '合同金额', '投标报价', '中标金额', '成交金额', '中标价', '成交价'];
|
||||||
|
const contextRadius = 200; // 关键词前后各取200字符
|
||||||
|
const extractedContexts = [];
|
||||||
|
|
||||||
|
for (const keyword of budgetKeywords) {
|
||||||
|
let pos = content.indexOf(keyword);
|
||||||
|
while (pos !== -1) {
|
||||||
|
const start = Math.max(0, pos - contextRadius);
|
||||||
|
const end = Math.min(content.length, pos + keyword.length + contextRadius);
|
||||||
|
extractedContexts.push(content.substring(start, end));
|
||||||
|
pos = content.indexOf(keyword, pos + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extractedContexts.length > 0) {
|
||||||
|
// 有相关内容,拼接:开头部分 + 提取的上下文
|
||||||
|
const headerContent = content.substring(0, 1500);
|
||||||
|
const relevantContent = [...new Set(extractedContexts)].join('\n---\n'); // 去重
|
||||||
|
truncatedContent = headerContent + '\n\n【以下为金额相关内容】\n' + relevantContent;
|
||||||
|
|
||||||
|
if (truncatedContent.length > maxContentLength) {
|
||||||
|
truncatedContent = truncatedContent.substring(0, maxContentLength) + '...(内容已截断)';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 没找到相关内容,使用原来的截断方式
|
||||||
|
truncatedContent = content.substring(0, maxContentLength) + '...(内容已截断)';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const prompt = `你是一个专业的招标文件分析助手。请从以下招标公告内容中提取预算金额信息。
|
||||||
|
|
||||||
|
要求:
|
||||||
|
1. 优先查找以下字段对应的金额:预算金额、项目预算、采购预算、预算、控制价、最高限价、招标金额、项目金额、合同金额、投标报价、中标金额、成交金额、中标价、成交价
|
||||||
|
2. 如果有多个金额,优先选择"预算金额"或"项目预算"
|
||||||
|
3. 金额统一转换为万元单位(如 70万元 = 70,700000元 = 70)
|
||||||
|
4. 严格按照 JSON 格式返回,不要添加任何其他文字
|
||||||
|
|
||||||
|
常见格式示例:
|
||||||
|
- "预算金额:70万元" → amount: 70
|
||||||
|
- "预算金额:700000元" → amount: 70
|
||||||
|
- "项目预算:70.00万元" → amount: 70
|
||||||
|
|
||||||
|
返回格式(必须是合法的 JSON):
|
||||||
|
{"amount": 数值, "unit": "万元", "text": "原文中的金额描述"}
|
||||||
|
|
||||||
|
如果没有找到金额,返回:
|
||||||
|
{"amount": null, "unit": null, "text": null}
|
||||||
|
|
||||||
|
公告内容:
|
||||||
|
${truncatedContent}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${config.baseUrl}/chat/completions`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${config.apiKey}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: config.model || 'qwen-turbo',
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: prompt,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
temperature: 0.1, // 低温度,保证输出稳定
|
||||||
|
max_tokens: 200,
|
||||||
|
}),
|
||||||
|
signal: AbortSignal.timeout(15000), // 15秒超时
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
console.error('LLM API 错误:', response.status, errorText);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
const assistantMessage = data.choices?.[0]?.message?.content;
|
||||||
|
|
||||||
|
if (!assistantMessage) {
|
||||||
|
console.error('LLM 返回内容为空');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析 JSON 响应
|
||||||
|
const jsonMatch = assistantMessage.match(/\{[\s\S]*\}/);
|
||||||
|
if (!jsonMatch) {
|
||||||
|
console.error('LLM 返回格式异常:', assistantMessage);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = JSON.parse(jsonMatch[0]);
|
||||||
|
|
||||||
|
if (result.amount === null || result.amount === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证金额合理性
|
||||||
|
const amount = parseFloat(result.amount);
|
||||||
|
if (isNaN(amount) || amount < 0.01 || amount > 100000000) {
|
||||||
|
console.error('LLM 提取的金额不合理:', result.amount);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`LLM 提取金额成功: ${amount} 万元`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
amount: amount,
|
||||||
|
unit: '万元',
|
||||||
|
text: result.text || `${amount}万元`,
|
||||||
|
source: 'llm', // 标记来源
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err.name === 'TimeoutError') {
|
||||||
|
console.error('LLM API 超时');
|
||||||
|
} else {
|
||||||
|
console.error('LLM 提取金额失败:', err.message);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试 LLM 连接
|
||||||
|
export async function testLLMConnection() {
|
||||||
|
const config = getLLMConfig();
|
||||||
|
|
||||||
|
if (!config || !config.apiKey) {
|
||||||
|
return { success: false, error: '未配置 API Key' };
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${config.baseUrl}/chat/completions`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${config.apiKey}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: config.model || 'qwen-turbo',
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: '请回复"连接成功"',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
max_tokens: 10,
|
||||||
|
}),
|
||||||
|
signal: AbortSignal.timeout(10000),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
return { success: false, error: `API 错误: ${response.status} - ${errorText}` };
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
const reply = data.choices?.[0]?.message?.content;
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: '连接成功',
|
||||||
|
model: config.model || 'qwen-turbo',
|
||||||
|
reply: reply,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err.name === 'TimeoutError') {
|
||||||
|
return { success: false, error: '连接超时' };
|
||||||
|
}
|
||||||
|
return { success: false, error: err.message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 LLM 状态
|
||||||
|
export function getLLMStatus() {
|
||||||
|
const config = getLLMConfig();
|
||||||
|
|
||||||
|
if (!config) {
|
||||||
|
return {
|
||||||
|
configured: false,
|
||||||
|
enabled: false,
|
||||||
|
model: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
configured: !!config.apiKey,
|
||||||
|
enabled: config.enabled && !!config.apiKey,
|
||||||
|
model: config.model || 'qwen-turbo',
|
||||||
|
baseUrl: config.baseUrl,
|
||||||
|
};
|
||||||
|
}
|
||||||
631
src/scheduler.js
Normal file
631
src/scheduler.js
Normal file
@@ -0,0 +1,631 @@
|
|||||||
|
import cron from 'node-cron';
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
import { dirname, join } from 'path';
|
||||||
|
import axios from 'axios';
|
||||||
|
import * as cheerio from 'cheerio';
|
||||||
|
import iconv from 'iconv-lite';
|
||||||
|
import { sendReportEmail } from './emailService.js';
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
|
|
||||||
|
// 加载配置文件
|
||||||
|
function loadConfig() {
|
||||||
|
try {
|
||||||
|
const configPath = join(__dirname, '..', 'config.json');
|
||||||
|
const configContent = readFileSync(configPath, 'utf-8');
|
||||||
|
return JSON.parse(configContent);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('加载配置文件失败:', error.message);
|
||||||
|
console.error('请确保 config.json 文件存在并配置正确');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据时间范围类型获取开始和结束日期
|
||||||
|
function getDateRangeByType(timeRange) {
|
||||||
|
const now = new Date();
|
||||||
|
const year = now.getFullYear();
|
||||||
|
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||||||
|
const day = String(now.getDate()).padStart(2, '0');
|
||||||
|
|
||||||
|
let startDate, endDate;
|
||||||
|
endDate = `${year}-${month}-${day}`; // 结束日期都是今天
|
||||||
|
|
||||||
|
switch (timeRange) {
|
||||||
|
case 'today':
|
||||||
|
// 今日
|
||||||
|
startDate = `${year}-${month}-${day}`;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'thisWeek': {
|
||||||
|
// 本周 (从周一开始)
|
||||||
|
const dayOfWeek = now.getDay(); // 0是周日,1是周一
|
||||||
|
const diff = dayOfWeek === 0 ? 6 : dayOfWeek - 1; // 计算到周一的天数差
|
||||||
|
const monday = new Date(now);
|
||||||
|
monday.setDate(now.getDate() - diff);
|
||||||
|
const weekYear = monday.getFullYear();
|
||||||
|
const weekMonth = String(monday.getMonth() + 1).padStart(2, '0');
|
||||||
|
const weekDay = String(monday.getDate()).padStart(2, '0');
|
||||||
|
startDate = `${weekYear}-${weekMonth}-${weekDay}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'thisMonth':
|
||||||
|
default:
|
||||||
|
// 本月
|
||||||
|
startDate = `${year}-${month}-01`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { startDate, endDate };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取本月的开始和结束日期 (兼容旧代码)
|
||||||
|
function getCurrentMonthDateRange() {
|
||||||
|
return getDateRangeByType('thisMonth');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从server.js复制的辅助函数
|
||||||
|
const BASE_URL = 'https://gjzx.nanjing.gov.cn/gggs/';
|
||||||
|
|
||||||
|
const http = axios.create({
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
timeout: 10000,
|
||||||
|
headers: {
|
||||||
|
'User-Agent': 'Mozilla/5.0 (compatible; gjzx-scraper/1.0)',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
function pickEncoding(contentType = '') {
|
||||||
|
const match = /charset=([^;]+)/i.exec(contentType);
|
||||||
|
if (!match) return 'utf-8';
|
||||||
|
const charset = match[1].trim().toLowerCase();
|
||||||
|
if (charset.includes('gb')) return 'gbk';
|
||||||
|
return charset;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchHtml(url) {
|
||||||
|
const res = await http.get(url);
|
||||||
|
const encoding = pickEncoding(res.headers['content-type']);
|
||||||
|
const html = iconv.decode(res.data, encoding || 'utf-8');
|
||||||
|
return html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPageUrl(pageIndex, baseUrl = BASE_URL) {
|
||||||
|
if (pageIndex === 0) {
|
||||||
|
return baseUrl;
|
||||||
|
}
|
||||||
|
const cleanBaseUrl = baseUrl.replace(/\/$/, '');
|
||||||
|
return `${cleanBaseUrl}/index_${pageIndex}.html`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseList(html) {
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
const items = [];
|
||||||
|
|
||||||
|
$('table tr').each((_, row) => {
|
||||||
|
const $row = $(row);
|
||||||
|
const link = $row.find('td:first-child a').first();
|
||||||
|
const dateCell = $row.find('td:nth-child(2)');
|
||||||
|
|
||||||
|
if (link.length && dateCell.length) {
|
||||||
|
const title = link.attr('title') || link.text().trim();
|
||||||
|
const rawHref = link.attr('href') || '';
|
||||||
|
const dateText = dateCell.text().trim();
|
||||||
|
|
||||||
|
if (!rawHref || !title || title.length < 5) return;
|
||||||
|
if (rawHref === './' || rawHref === '../') return;
|
||||||
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(dateText)) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const href = new URL(rawHref, BASE_URL).toString();
|
||||||
|
items.push({ title, href, date: dateText });
|
||||||
|
} catch (err) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDateInRange(dateStr, startDate, endDate) {
|
||||||
|
if (!dateStr) return false;
|
||||||
|
const date = new Date(dateStr);
|
||||||
|
if (isNaN(date.getTime())) return false;
|
||||||
|
|
||||||
|
if (startDate && date < new Date(startDate)) return false;
|
||||||
|
if (endDate && date > new Date(endDate)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchListByDateRange(startDate, endDate, maxPages = 23) {
|
||||||
|
const allItems = [];
|
||||||
|
let shouldContinue = true;
|
||||||
|
let pageIndex = 0;
|
||||||
|
|
||||||
|
console.log(`开始按时间范围采集: ${startDate || '不限'} 至 ${endDate || '不限'}`);
|
||||||
|
|
||||||
|
while (shouldContinue && pageIndex < maxPages) {
|
||||||
|
const pageUrl = getPageUrl(pageIndex);
|
||||||
|
console.log(`正在采集第 ${pageIndex + 1} 页: ${pageUrl}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const html = await fetchHtml(pageUrl);
|
||||||
|
const items = parseList(html);
|
||||||
|
|
||||||
|
if (items.length === 0) {
|
||||||
|
console.log(`第 ${pageIndex + 1} 页没有数据,停止采集`);
|
||||||
|
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 + 1} 页所有项目都早于起始日期,停止采集`);
|
||||||
|
shouldContinue = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`第 ${pageIndex + 1} 页找到 ${items.length} 条,符合条件 ${hasItemsInRange ? '有' : '无'}`);
|
||||||
|
|
||||||
|
pageIndex++;
|
||||||
|
|
||||||
|
if (shouldContinue && pageIndex < maxPages) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`采集第 ${pageIndex + 1} 页失败: ${err.message}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`总共采集了 ${pageIndex} 页,找到 ${allItems.length} 条符合条件的公告`);
|
||||||
|
return allItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从server.js导入parseDetail相关函数
|
||||||
|
function parseDetail(html) {
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
|
let title = $('.title18').text().trim();
|
||||||
|
if (!title) {
|
||||||
|
title = $('.article-info h1').text().trim();
|
||||||
|
}
|
||||||
|
if (!title) {
|
||||||
|
title = $('h1').first().text().trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
const publishTd = $('td:contains("发布部门")').filter((_, el) => {
|
||||||
|
return $(el).text().includes('发布时间');
|
||||||
|
});
|
||||||
|
const publishText = publishTd.text().trim();
|
||||||
|
let timeMatch = publishText.match(/(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})/);
|
||||||
|
let publishTime = timeMatch ? timeMatch[1] : '';
|
||||||
|
|
||||||
|
if (!publishTime) {
|
||||||
|
const infoText = $('.info-sources').text() || $('body').text();
|
||||||
|
timeMatch = infoText.match(/(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2})/);
|
||||||
|
publishTime = timeMatch ? timeMatch[1] : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
let content = '';
|
||||||
|
const contentSelectors = [
|
||||||
|
'.zhenwen td',
|
||||||
|
'.con',
|
||||||
|
'.article-content',
|
||||||
|
'.ewb-article-content',
|
||||||
|
'body'
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const selector of contentSelectors) {
|
||||||
|
const el = $(selector).first();
|
||||||
|
if (el.length > 0) {
|
||||||
|
const text = el.text().trim();
|
||||||
|
if (text.length > content.length) {
|
||||||
|
content = text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const budget = extractBudget(content);
|
||||||
|
|
||||||
|
return {
|
||||||
|
title,
|
||||||
|
publishTime,
|
||||||
|
content,
|
||||||
|
budget,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractBudget(content) {
|
||||||
|
let cleanedContent = content.replace(/(\d)\s*[\n\r]\s*(?=\d)/g, '$1');
|
||||||
|
|
||||||
|
const patterns = [
|
||||||
|
{ regex: /(?:¥|¥|人民币)\s*([\d,,]+(?:\.\d+)?)\s*万元/i, priority: 1 },
|
||||||
|
{ regex: /[((][¥¥]([\d,,]+(?:\.\d+)?)[))]/i, priority: 2, divider: 10000 },
|
||||||
|
{ regex: /([\d,,]+(?:\.\d+)?)\s*万元/i, priority: 3 },
|
||||||
|
{ regex: /(?:¥|¥|人民币)\s*([\d,,]+(?:\.\d+)?)\s*元/i, priority: 4, divider: 10000 },
|
||||||
|
{ regex: /([\d,,]+(?:\.\d+)?)\s*元(?!整)/i, priority: 5, divider: 10000 }
|
||||||
|
];
|
||||||
|
|
||||||
|
let bestMatch = null;
|
||||||
|
let bestPriority = Infinity;
|
||||||
|
|
||||||
|
for (const pattern of patterns) {
|
||||||
|
const match = cleanedContent.match(pattern.regex);
|
||||||
|
if (match && pattern.priority < bestPriority) {
|
||||||
|
const numberStr = match[1].replace(/[,,]/g, '');
|
||||||
|
let amount = parseFloat(numberStr);
|
||||||
|
|
||||||
|
if (pattern.divider) {
|
||||||
|
amount = amount / pattern.divider;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isNaN(amount) && amount >= 0.01 && amount <= 100000000) {
|
||||||
|
bestMatch = {
|
||||||
|
amount,
|
||||||
|
unit: '万元',
|
||||||
|
text: match[0],
|
||||||
|
originalUnit: pattern.divider ? '元' : '万元'
|
||||||
|
};
|
||||||
|
bestPriority = pattern.priority;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestMatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从API获取PDF URL
|
||||||
|
async function fetchPdfUrlFromApi(pageUrl) {
|
||||||
|
try {
|
||||||
|
const bulletinIdMatch = pageUrl.match(/bulletinDetails\/[^\/]+\/([a-f0-9]+)/i);
|
||||||
|
const bulletinTypeMatch = pageUrl.match(/bulletinType=(\d+)/);
|
||||||
|
|
||||||
|
if (!bulletinIdMatch) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bulletinId = bulletinIdMatch[1];
|
||||||
|
const bulletinType = bulletinTypeMatch ? bulletinTypeMatch[1] : '1';
|
||||||
|
|
||||||
|
const apiUrl = `https://api.jszbtb.com/DataGatewayApi/PublishBulletin/BulletinType/${bulletinType}/ID/${bulletinId}`;
|
||||||
|
|
||||||
|
const response = await http.get(apiUrl, {
|
||||||
|
headers: {
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Referer': 'https://www.jszbcg.com/'
|
||||||
|
},
|
||||||
|
responseType: 'arraybuffer'
|
||||||
|
});
|
||||||
|
|
||||||
|
const responseText = iconv.decode(response.data, 'utf-8');
|
||||||
|
const data = JSON.parse(responseText);
|
||||||
|
|
||||||
|
if (data.success && data.data && data.data.signedPdfUrl) {
|
||||||
|
return data.data.signedPdfUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
} catch (err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractPdfUrl(html, pageUrl) {
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
|
let iframe = $('iframe').first();
|
||||||
|
if (!iframe.length) {
|
||||||
|
iframe = $('iframe[src*="pdf"]').first();
|
||||||
|
}
|
||||||
|
if (!iframe.length) {
|
||||||
|
iframe = $('iframe[src*="viewer"]').first();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iframe.length) {
|
||||||
|
const src = iframe.attr('src');
|
||||||
|
if (!src) return null;
|
||||||
|
|
||||||
|
const match = src.match(/[?&]file=([^&]+)/);
|
||||||
|
if (match) {
|
||||||
|
let pdfUrl = decodeURIComponent(match[1]);
|
||||||
|
|
||||||
|
if (!pdfUrl.startsWith('http://') && !pdfUrl.startsWith('https://')) {
|
||||||
|
try {
|
||||||
|
pdfUrl = new URL(pdfUrl, pageUrl).toString();
|
||||||
|
} catch (err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pdfUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchPdfContent(pdfUrl) {
|
||||||
|
try {
|
||||||
|
const { PDFParse } = await import('pdf-parse');
|
||||||
|
|
||||||
|
const response = await http.get(pdfUrl, {
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
timeout: 30000,
|
||||||
|
});
|
||||||
|
|
||||||
|
const parser = new PDFParse({ data: response.data });
|
||||||
|
const result = await parser.getText();
|
||||||
|
await parser.destroy();
|
||||||
|
|
||||||
|
return result.text;
|
||||||
|
} catch (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function parseDetailEnhanced(html, pageUrl) {
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
|
let pdfUrl = null;
|
||||||
|
|
||||||
|
if (pageUrl.includes('jszbcg.com')) {
|
||||||
|
pdfUrl = await fetchPdfUrlFromApi(pageUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pdfUrl) {
|
||||||
|
pdfUrl = extractPdfUrl(html, pageUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
let content = '';
|
||||||
|
let pdfParsed = false;
|
||||||
|
|
||||||
|
if (pdfUrl) {
|
||||||
|
try {
|
||||||
|
content = await fetchPdfContent(pdfUrl);
|
||||||
|
pdfParsed = true;
|
||||||
|
} catch (err) {
|
||||||
|
const htmlDetail = parseDetail(html);
|
||||||
|
content = htmlDetail.content;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const htmlDetail = parseDetail(html);
|
||||||
|
content = htmlDetail.content;
|
||||||
|
}
|
||||||
|
|
||||||
|
const budget = extractBudget(content);
|
||||||
|
const basicInfo = parseDetail(html);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...basicInfo,
|
||||||
|
content,
|
||||||
|
budget,
|
||||||
|
hasPdf: pdfParsed,
|
||||||
|
pdfUrl: pdfParsed ? pdfUrl : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定时任务执行函数
|
||||||
|
async function executeScheduledTask(config) {
|
||||||
|
try {
|
||||||
|
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 || 100000; // 默认10亿(100000万元)
|
||||||
|
|
||||||
|
const timeRangeNames = {
|
||||||
|
'today': '今日',
|
||||||
|
'thisWeek': '本周',
|
||||||
|
'thisMonth': '本月'
|
||||||
|
};
|
||||||
|
console.log(`采集时间段: ${timeRangeNames[timeRange] || '本月'}`);
|
||||||
|
console.log(`采集时间范围: ${startDate} 至 ${endDate}`);
|
||||||
|
console.log(`金额阈值: ${threshold}万元 (${threshold / 10000}亿元)`);
|
||||||
|
|
||||||
|
// 采集列表
|
||||||
|
const items = await fetchListByDateRange(startDate, endDate, 23);
|
||||||
|
|
||||||
|
if (items.length === 0) {
|
||||||
|
console.log('本月暂无公告数据');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 采集详情
|
||||||
|
console.log('========================================');
|
||||||
|
console.log(`开始采集 ${items.length} 条公告的详情...`);
|
||||||
|
const results = [];
|
||||||
|
for (let i = 0; i < items.length; i++) {
|
||||||
|
const item = items[i];
|
||||||
|
try {
|
||||||
|
console.log(`[${i + 1}/${items.length}] 正在采集: ${item.title}`);
|
||||||
|
const html = await fetchHtml(item.href);
|
||||||
|
const detail = await parseDetailEnhanced(html, item.href);
|
||||||
|
results.push({
|
||||||
|
...item,
|
||||||
|
detail,
|
||||||
|
});
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`采集失败: ${err.message}`);
|
||||||
|
results.push({
|
||||||
|
...item,
|
||||||
|
detail: null,
|
||||||
|
error: err.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 筛选大于阈值的项目
|
||||||
|
const filtered = results.filter((item) => {
|
||||||
|
return item.detail?.budget && item.detail.budget.amount > threshold;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('========================================');
|
||||||
|
console.log(`筛选结果: 找到 ${filtered.length} 个大于 ${threshold}万元 的项目`);
|
||||||
|
|
||||||
|
if (filtered.length === 0) {
|
||||||
|
console.log('本月暂无符合条件的大额项目');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算总金额
|
||||||
|
const total = filtered.reduce(
|
||||||
|
(sum, item) => sum + (item.detail.budget?.amount || 0),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
// 生成报告
|
||||||
|
const report = {
|
||||||
|
summary: {
|
||||||
|
total_count: results.length,
|
||||||
|
filtered_count: filtered.length,
|
||||||
|
threshold: `${threshold}万元`,
|
||||||
|
total_amount: `${total.toFixed(2)}万元`,
|
||||||
|
generated_at: new Date().toISOString(),
|
||||||
|
date_range: { startDate, endDate },
|
||||||
|
},
|
||||||
|
projects: filtered.map((item) => ({
|
||||||
|
title: item.title,
|
||||||
|
date: item.date,
|
||||||
|
publish_time: item.detail.publishTime,
|
||||||
|
budget: item.detail.budget,
|
||||||
|
url: item.href,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
// 发送邮件
|
||||||
|
console.log('========================================');
|
||||||
|
console.log('正在发送邮件报告...');
|
||||||
|
const emailConfig = config.email;
|
||||||
|
|
||||||
|
const result = await sendReportEmail(emailConfig, report);
|
||||||
|
|
||||||
|
console.log('邮件发送成功!');
|
||||||
|
console.log('收件人:', emailConfig.recipients);
|
||||||
|
console.log('MessageId:', result.messageId);
|
||||||
|
console.log('========================================');
|
||||||
|
console.log('定时任务执行完成');
|
||||||
|
console.log('========================================');
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('========================================');
|
||||||
|
console.error('定时任务执行失败:', error.message);
|
||||||
|
console.error(error.stack);
|
||||||
|
console.error('========================================');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存储当前的定时任务
|
||||||
|
let currentScheduledTask = null;
|
||||||
|
|
||||||
|
// 初始化定时任务
|
||||||
|
export function initScheduler() {
|
||||||
|
const config = loadConfig();
|
||||||
|
|
||||||
|
if (!config) {
|
||||||
|
console.error('无法启动定时任务: 配置文件加载失败');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.scheduler || !config.scheduler.enabled) {
|
||||||
|
console.log('定时任务已禁用');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.email || !config.email.smtpHost || !config.email.smtpUser) {
|
||||||
|
console.error('无法启动定时任务: 邮件配置不完整');
|
||||||
|
console.error('请在 config.json 中配置邮件信息');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cronTime = config.scheduler.cronTime || '0 9 * * *';
|
||||||
|
|
||||||
|
console.log('========================================');
|
||||||
|
console.log('定时任务已启动');
|
||||||
|
console.log('执行计划:', cronTime);
|
||||||
|
console.log('金额阈值:', config.scheduler.threshold, '万元');
|
||||||
|
console.log('收件人:', config.email.recipients);
|
||||||
|
console.log('========================================');
|
||||||
|
|
||||||
|
// 如果已有任务在运行,先停止
|
||||||
|
if (currentScheduledTask) {
|
||||||
|
currentScheduledTask.stop();
|
||||||
|
console.log('已停止旧的定时任务');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建定时任务
|
||||||
|
currentScheduledTask = cron.schedule(cronTime, () => {
|
||||||
|
executeScheduledTask(config);
|
||||||
|
}, {
|
||||||
|
timezone: 'Asia/Shanghai'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新加载配置并重启定时任务
|
||||||
|
export function reloadScheduler() {
|
||||||
|
console.log('重新加载定时任务配置...');
|
||||||
|
|
||||||
|
// 停止当前任务
|
||||||
|
if (currentScheduledTask) {
|
||||||
|
currentScheduledTask.stop();
|
||||||
|
currentScheduledTask = null;
|
||||||
|
console.log('已停止当前定时任务');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新初始化
|
||||||
|
initScheduler();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止定时任务
|
||||||
|
export function stopScheduler() {
|
||||||
|
if (currentScheduledTask) {
|
||||||
|
currentScheduledTask.stop();
|
||||||
|
currentScheduledTask = null;
|
||||||
|
console.log('定时任务已停止');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取定时任务状态
|
||||||
|
export function getSchedulerStatus() {
|
||||||
|
const config = loadConfig();
|
||||||
|
return {
|
||||||
|
isRunning: currentScheduledTask !== null,
|
||||||
|
config: config ? {
|
||||||
|
enabled: config.scheduler?.enabled || false,
|
||||||
|
cronTime: config.scheduler?.cronTime || '0 9 * * *',
|
||||||
|
threshold: config.scheduler?.threshold || 100000,
|
||||||
|
} : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 手动执行任务(用于测试)
|
||||||
|
export async function runTaskNow() {
|
||||||
|
const config = loadConfig();
|
||||||
|
if (!config) {
|
||||||
|
throw new Error('配置文件加载失败');
|
||||||
|
}
|
||||||
|
await executeScheduledTask(config);
|
||||||
|
}
|
||||||
151
src/server.js
151
src/server.js
@@ -1,12 +1,15 @@
|
|||||||
|
import 'dotenv/config';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import * as cheerio from 'cheerio';
|
import * as cheerio from 'cheerio';
|
||||||
import iconv from 'iconv-lite';
|
import iconv from 'iconv-lite';
|
||||||
import { sendReportEmail } from './emailService.js';
|
import { sendReportEmail } from './emailService.js';
|
||||||
|
import { initScheduler, runTaskNow, reloadScheduler, getSchedulerStatus } from './scheduler.js';
|
||||||
|
import { extractBudgetWithLLM, testLLMConnection, getLLMStatus, isLLMEnabled } from './llmService.js';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = 3000;
|
const PORT = process.env.PORT || 5000;
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
@@ -319,7 +322,7 @@ function parseDetail(html) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 增强版parseDetail,支持PDF解析
|
// 增强版parseDetail,支持PDF解析和LLM金额提取
|
||||||
async function parseDetailEnhanced(html, pageUrl) {
|
async function parseDetailEnhanced(html, pageUrl) {
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
@@ -357,8 +360,25 @@ async function parseDetailEnhanced(html, pageUrl) {
|
|||||||
content = htmlDetail.content;
|
content = htmlDetail.content;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用现有的extractBudget函数提取金额
|
// 提取金额:优先使用 LLM,失败则降级到正则表达式
|
||||||
const budget = extractBudget(content);
|
let budget = null;
|
||||||
|
if (isLLMEnabled()) {
|
||||||
|
console.log('使用 LLM 提取金额...');
|
||||||
|
budget = await extractBudgetWithLLM(content);
|
||||||
|
if (budget) {
|
||||||
|
console.log(`LLM 提取成功: ${budget.amount} ${budget.unit}`);
|
||||||
|
} else {
|
||||||
|
console.log('LLM 提取失败,降级到正则表达式');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果 LLM 未启用或提取失败,使用正则表达式
|
||||||
|
if (!budget) {
|
||||||
|
budget = extractBudget(content);
|
||||||
|
if (budget) {
|
||||||
|
budget.source = 'regex'; // 标记来源
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获取其他基本信息(标题、发布时间等)
|
// 获取其他基本信息(标题、发布时间等)
|
||||||
const basicInfo = parseDetail(html);
|
const basicInfo = parseDetail(html);
|
||||||
@@ -732,6 +752,129 @@ app.post('/api/test-pdf', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 获取配置
|
||||||
|
app.get('/api/config', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { readFileSync } = await import('fs');
|
||||||
|
const { join } = await import('path');
|
||||||
|
const { fileURLToPath } = await import('url');
|
||||||
|
const { dirname } = await import('path');
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
|
const configPath = join(__dirname, '..', 'config.json');
|
||||||
|
|
||||||
|
const configContent = readFileSync(configPath, 'utf-8');
|
||||||
|
const config = JSON.parse(configContent);
|
||||||
|
|
||||||
|
// 不返回敏感信息(密码和API Key)
|
||||||
|
if (config.email && config.email.smtpPass) {
|
||||||
|
config.email.smtpPass = '***已配置***';
|
||||||
|
}
|
||||||
|
if (config.llm && config.llm.apiKey) {
|
||||||
|
config.llm.apiKey = '***已配置***';
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ success: true, data: config });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 更新配置
|
||||||
|
app.post('/api/config', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { writeFileSync, readFileSync } = await import('fs');
|
||||||
|
const { join } = await import('path');
|
||||||
|
const { fileURLToPath } = await import('url');
|
||||||
|
const { dirname } = await import('path');
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
|
const configPath = join(__dirname, '..', 'config.json');
|
||||||
|
|
||||||
|
const newConfig = req.body;
|
||||||
|
|
||||||
|
// 读取旧配置以保留敏感信息
|
||||||
|
const oldConfigContent = readFileSync(configPath, 'utf-8');
|
||||||
|
const oldConfig = JSON.parse(oldConfigContent);
|
||||||
|
|
||||||
|
// 如果密码字段是占位符,保留原密码
|
||||||
|
if (newConfig.email && newConfig.email.smtpPass === '***已配置***') {
|
||||||
|
newConfig.email.smtpPass = oldConfig.email?.smtpPass || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果 LLM API Key 是占位符,保留原 API Key
|
||||||
|
if (newConfig.llm && newConfig.llm.apiKey === '***已配置***') {
|
||||||
|
newConfig.llm.apiKey = oldConfig.llm?.apiKey || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存配置
|
||||||
|
writeFileSync(configPath, JSON.stringify(newConfig, null, 2), 'utf-8');
|
||||||
|
|
||||||
|
// 重新加载定时任务(如果定时任务配置有变化)
|
||||||
|
reloadScheduler();
|
||||||
|
|
||||||
|
res.json({ success: true, message: '配置已保存并重新加载定时任务' });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// LLM 状态接口
|
||||||
|
app.get('/api/llm/status', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const status = getLLMStatus();
|
||||||
|
res.json({ success: true, data: status });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// LLM 连接测试接口
|
||||||
|
app.post('/api/llm/test', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const result = await testLLMConnection();
|
||||||
|
res.json({ success: result.success, data: result });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 获取定时任务状态
|
||||||
|
app.get('/api/scheduler/status', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const status = getSchedulerStatus();
|
||||||
|
res.json({ success: true, data: status });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ success: false, error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 手动触发定时任务的API(用于测试)
|
||||||
|
app.post('/api/run-scheduled-task', async (req, res) => {
|
||||||
|
try {
|
||||||
|
console.log('手动触发定时任务...');
|
||||||
|
// 在后台执行任务,不阻塞响应
|
||||||
|
runTaskNow().catch(err => {
|
||||||
|
console.error('定时任务执行失败:', err);
|
||||||
|
});
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
message: '定时任务已触发,正在后台执行...'
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: error.message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server running at http://localhost:${PORT}`);
|
console.log(`Server running at http://localhost:${PORT}`);
|
||||||
|
|
||||||
|
// 启动定时任务
|
||||||
|
console.log('正在初始化定时任务...');
|
||||||
|
initScheduler();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user