Files
soul/app/api/orders/route.ts

68 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 订单管理接口
* 开发: 卡若
* 技术支持: 存客宝
*
* GET /api/orders - 管理后台:返回全部订单(无 userId
* GET /api/orders?userId= - 按用户返回订单
*/
import { type NextRequest, NextResponse } from "next/server"
import { query } from "@/lib/db"
function rowToOrder(row: Record<string, unknown>) {
return {
id: row.id,
orderSn: row.order_sn,
userId: row.user_id,
openId: row.open_id,
productType: row.product_type,
productId: row.product_id,
amount: row.amount,
description: row.description,
status: row.status,
transactionId: row.transaction_id,
payTime: row.pay_time,
createdAt: row.created_at,
updatedAt: row.updated_at,
}
}
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const userId = searchParams.get("userId")
let rows: Record<string, unknown>[] = []
try {
if (userId) {
rows = (await query(
"SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC",
[userId]
)) as Record<string, unknown>[]
} else {
// 管理后台:无 userId 时返回全部订单
rows = (await query(
"SELECT * FROM orders ORDER BY created_at DESC"
)) as Record<string, unknown>[]
}
} catch (e) {
console.error("[Karuo] Orders query error:", e)
// 表可能未初始化,返回空列表
rows = []
}
const orders = rows.map(rowToOrder)
return NextResponse.json({
code: 0,
message: "获取成功",
data: orders,
success: true,
orders,
})
} catch (error) {
console.error("[Karuo] Get orders error:", error)
return NextResponse.json({ code: 500, message: "服务器错误" }, { status: 500 })
}
}