@ -36,6 +36,37 @@ async function testDbConnection() {
testDbConnection ( ) ;
const server = http . createServer ( async ( req , res ) => {
// 处理GET请求,获取最新的合格证信息
if ( req . method === 'GET' && req . url === '/getLatestCertificate' ) {
try {
// 从数据库获取最新的合格证信息
const [ rows ] = await pool . execute (
'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 ;
// 返回成功响应
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 } ) ) ;
} catch ( error ) {
console . error ( '获取合格证信息失败:' , error . message ) ;
res . writeHead ( 500 , {
'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 : false , error : '获取失败,请重试' } ) ) ;
}
return ;
}
// 处理POST请求
if ( req . method === 'POST' && req . url === '/submit' ) {
try {