129 lines
4.0 KiB
JavaScript
129 lines
4.0 KiB
JavaScript
|
|
const express = require('express');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
const app = express();
|
||
|
|
const PORT = 3000;
|
||
|
|
|
||
|
|
// Middleware
|
||
|
|
app.use(express.json());
|
||
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
||
|
|
|
||
|
|
// API: 搜索优惠券 (预留淘宝/京东联盟API接入点)
|
||
|
|
app.get('/api/search', async (req, res) => {
|
||
|
|
const { keyword } = req.query;
|
||
|
|
if (!keyword) {
|
||
|
|
return res.json({ success: false, message: '请输入搜索关键词' });
|
||
|
|
}
|
||
|
|
|
||
|
|
// TODO: 接入淘宝联盟API / 京东联盟API
|
||
|
|
// 目前返回模拟数据用于本地验证
|
||
|
|
const mockResults = generateMockResults(keyword);
|
||
|
|
res.json({ success: true, data: mockResults });
|
||
|
|
});
|
||
|
|
|
||
|
|
// API: 今日特价
|
||
|
|
app.get('/api/today-deals', (req, res) => {
|
||
|
|
// TODO: 接入真实特价API
|
||
|
|
const deals = generateMockDeals();
|
||
|
|
res.json({ success: true, data: deals });
|
||
|
|
});
|
||
|
|
|
||
|
|
// API: 线报广场
|
||
|
|
app.get('/api/linbao', (req, res) => {
|
||
|
|
// TODO: 接入线报数据源
|
||
|
|
const linbao = generateMockLinbao();
|
||
|
|
res.json({ success: true, data: linbao });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 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'));
|
||
|
|
});
|
||
|
|
|
||
|
|
// Mock data generators
|
||
|
|
function generateMockResults(keyword) {
|
||
|
|
return [
|
||
|
|
{
|
||
|
|
id: '1',
|
||
|
|
title: `${keyword} 空气炸锅 智能版`,
|
||
|
|
price: 199,
|
||
|
|
originalPrice: 299,
|
||
|
|
coupon: 50,
|
||
|
|
commission: 15,
|
||
|
|
shop: '某某电器旗舰店',
|
||
|
|
link: '#',
|
||
|
|
platform: 'taobao'
|
||
|
|
},
|
||
|
|
{
|
||
|
|
id: '2',
|
||
|
|
title: `${keyword} 破壁机 多功能`,
|
||
|
|
price: 299,
|
||
|
|
originalPrice: 499,
|
||
|
|
coupon: 80,
|
||
|
|
commission: 25,
|
||
|
|
shop: '厨房电器专营店',
|
||
|
|
link: '#',
|
||
|
|
platform: 'taobao'
|
||
|
|
},
|
||
|
|
{
|
||
|
|
id: '3',
|
||
|
|
title: `${keyword} 电饭煲 智能预约`,
|
||
|
|
price: 159,
|
||
|
|
originalPrice: 259,
|
||
|
|
coupon: 40,
|
||
|
|
commission: 12,
|
||
|
|
shop: '品牌电器直营',
|
||
|
|
link: '#',
|
||
|
|
platform: 'jd'
|
||
|
|
}
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
function generateMockDeals() {
|
||
|
|
return [
|
||
|
|
{ id: 'd1', title: '苏泊尔空气炸锅', price: 189, originalPrice: 299, discount: '63折', endTime: '2026-06-05' },
|
||
|
|
{ id: 'd2', title: '九阳破壁机', price: 299, originalPrice: 599, discount: '5折', endTime: '2026-06-05' },
|
||
|
|
{ id: 'd3', title: '美的电饭煲', price: 159, originalPrice: 299, discount: '53折', endTime: '2026-06-05' },
|
||
|
|
{ id: 'd4', title: '小米手环8', price: 199, originalPrice: 299, discount: '67折', endTime: '2026-06-06' },
|
||
|
|
{ id: 'd5', title: '华为手表GT4', price: 899, originalPrice: 1488, discount: '6折', endTime: '2026-06-07' }
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
function generateMockLinbao() {
|
||
|
|
return [
|
||
|
|
{ id: 'l1', title: '淘宝618预售开启,付定金可抵双倍', source: '官方', time: '10分钟前', hot: 98 },
|
||
|
|
{ id: 'l2', title: '美团外卖新用户立减15元', source: '美团', time: '30分钟前', hot: 85 },
|
||
|
|
{ id: 'l3', title: '京东闪购不定时放大额券', source: '京东', time: '1小时前', hot: 92 },
|
||
|
|
{ id: 'l4', title: '饿了么端午红包来袭', source: '饿了么', time: '2小时前', hot: 78 },
|
||
|
|
{ id: 'l5', title: '拼多多百亿补贴再加码', source: '拼多多', time: '3小时前', hot: 88 }
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
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 历史价格`);
|
||
|
|
});
|