feat: 完成前端业务闭环与后端联调
This commit is contained in:
@@ -1,22 +1,26 @@
|
||||
import { hasRemoteConfig } from '@/utils/runtime-config.js'
|
||||
import {
|
||||
assertVipPaymentMethod,
|
||||
normalizeVipCapability,
|
||||
normalizeVipOrders,
|
||||
normalizeVipPackages
|
||||
normalizeVipPackages,
|
||||
normalizeVipPaymentOrder,
|
||||
normalizeVipPaymentStatus
|
||||
} from './vip-contract.js'
|
||||
import {
|
||||
createRequestError,
|
||||
requestStrict
|
||||
} from './request-client.js'
|
||||
|
||||
const requireRemoteVip = (label) => {
|
||||
if (hasRemoteConfig()) return
|
||||
throw createRequestError(`${label}读取需要真实服务,当前本地预览不会伪造结果`, 'REMOTE_READ_REQUIRED')
|
||||
const assertVipId = (value, field) => {
|
||||
const id = value === undefined || value === null ? '' : String(value)
|
||||
if (!/^[1-9]\d*$/.test(id)) {
|
||||
throw createRequestError(`${field}无效`, 'VIP_REQUEST_INVALID')
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
export const vipApi = {
|
||||
async getVipCapability(requestOptions = {}) {
|
||||
requireRemoteVip('VIP 购买能力')
|
||||
const capability = await requestStrict({
|
||||
url: '/genealogy/app/vip/capability',
|
||||
method: 'GET'
|
||||
@@ -27,7 +31,6 @@ export const vipApi = {
|
||||
},
|
||||
|
||||
async getVipPackages(requestOptions = {}) {
|
||||
requireRemoteVip('VIP 套餐')
|
||||
const packages = await requestStrict({
|
||||
url: '/genealogy/app/vip/packages',
|
||||
method: 'GET'
|
||||
@@ -38,7 +41,6 @@ export const vipApi = {
|
||||
},
|
||||
|
||||
async getVipOrders(requestOptions = {}) {
|
||||
requireRemoteVip('VIP 订单')
|
||||
const orders = await requestStrict({
|
||||
url: '/genealogy/app/vip/orders',
|
||||
method: 'GET'
|
||||
@@ -46,5 +48,44 @@ export const vipApi = {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeVipOrders(orders)
|
||||
},
|
||||
|
||||
async createVipOrder(packageId, genealogyId = '', paymentMethod, requestId, requestOptions = {}) {
|
||||
if (typeof requestId !== 'string' || !requestId.trim() || requestId.length > 64) {
|
||||
throw createRequestError('VIP 下单请求号无效', 'VIP_REQUEST_INVALID')
|
||||
}
|
||||
const order = await requestStrict({
|
||||
url: '/genealogy/app/vip/orders',
|
||||
method: 'POST',
|
||||
data: {
|
||||
packageId: assertVipId(packageId, 'VIP 套餐标识'),
|
||||
...(genealogyId ? { genealogyId: assertVipId(genealogyId, '家谱标识') } : {}),
|
||||
paymentMethod: assertVipPaymentMethod(paymentMethod),
|
||||
requestId: requestId.trim()
|
||||
}
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeVipPaymentOrder(order, paymentMethod)
|
||||
},
|
||||
|
||||
async getVipPaymentStatus(transactionId, requestOptions = {}) {
|
||||
const status = await requestStrict({
|
||||
url: `/genealogy/app/vip/orders/${assertVipId(transactionId, '支付流水标识')}/payment`,
|
||||
method: 'GET'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeVipPaymentStatus(status)
|
||||
},
|
||||
|
||||
async closeVipPayment(transactionId, requestOptions = {}) {
|
||||
const status = await requestStrict({
|
||||
url: `/genealogy/app/vip/orders/${assertVipId(transactionId, '支付流水标识')}/close`,
|
||||
method: 'POST'
|
||||
}, {
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return normalizeVipPaymentStatus(status)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user