Files
points-mall/supplied-service/netlify/functions/supplier-points.ts
T

63 lines
1.7 KiB
TypeScript

import type { Context } from '@netlify/functions'
import { guardMethod } from '../utils/method.js'
import { fail, ok } from '../utils/http.js'
import { readJson } from '../utils/request.js'
import { withErrorBoundary } from '../utils/handler.js'
type SupplierPointsRequest = {
supplierId?: string
token?: string
userId?: string
}
type BridgePointsResponse = {
resultCode?: number
message?: string
userPoints?: number | string
}
const normalizeBase = (base: string) => base.replace(/\/$/, '')
export default withErrorBoundary(async (request: Request, _context: Context) => {
const methodResponse = guardMethod(request, ['POST'])
if (methodResponse) return methodResponse
const body = await readJson<SupplierPointsRequest>(request)
const supplierId = String(body.supplierId || '').trim()
const token = String(body.token || '').trim()
const userId = String(body.userId || '').trim()
if (!supplierId || !token || !userId) {
return fail('供应商、用户或授权信息不能为空')
}
const apiBase = normalizeBase(process.env.POINTS_MALL_OPEN_API_BASE || '')
if (!apiBase) {
return fail('平台积分商城接口地址未配置', 500, 'OPEN_API_BASE_MISSING')
}
const response = await fetch(`${apiBase}/pointOrder/queryPoints`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
token
},
body: JSON.stringify({
supplierId,
token,
userId
})
})
const pointsPayload = (await response.json().catch(() => ({}))) as BridgePointsResponse
if (!response.ok || pointsPayload.resultCode !== 0) {
return fail(pointsPayload.message || '查询用户积分失败', 502, 'BRIDGE_POINTS_FAILED')
}
return ok({
userPoints: Number(pointsPayload.userPoints || 0)
})
})