Browse Source

修复时间处理问题,统一使用getBeijingTime()函数

pull/1/head
徐飞洋 3 months ago
parent
commit
e26a6b7d1b
  1. 39
      server-example/server-mysql.js

39
server-example/server-mysql.js

@ -160,9 +160,8 @@ const upload = multer({
// 添加请求日志中间件,捕获所有到达服务器的请求(必须放在bodyParser之后)
app.use((req, res, next) => {
// 将UTC时间转换为北京时间(UTC+8)
const now = new Date();
const beijingTime = new Date(now.getTime() + 8 * 60 * 60 * 1000);
// 使用统一的时间处理函数获取当前时间
const beijingTime = getBeijingTime();
const formattedTime = beijingTime.toISOString().replace('Z', '+08:00');
console.log(`[${formattedTime}] 收到请求: ${req.method} ${req.url}`);
@ -3273,7 +3272,7 @@ async function handleAddImagesToExistingProduct(req, res, existingProductId, upl
await Product.update({
imageUrls: imageUrlsJson,
allImageUrls: imageUrlsJson,
updated_at: new Date(),
updated_at: getBeijingTime(),
hasMultipleImages: mergedImageUrls.length > 1,
totalImages: mergedImageUrls.length
}, {
@ -3664,7 +3663,7 @@ app.post('/api/products/edit', async (req, res) => {
await Product.update(
{
...updateData,
updated_at: new Date()
updated_at: getBeijingTime()
},
{
where: { productId }
@ -5091,8 +5090,8 @@ app.post('/api/product/publish', async (req, res) => {
yolk: product.yolk || '',
specification: product.specification || '',
status: 'pending_review', // 严格设置为待审核状态
created_at: new Date(),
updated_at: new Date()
created_at: getBeijingTime(),
updated_at: getBeijingTime()
});
// 立即验证创建后的状态
@ -5635,7 +5634,7 @@ app.post('/api/product/edit', async (req, res) => {
originalQuantity: originalQuantity,
// 如果是重新提交审核,清除拒绝原因
rejectReason: isResubmit ? null : existingProduct.rejectReason,
updated_at: new Date()
updated_at: getBeijingTime()
};
// 【新增】更新前的最终数据验证
@ -5975,7 +5974,7 @@ app.post('/api/settlement/submit', async (req, res) => {
proofurl: proofurl || '', // 证明材料 - NOT NULL约束,使用空字符串
brandurl: brandurl || '', // 品牌授权链文件
partnerstatus: 'underreview', // 合作商状态明确设置为审核中,覆盖数据库默认值
updated_at: new Date()
updated_at: getBeijingTime()
}, {
where: { userId: user.userId }
});
@ -6325,7 +6324,7 @@ app.post('/api/conversations/:conversationId/read', async (req, res) => {
const conversationId = req.params.conversationId;
const { userId, managerId, type } = req.body;
const now = new Date();
const now = getBeijingTime();
let updateField;
if (type === 'user') {
@ -6478,7 +6477,7 @@ wss.on('connection', (ws, req) => {
managerId: null,
isUser: false,
isManager: false,
connectedAt: new Date()
connectedAt: getBeijingTime()
});
// 连接认证处理
@ -6660,7 +6659,7 @@ wss.on('connection', (ws, req) => {
// 更新chat_online_status表的心跳时间
async function updateChatOnlineStatusHeartbeat(userId, type) {
try {
const now = new Date();
const now = getBeijingTime();
// 根据userId是否为null使用不同的SQL语句
let sql;
let replacements;
@ -6687,7 +6686,7 @@ async function updateChatOnlineStatusHeartbeat(userId, type) {
// 更新chat_online_status表为离线状态
async function updateChatOnlineStatusOffline(userId, type) {
try {
const now = new Date();
const now = getBeijingTime();
// 根据userId是否为null使用不同的SQL语句
let sql;
let replacements;
@ -6916,7 +6915,7 @@ async function handleAuth(ws, data) {
// 更新chat_online_status表
async function updateChatOnlineStatus(userId, type, socketId, deviceInfo) {
try {
const now = new Date();
const now = getBeijingTime();
// 使用INSERT ... ON DUPLICATE KEY UPDATE语法确保只更新或插入一条记录
await sequelize.query(
`INSERT INTO chat_online_status
@ -7175,7 +7174,7 @@ async function createOrGetConversation(userId, managerId) {
// 创建新会话
const conversationId = crypto.randomUUID();
const now = new Date();
const now = getBeijingTime();
await sequelize.query(
`INSERT INTO chat_conversations
@ -7625,7 +7624,7 @@ async function handleChatMessage(ws, payload) {
// 生成消息ID和时间戳
const messageId = payload.messageId || crypto.randomUUID(); // 允许前端提供messageId
const now = new Date();
const now = getBeijingTime();
console.log('准备存储消息:', {
messageId,
@ -7806,8 +7805,8 @@ async function storeMessage(messageData) {
fileUrl || null,
fileSize || null,
duration || null,
createdAt || new Date(),
createdAt || new Date()
createdAt || getBeijingTime(),
createdAt || getBeijingTime()
]
}
);
@ -7866,7 +7865,7 @@ async function updateUnreadCount(conversationId, countField, increment) {
`UPDATE chat_conversations
SET ${countField} = ${countField} + ?, updated_at = ?
WHERE conversation_id = ?`,
{ replacements: [increment, new Date(), conversationId] }
{ replacements: [increment, getBeijingTime(), conversationId] }
);
} catch (error) {
console.error('更新未读计数失败:', error);
@ -7896,7 +7895,7 @@ async function handleMarkRead(ws, payload) {
}
try {
const now = new Date();
const now = getBeijingTime();
let countField;
let updateQuery;
let updateParams;

Loading…
Cancel
Save