Browse Source

实现预创建货源功能

Boss
Default User 1 month ago
parent
commit
6296a60fc3
  1. 73
      Reject.js
  2. 18
      supply.html

73
Reject.js

@ -970,18 +970,19 @@ app.post('/api/supplies/create', async (req, res) => {
quality, quality,
producting: producting || '', // 添加产品包装 producting: producting || '', // 添加产品包装
region, region,
status: 'published', // 直接上架,而不是审核中 status: req.body.pre_create ? 'pending' : 'published', // 设置预创建时间时状态为待创建,否则直接上架
supplyStatus: supplyStatus || '', // 预售/现货 supplyStatus: supplyStatus || '', // 预售/现货
sourceType: sourceType || '', // 平台货源/三方认证/三方未认证 sourceType: sourceType || '', // 平台货源/三方认证/三方未认证
description: description || '', description: description || '',
rejectReason: '', rejectReason: '',
imageUrls: uploadedImageUrls.length > 0 ? JSON.stringify(uploadedImageUrls) : '[]', imageUrls: uploadedImageUrls.length > 0 ? JSON.stringify(uploadedImageUrls) : '[]',
created_at: new Date(), created_at: req.body.pre_create ? null : new Date(), // 设置预创建时间时不设置创建时间,由定时任务设置
product_contact: productContact, // 添加联系人名称 product_contact: productContact, // 添加联系人名称
contact_phone: contactPhone, // 添加联系人电话 contact_phone: contactPhone, // 添加联系人电话
autoOfflineTime: req.body.autoOfflineTime, // 自动下架时间 autoOfflineTime: req.body.autoOfflineTime, // 自动下架时间
autoOfflineDays: req.body.autoOfflineDays, // 自动下架天数 autoOfflineDays: req.body.autoOfflineDays, // 自动下架天数
autoOfflineHours: req.body.autoOfflineHours // 自动下架小时数 autoOfflineHours: req.body.autoOfflineHours, // 自动下架小时数
pre_create: req.body.pre_create // 预创建时间
}; };
// 插入商品数据 // 插入商品数据
@ -994,21 +995,21 @@ app.post('/api/supplies/create', async (req, res) => {
if (columns.length === 0) { if (columns.length === 0) {
// 没有quality字段,不包含quality字段的插入 // 没有quality字段,不包含quality字段的插入
insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, freshness, costprice, quantity, grossWeight, yolk, specification, producting, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone, autoOfflineTime, autoOfflineDays, autoOfflineHours, spec_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, freshness, costprice, quantity, grossWeight, yolk, specification, producting, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone, autoOfflineTime, autoOfflineDays, autoOfflineHours, spec_status, pre_create) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
insertParams = [ insertParams = [
productId, productData.sellerId, productName, category || '', req.body.freshness || '', costprice || '', quantity, grossWeight, productId, productData.sellerId, productName, category || '', req.body.freshness || '', costprice || '', quantity, grossWeight,
yolk, specification, producting, region, productData.status, productData.supplyStatus, productData.sourceType, yolk, specification, producting, region, productData.status, productData.supplyStatus, productData.sourceType,
productData.description, productData.rejectReason, productData.imageUrls, new Date(), new Date(), productData.description, productData.rejectReason, productData.imageUrls, new Date(), new Date(),
productContact, contactPhone, req.body.autoOfflineTime, req.body.autoOfflineDays, req.body.autoOfflineHours, spec_status || '' // 添加联系人信息和自动下架时间、小时数、规格状态 productContact, contactPhone, req.body.autoOfflineTime, req.body.autoOfflineDays, req.body.autoOfflineHours, spec_status || '', req.body.pre_create // 添加联系人信息和自动下架时间、小时数、规格状态、预创建时间
]; ];
} else { } else {
// 有quality字段,包含quality字段的插入 // 有quality字段,包含quality字段的插入
insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, freshness, costprice, quantity, grossWeight, yolk, specification, producting, quality, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone, autoOfflineTime, autoOfflineDays, autoOfflineHours, spec_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; insertQuery = 'INSERT INTO products (productId, sellerId, productName, category, freshness, costprice, quantity, grossWeight, yolk, specification, producting, quality, region, status, supplyStatus, sourceType, description, rejectReason, imageUrls, created_at, audit_time, product_contact, contact_phone, autoOfflineTime, autoOfflineDays, autoOfflineHours, spec_status, pre_create) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
insertParams = [ insertParams = [
productId, productData.sellerId, productName, category || '', req.body.freshness || '', costprice || '', quantity, grossWeight, productId, productData.sellerId, productName, category || '', req.body.freshness || '', costprice || '', quantity, grossWeight,
yolk, specification, producting, quality, region, productData.status, productData.supplyStatus, productData.sourceType, yolk, specification, producting, quality, region, productData.status, productData.supplyStatus, productData.sourceType,
productData.description, productData.rejectReason, productData.imageUrls, productData.description, productData.rejectReason, productData.imageUrls,
new Date(), new Date(), productContact, contactPhone, req.body.autoOfflineTime, req.body.autoOfflineDays, req.body.autoOfflineHours, spec_status || '' // 添加联系人信息和自动下架时间、小时数、规格状态 new Date(), new Date(), productContact, contactPhone, req.body.autoOfflineTime, req.body.autoOfflineDays, req.body.autoOfflineHours, spec_status || '', req.body.pre_create // 添加联系人信息和自动下架时间、小时数、规格状态、预创建时间
]; ];
} }
@ -2510,6 +2511,64 @@ async function ensureDatabaseSchema() {
} }
} }
// 定时任务:检查并自动创建达到预创建时间的货源
async function checkPreCreateSupplies() {
try {
const connection = await pool.getConnection();
console.log('开始检查预创建货源...');
// 查询达到预创建时间且状态为待创建的货源
const [supplies] = await connection.query(`
SELECT * FROM products
WHERE pre_create IS NOT NULL
AND pre_create <= NOW()
AND status = 'pending'
`);
console.log(`发现 ${supplies.length} 个达到预创建时间的货源`);
// 处理每个达到预创建时间的货源
for (const supply of supplies) {
try {
// 更新货源状态为已发布,将创建时间设置为预创建时间
await connection.query(`
UPDATE products
SET status = 'published', created_at = pre_create
WHERE productId = ?
`, [supply.productId]);
console.log(`已自动创建货源: ${supply.productId} - ${supply.productName}`);
// 可选:通过WebSocket通知相关用户
const notification = {
type: 'supply_created',
message: `您的预创建货源 ${supply.productName} 已自动创建`,
supplyId: supply.productId,
timestamp: new Date().toISOString()
};
// 发送通知给所有连接的客户端
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(notification));
}
});
} catch (error) {
console.error(`处理预创建货源失败 ${supply.productId}:`, error.message);
}
}
connection.release();
} catch (error) {
console.error('检查预创建货源失败:', error.message);
}
}
// 启动定时任务(每五分钟检查一次)
setInterval(checkPreCreateSupplies, 300000);
console.log('预创建货源检查定时任务已启动,每五分钟执行一次');
// 启动服务器 // 启动服务器
startServer(); startServer();

18
supply.html

@ -1623,6 +1623,13 @@
<option value="10080">10080分钟 (7天)</option> <option value="10080">10080分钟 (7天)</option>
</select> </select>
</div> </div>
<!-- 预创建时间 -->
<div class="form-group">
<label class="form-label">预创建时间</label>
<input type="datetime-local" class="form-input" id="preCreateTime" placeholder="选择预计创建时间">
<div style="font-size: 12px; color: #999; margin-top: 5px;">设置预创建时间后,系统将在指定时间自动创建货源</div>
</div>
</form> </form>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
@ -6533,7 +6540,16 @@
phoneNumber: '13800138000', // 使用默认电话号码 phoneNumber: '13800138000', // 使用默认电话号码
autoOfflineDays: null, // 不再使用天数,设置为null autoOfflineDays: null, // 不再使用天数,设置为null
autoOfflineHours: autoOfflineHours, autoOfflineHours: autoOfflineHours,
autoOfflineTime: autoOfflineTimeStr autoOfflineTime: autoOfflineTimeStr,
pre_create: document.getElementById('preCreateTime').value ? function() {
const d = new Date(document.getElementById('preCreateTime').value);
return d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0') + ' ' +
String(d.getHours()).padStart(2, '0') + ':' +
String(d.getMinutes()).padStart(2, '0') + ':' +
String(d.getSeconds()).padStart(2, '0');
}() : null
}; };
// 验证表单 // 验证表单

Loading…
Cancel
Save