From d4521335c9bcca69e19626c6a34182d1a4f3c122 Mon Sep 17 00:00:00 2001 From: Default User Date: Fri, 30 Jan 2026 14:16:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E6=9C=9F=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=97=AE=E9=A2=98=EF=BC=9A=E7=A7=BB=E9=99=A4=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E6=97=B6=E5=8C=BA=E8=BD=AC=E6=8D=A2=EF=BC=8C?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E7=9A=84=E5=8C=97=E4=BA=AC=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- certificate.html | 12 +++++++++++- server.js | 35 ++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/certificate.html b/certificate.html index 0e93a08..c20d090 100644 --- a/certificate.html +++ b/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) { const container = document.getElementById('certificateInfo'); @@ -520,7 +530,7 @@
开具日期: - ${certificate.date || ''} + ${formatDate(certificate.date) || ''}
`; diff --git a/server.js b/server.js index e2d5b57..06dd2e5 100644 --- a/server.js +++ b/server.js @@ -81,7 +81,7 @@ const server = http.createServer(async (req, res) => { try { // 从数据库获取最新的合格证信息 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; @@ -120,9 +120,8 @@ const server = http.createServer(async (req, res) => { try { // 解析表单数据 const formData = new URLSearchParams(body); - // 使用当前的北京时间(精确到秒)作为开具日期 + // 使用当前时间作为开具日期,数据库连接已配置为北京时间 const now = new Date(); - const beijingTime = new Date(now.getTime() + 8 * 60 * 60 * 1000); // 转换为北京时间 const certificate = { subjectName: formData.get('subjectName'), @@ -131,7 +130,7 @@ const server = http.createServer(async (req, res) => { weight: formData.get('weight'), basis: formData.get('basis'), origin: formData.get('origin'), - date: beijingTime, + date: now, signature: formData.get('signature') }; @@ -176,19 +175,21 @@ const server = http.createServer(async (req, res) => { console.log('数据插入数据库成功'); - // 返回成功响应,确保返回的signature是OSS URL - const responseCertificate = { - ...certificate, - signature: signatureUrl - }; - - res.writeHead(200, { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type' - }); - res.end(JSON.stringify({ success: true, certificate: responseCertificate })); + // 返回成功响应,确保返回的signature是OSS URL,日期只显示到日 + const responseCertificate = { + ...certificate, + signature: signatureUrl, + // 格式化日期为YYYY-MM-DD格式 + date: certificate.date.toISOString().split('T')[0] + }; + + res.writeHead(200, { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type' + }); + res.end(JSON.stringify({ success: true, certificate: responseCertificate })); } catch (error) { console.error('处理表单数据失败:', error.message); res.writeHead(500, {