feat: 基本实现拆单收货
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import 'dotenv/config'
|
||||
import Koa from 'koa'
|
||||
import Router from '@koa/router'
|
||||
import bodyParser from 'koa-bodyparser'
|
||||
import type { FunctionHandler } from './utils/handler.js'
|
||||
import cartAdd from './functions/cart-add.js'
|
||||
import login from './functions/login.js'
|
||||
import orderCancelMain from './functions/order-cancel-main.js'
|
||||
import orderCancelPaidMain from './functions/order-cancel-paid-main.js'
|
||||
import orderCancelSub from './functions/order-cancel-sub.js'
|
||||
import orderCreate from './functions/order-create.js'
|
||||
import orderList from './functions/order-list.js'
|
||||
import orderPay from './functions/order-pay.js'
|
||||
import orderPushEvent from './functions/order-push-event.js'
|
||||
import productDetail from './functions/product-detail.js'
|
||||
import productList from './functions/product-list.js'
|
||||
import supplierDebugTokens from './functions/supplier-debug-tokens.js'
|
||||
import supplierInfo from './functions/supplier-info.js'
|
||||
import supplierOrderInfo from './functions/supplier-order-info.js'
|
||||
import supplierPoints from './functions/supplier-points.js'
|
||||
import supplierToken from './functions/supplier-token.js'
|
||||
import upload from './functions/upload.js'
|
||||
import userInfo from './functions/user-info.js'
|
||||
|
||||
const app = new Koa()
|
||||
const router = new Router()
|
||||
const port = Number(process.env.PORT || 8888)
|
||||
|
||||
const handlers: Record<string, FunctionHandler> = {
|
||||
'cart-add': cartAdd,
|
||||
login,
|
||||
'order-cancel-main': orderCancelMain,
|
||||
'order-cancel-paid-main': orderCancelPaidMain,
|
||||
'order-cancel-sub': orderCancelSub,
|
||||
'order-create': orderCreate,
|
||||
'order-list': orderList,
|
||||
'order-pay': orderPay,
|
||||
'order-push-event': orderPushEvent,
|
||||
'product-detail': productDetail,
|
||||
'product-list': productList,
|
||||
'supplier-debug-tokens': supplierDebugTokens,
|
||||
'supplier-info': supplierInfo,
|
||||
'supplier-order-info': supplierOrderInfo,
|
||||
'supplier-points': supplierPoints,
|
||||
'supplier-token': supplierToken,
|
||||
upload,
|
||||
'user-info': userInfo
|
||||
}
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS'
|
||||
}
|
||||
|
||||
const requestBody = (ctx: Koa.Context) => {
|
||||
if (ctx.method === 'GET' || ctx.method === 'HEAD') {
|
||||
return undefined
|
||||
}
|
||||
return JSON.stringify(ctx.request.body || {})
|
||||
}
|
||||
|
||||
const toRequest = (ctx: Koa.Context) =>
|
||||
new Request(`${ctx.protocol}://${ctx.host}${ctx.originalUrl}`, {
|
||||
method: ctx.method,
|
||||
headers: ctx.headers as HeadersInit,
|
||||
body: requestBody(ctx)
|
||||
})
|
||||
|
||||
const writeResponse = async (ctx: Koa.Context, response: Response) => {
|
||||
ctx.status = response.status
|
||||
Object.entries(corsHeaders).forEach(([key, value]) => ctx.set(key, value))
|
||||
response.headers.forEach((value, key) => ctx.set(key, value))
|
||||
ctx.body = response.body ? await response.text() : undefined
|
||||
}
|
||||
|
||||
const dispatch = (name: string) => async (ctx: Koa.Context) => {
|
||||
const handler = handlers[name]
|
||||
if (!handler) {
|
||||
ctx.status = 404
|
||||
ctx.body = { success: false, error: { code: 'NOT_FOUND', message: '接口不存在' } }
|
||||
return
|
||||
}
|
||||
const response = await handler(toRequest(ctx), {})
|
||||
await writeResponse(ctx, response)
|
||||
}
|
||||
|
||||
app.use(bodyParser())
|
||||
app.use(async (ctx, next) => {
|
||||
Object.entries(corsHeaders).forEach(([key, value]) => ctx.set(key, value))
|
||||
if (ctx.method === 'OPTIONS') {
|
||||
ctx.status = 204
|
||||
return
|
||||
}
|
||||
await next()
|
||||
})
|
||||
|
||||
router.all('/api/:name', async ctx => dispatch(ctx.params.name)(ctx))
|
||||
router.post('/supplierOpenApi/pointOrder/queryOrderInfo', dispatch('supplier-order-info'))
|
||||
router.post('/supplierOpenApi/debug/tokens', dispatch('supplier-debug-tokens'))
|
||||
|
||||
app.use(router.routes())
|
||||
app.use(router.allowedMethods())
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`supplied-service Koa server running at http://localhost:${port}`)
|
||||
})
|
||||
Reference in New Issue
Block a user