feat: 积分商城-供应商
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
PORT=8050
|
||||
SUPPLIED_SERVICE_BASE_URL=http://127.0.0.1:8888
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "esf-relay",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node src/server.mjs"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import http from 'node:http'
|
||||
|
||||
const port = Number(process.env.PORT || 8050)
|
||||
const suppliedServiceBaseUrl = String(process.env.SUPPLIED_SERVICE_BASE_URL || 'http://127.0.0.1:8888').replace(/\/$/, '')
|
||||
|
||||
const addressRoutes = new Map([
|
||||
['/supplierOpenApi/pointOrder/queryOrderInfo', '/supplierOpenApi/pointOrder/queryOrderInfo'],
|
||||
['/supplierOpenApi/pointOrder/queryReconciliationInfo', '/supplierOpenApi/pointOrder/queryReconciliationInfo'],
|
||||
['/supplierOpenApi/pointOrder/unReconciledMsg', '/supplierOpenApi/pointOrder/unReconciledMsg'],
|
||||
['/supplierOpenApi/pointOrder/reconciliationResult', '/supplierOpenApi/pointOrder/reconciliationResult']
|
||||
])
|
||||
|
||||
const readBody = request => new Promise((resolve, reject) => {
|
||||
const chunks = []
|
||||
request.on('data', chunk => chunks.push(chunk))
|
||||
request.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
|
||||
request.on('error', reject)
|
||||
})
|
||||
|
||||
const getForwardPath = address4 => {
|
||||
if (typeof address4 !== 'string') {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return addressRoutes.get(new URL(address4).pathname) || null
|
||||
} catch {
|
||||
return addressRoutes.get(address4) || null
|
||||
}
|
||||
}
|
||||
|
||||
const writeJson = (response, statusCode, body) => {
|
||||
response.writeHead(statusCode, { 'Content-Type': 'application/json;charset=UTF-8' })
|
||||
response.end(JSON.stringify(body))
|
||||
}
|
||||
|
||||
const server = http.createServer(async (request, response) => {
|
||||
if (request.method !== 'POST' || request.url !== '/esf/S120020967Api') {
|
||||
writeJson(response, 404, { resultCode: 404, message: '接口不存在' })
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const bodyText = await readBody(request)
|
||||
const body = JSON.parse(bodyText)
|
||||
const forwardPath = getForwardPath(body.Address4)
|
||||
if (!forwardPath) {
|
||||
writeJson(response, 400, { resultCode: 400, message: '不支持的Address4' })
|
||||
return
|
||||
}
|
||||
|
||||
const upstreamResponse = await fetch(`${suppliedServiceBaseUrl}${forwardPath}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
|
||||
body: bodyText
|
||||
})
|
||||
const responseBody = await upstreamResponse.text()
|
||||
response.writeHead(upstreamResponse.status, {
|
||||
'Content-Type': upstreamResponse.headers.get('content-type') || 'application/json;charset=UTF-8'
|
||||
})
|
||||
response.end(responseBody)
|
||||
} catch (error) {
|
||||
writeJson(response, 502, {
|
||||
resultCode: 502,
|
||||
message: error instanceof Error ? error.message : 'ESF中转请求失败'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`ESF relay running at http://127.0.0.1:${port}/esf/S120020967Api`)
|
||||
})
|
||||
Reference in New Issue
Block a user