Browse Source

修复日期处理问题:移除重复的时区转换,确保数据库存储正确的北京时间

master
Default User 1 month ago
parent
commit
d4521335c9
  1. 12
      certificate.html
  2. 33
      server.js

12
certificate.html

@ -488,6 +488,16 @@
}); });
} }
// 格式化日期,确保只显示到日
function formatDate(dateValue) {
if (!dateValue) return '';
// 如果是完整的日期时间字符串,只取日期部分
if (typeof dateValue === 'string' && dateValue.includes('T')) {
return dateValue.split('T')[0];
}
return dateValue;
}
// 显示合格证信息 // 显示合格证信息
function displayCertificate(certificate) { function displayCertificate(certificate) {
const container = document.getElementById('certificateInfo'); const container = document.getElementById('certificateInfo');
@ -520,7 +530,7 @@
</div> </div>
<div class="info-item"> <div class="info-item">
<span class="label">开具日期:</span> <span class="label">开具日期:</span>
<span class="value">${certificate.date || ''}</span> <span class="value">${formatDate(certificate.date) || ''}</span>
</div> </div>
`; `;

33
server.js

@ -81,7 +81,7 @@ const server = http.createServer(async (req, res) => {
try { try {
// 从数据库获取最新的合格证信息 // 从数据库获取最新的合格证信息
const [rows] = await pool.execute( const [rows] = await pool.execute(
'SELECT company as subjectName, phoneNumber as contact, productName, grossWeight as weight, commitBasis as basis, origin, issueDate, DATE_FORMAT(issueDate, "%Y-%m-%d") as date, signature FROM certificate ORDER BY id DESC LIMIT 1' 'SELECT company as subjectName, phoneNumber as contact, productName, grossWeight as weight, commitBasis as basis, origin, DATE_FORMAT(issueDate, "%Y-%m-%d") as date, signature FROM certificate ORDER BY id DESC LIMIT 1'
); );
const certificate = rows.length > 0 ? rows[0] : null; const certificate = rows.length > 0 ? rows[0] : null;
@ -120,9 +120,8 @@ const server = http.createServer(async (req, res) => {
try { try {
// 解析表单数据 // 解析表单数据
const formData = new URLSearchParams(body); const formData = new URLSearchParams(body);
// 使用当前的北京时间(精确到秒)作为开具日期 // 使用当前时间作为开具日期,数据库连接已配置为北京时间
const now = new Date(); const now = new Date();
const beijingTime = new Date(now.getTime() + 8 * 60 * 60 * 1000); // 转换为北京时间
const certificate = { const certificate = {
subjectName: formData.get('subjectName'), subjectName: formData.get('subjectName'),
@ -131,7 +130,7 @@ const server = http.createServer(async (req, res) => {
weight: formData.get('weight'), weight: formData.get('weight'),
basis: formData.get('basis'), basis: formData.get('basis'),
origin: formData.get('origin'), origin: formData.get('origin'),
date: beijingTime, date: now,
signature: formData.get('signature') signature: formData.get('signature')
}; };
@ -176,19 +175,21 @@ const server = http.createServer(async (req, res) => {
console.log('数据插入数据库成功'); console.log('数据插入数据库成功');
// 返回成功响应,确保返回的signature是OSS URL // 返回成功响应,确保返回的signature是OSS URL,日期只显示到日
const responseCertificate = { const responseCertificate = {
...certificate, ...certificate,
signature: signatureUrl signature: signatureUrl,
}; // 格式化日期为YYYY-MM-DD格式
date: certificate.date.toISOString().split('T')[0]
};
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS', 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type' 'Access-Control-Allow-Headers': 'Content-Type'
}); });
res.end(JSON.stringify({ success: true, certificate: responseCertificate })); res.end(JSON.stringify({ success: true, certificate: responseCertificate }));
} catch (error) { } catch (error) {
console.error('处理表单数据失败:', error.message); console.error('处理表单数据失败:', error.message);
res.writeHead(500, { res.writeHead(500, {

Loading…
Cancel
Save