2026-06-22 22:53:09 +08:00
|
|
|
const express = require('express');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
|
|
const app = express();
|
2026-06-23 22:53:14 +08:00
|
|
|
const PORT = process.env.PORT || 3000;
|
2026-06-22 22:53:09 +08:00
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
|
2026-06-23 22:53:14 +08:00
|
|
|
// CORS headers
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
|
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
|
return res.sendStatus(200);
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Request logging
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
res.on('finish', () => {
|
|
|
|
|
const duration = Date.now() - start;
|
|
|
|
|
console.log(`${req.method} ${req.path} - ${res.statusCode} (${duration}ms)`);
|
|
|
|
|
});
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-22 22:53:09 +08:00
|
|
|
// API: 搜索优惠券 (预留淘宝/京东联盟API接入点)
|
|
|
|
|
app.get('/api/search', async (req, res) => {
|
|
|
|
|
const { keyword } = req.query;
|
|
|
|
|
if (!keyword) {
|
2026-06-23 22:53:14 +08:00
|
|
|
return res.status(400).json({ success: false, message: '请输入搜索关键词' });
|
2026-06-22 22:53:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-23 22:53:14 +08:00
|
|
|
try {
|
|
|
|
|
// TODO: 接入淘宝联盟API / 京东联盟API
|
|
|
|
|
// 目前返回模拟数据用于本地验证
|
|
|
|
|
const mockResults = generateMockResults(keyword);
|
|
|
|
|
res.json({ success: true, data: mockResults, keyword, timestamp: Date.now() });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('搜索API错误:', error);
|
|
|
|
|
res.status(500).json({ success: false, message: '服务器错误,请重试' });
|
|
|
|
|
}
|
2026-06-22 22:53:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// API: 今日特价
|
|
|
|
|
app.get('/api/today-deals', (req, res) => {
|
2026-06-23 22:53:14 +08:00
|
|
|
try {
|
|
|
|
|
// TODO: 接入真实特价API
|
|
|
|
|
const deals = generateMockDeals();
|
|
|
|
|
res.json({ success: true, data: deals, timestamp: Date.now() });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('特价API错误:', error);
|
|
|
|
|
res.status(500).json({ success: false, message: '服务器错误,请重试' });
|
|
|
|
|
}
|
2026-06-22 22:53:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// API: 线报广场
|
|
|
|
|
app.get('/api/linbao', (req, res) => {
|
2026-06-23 22:53:14 +08:00
|
|
|
try {
|
|
|
|
|
// TODO: 接入线报数据源
|
|
|
|
|
const linbao = generateMockLinbao();
|
|
|
|
|
res.json({ success: true, data: linbao, timestamp: Date.now() });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('线报API错误:', error);
|
|
|
|
|
res.status(500).json({ success: false, message: '服务器错误,请重试' });
|
|
|
|
|
}
|
2026-06-22 22:53:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// API: 历史价格查询 (预留)
|
|
|
|
|
app.get('/api/price-history', (req, res) => {
|
|
|
|
|
const { itemid } = req.query;
|
|
|
|
|
// TODO: 接入价格监控服务
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: {
|
|
|
|
|
itemid,
|
|
|
|
|
history: [
|
|
|
|
|
{ date: '2026-05-01', price: 89 },
|
|
|
|
|
{ date: '2026-05-15', price: 85 },
|
|
|
|
|
{ date: '2026-05-28', price: 79 },
|
|
|
|
|
{ date: '2026-06-01', price: 75 },
|
|
|
|
|
{ date: '2026-06-04', price: 72 }
|
|
|
|
|
],
|
|
|
|
|
lowest: { price: 72, date: '2026-06-04' }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Serve main page
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
|
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-23 22:53:14 +08:00
|
|
|
// 404 handler
|
|
|
|
|
app.use((req, res) => {
|
|
|
|
|
res.status(404).json({ success: false, message: 'API不存在' });
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-22 22:53:09 +08:00
|
|
|
// Mock data generators
|
|
|
|
|
function generateMockResults(keyword) {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
id: '1',
|
2026-06-22 23:52:54 +08:00
|
|
|
title: `苏泊尔空气炸锅5.5L大容量${keyword}智能款`,
|
|
|
|
|
price: 189,
|
|
|
|
|
originalPrice: 399,
|
|
|
|
|
coupon: 60,
|
2026-06-22 22:53:09 +08:00
|
|
|
commission: 15,
|
2026-06-22 23:52:54 +08:00
|
|
|
shop: '苏泊尔官方旗舰店',
|
|
|
|
|
link: 'https://s.click.taobao.com/abc123',
|
|
|
|
|
platform: 'taobao',
|
|
|
|
|
img: 'https://img.alicdn.com/bao/uploaded/O1CN01Z1a2b3_!!600000000.jpg'
|
2026-06-22 22:53:09 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '2',
|
2026-06-22 23:52:54 +08:00
|
|
|
title: `九阳破壁机L12-Y5多功能全自动${keyword}`,
|
2026-06-22 22:53:09 +08:00
|
|
|
price: 299,
|
2026-06-22 23:52:54 +08:00
|
|
|
originalPrice: 699,
|
|
|
|
|
coupon: 100,
|
2026-06-22 22:53:09 +08:00
|
|
|
commission: 25,
|
2026-06-22 23:52:54 +08:00
|
|
|
shop: '九阳厨房电器旗舰店',
|
|
|
|
|
link: 'https://s.click.taobao.com/def456',
|
|
|
|
|
platform: 'taobao',
|
|
|
|
|
img: 'https://img.alicdn.com/bao/uploaded/O1CN01C3d4e5_!!600000000.jpg'
|
2026-06-22 22:53:09 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '3',
|
2026-06-22 23:52:54 +08:00
|
|
|
title: `美的${keyword}电饭煲MB-RE476智能预约`,
|
2026-06-22 22:53:09 +08:00
|
|
|
price: 159,
|
2026-06-22 23:52:54 +08:00
|
|
|
originalPrice: 399,
|
|
|
|
|
coupon: 50,
|
2026-06-22 22:53:09 +08:00
|
|
|
commission: 12,
|
2026-06-22 23:52:54 +08:00
|
|
|
shop: '美的官方旗舰店',
|
|
|
|
|
link: 'https://u.jd.com/gh789',
|
|
|
|
|
platform: 'jd',
|
|
|
|
|
img: 'https://img.alicdn.com/bao/uploaded/O1CN01F6h7i8_!!600000000.jpg'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '4',
|
|
|
|
|
title: `小米手环8 NFC版${keyword}运动健康监测`,
|
|
|
|
|
price: 229,
|
|
|
|
|
originalPrice: 399,
|
|
|
|
|
coupon: 30,
|
|
|
|
|
commission: 18,
|
|
|
|
|
shop: '小米官方旗舰店',
|
|
|
|
|
link: 'https://s.click.taobao.com/jkl012',
|
|
|
|
|
platform: 'taobao',
|
|
|
|
|
img: 'https://img.alicdn.com/bao/uploaded/O1CN01G8m9n0_!!600000000.jpg'
|
2026-06-22 22:53:09 +08:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateMockDeals() {
|
|
|
|
|
return [
|
2026-06-22 23:52:54 +08:00
|
|
|
{ id: 'd1', title: '苏泊尔空气炸锅5.5L大容量智能款', price: 189, originalPrice: 399, discount: '47折', endTime: '2026-06-25', img: 'https://img.alicdn.com/bao/uploaded/O1CN01abc123_!!600000000.jpg', platform: 'taobao', commission: 15 },
|
|
|
|
|
{ id: 'd2', title: '九阳破壁机L12-Y536多功能全自动', price: 299, originalPrice: 699, discount: '43折', endTime: '2026-06-25', img: 'https://img.alicdn.com/bao/uploaded/O1CN01def456_!!600000000.jpg', platform: 'taobao', commission: 25 },
|
|
|
|
|
{ id: 'd3', title: '美的电饭煲MB-RE476智能预约', price: 159, originalPrice: 399, discount: '40折', endTime: '2026-06-23', img: 'https://img.alicdn.com/bao/uploaded/O1CN01ghi789_!!600000000.jpg', platform: 'jd', commission: 12 },
|
|
|
|
|
{ id: 'd4', title: '小米手环8 NFC版运动健康', price: 229, originalPrice: 399, discount: '57折', endTime: '2026-06-26', img: 'https://img.alicdn.com/bao/uploaded/O1CN01jkl012_!!600000000.jpg', platform: 'taobao', commission: 18 },
|
|
|
|
|
{ id: 'd5', title: '戴森吹风机HD15负离子护发', price: 2199, originalPrice: 3299, discount: '67折', endTime: '2026-06-28', img: 'https://img.alicdn.com/bao/uploaded/O1CN01mno345_!!600000000.jpg', platform: 'taobao', commission: 180 },
|
|
|
|
|
{ id: 'd6', title: '添可洗地机2.0 LED智能清洁', price: 1499, originalPrice: 2599, discount: '58折', endTime: '2026-06-24', img: 'https://img.alicdn.com/bao/uploaded/O1CN01pqr678_!!600000000.jpg', platform: 'jd', commission: 120 }
|
2026-06-22 22:53:09 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateMockLinbao() {
|
|
|
|
|
return [
|
2026-06-22 23:52:54 +08:00
|
|
|
{ id: 'l1', title: '🎉 淘宝618预售开启!付定金可抵双倍,AirPods Pro低至999', source: '官方公告', time: '10分钟前', hot: 98, url: 'https://taobao.com' },
|
|
|
|
|
{ id: 'l2', title: '🔥 京东PLUS会员专属大额券,500-50全场通用', source: '京东', time: '30分钟前', hot: 92, url: 'https://jd.com' },
|
|
|
|
|
{ id: 'l3', title: '💰 美团外卖新用户首单立减15元,红包秒到账', source: '美团', time: '1小时前', hot: 85, url: 'https://meituan.com' },
|
|
|
|
|
{ id: 'l4', title: '🛒 拼多多百亿补贴再加码,iPhone15 Pro Max直降500', source: '拼多多', time: '2小时前', hot: 95, url: 'https://pinduoduo.com' },
|
|
|
|
|
{ id: 'l5', title: '⏰ 饿了么端午粽子专场,满39减10', source: '饿了么', time: '3小时前', hot: 78, url: 'https://elem.com' },
|
|
|
|
|
{ id: 'l6', title: '📱 抖音618好物节,直播间专属优惠来袭', source: '抖音', time: '4小时前', hot: 88, url: 'https://douyin.com' }
|
2026-06-22 22:53:09 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
|
console.log(`智币省 服务已启动: http://localhost:${PORT}`);
|
|
|
|
|
console.log(`API端点:`);
|
|
|
|
|
console.log(` - GET /api/search?keyword=xxx 搜索优惠券`);
|
|
|
|
|
console.log(` - GET /api/today-deals 今日特价`);
|
|
|
|
|
console.log(` - GET /api/linbao 线报广场`);
|
|
|
|
|
console.log(` - GET /api/price-history?itemid=xxx 历史价格`);
|
|
|
|
|
});
|