gbmake-payload/src/app/api/admin/taobao/sync-product/route.ts

56 lines
1.7 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.

import { getPayload } from 'payload'
import config from '@payload-config'
import { NextResponse } from 'next/server'
import { syncProductTaobaoLinks } from '@/lib/taobao'
import { addCorsHeaders, handleCorsOptions } from '@/lib/cors'
export async function OPTIONS(request: Request) {
return handleCorsOptions(request.headers.get('origin'))
}
/**
* POST /api/admin/taobao/sync-product
* 为指定产品解析淘宝链接并回填 title / thumbnail / price
*
* Body: {
* productId: string
* collection: 'products' | 'preorder-products'
* force?: boolean // true = 覆盖已有字段false (默认) = 只填充空字段
* }
*/
export async function POST(request: Request) {
const origin = request.headers.get('origin')
try {
const { productId, collection, force = false } = await request.json()
if (!productId || !collection) {
return addCorsHeaders(
NextResponse.json({ success: false, error: 'productId 和 collection 必填' }, { status: 400 }),
origin,
)
}
if (!['products', 'preorder-products'].includes(collection)) {
return addCorsHeaders(
NextResponse.json({ success: false, error: '无效的 collection' }, { status: 400 }),
origin,
)
}
const payload = await getPayload({ config })
const result = await syncProductTaobaoLinks(payload, productId, collection, force)
return addCorsHeaders(
NextResponse.json({ success: true, ...result }),
origin,
)
} catch (err: any) {
console.error('[taobao/sync-product]', err)
return addCorsHeaders(
NextResponse.json({ success: false, error: err?.message ?? 'Unknown error' }, { status: 500 }),
origin,
)
}
}