import { normalizeEarningLedger, normalizeEarningPage, normalizeEarningPageQuery, normalizeEarningSummary, normalizeEarningWithdrawal, normalizeEarningWithdrawalPayload } from './earning-contract.js' import { normalizeResourcePathId } from './request-normalizers.js' import { hasRemoteConfig } from '@/utils/runtime-config.js' import { createRequestError, requestStrict } from './request-client.js' const requireRemoteEarning = (label, operation) => { if (hasRemoteConfig()) return throw createRequestError( `${label}${operation}需要真实服务,当前本地预览不会伪造结果`, operation === '读取' ? 'REMOTE_READ_REQUIRED' : 'REMOTE_WRITE_REQUIRED' ) } export const earningApi = { async getEarningSummary(requestOptions = {}) { requireRemoteEarning('收益汇总', '读取') const earningSummary = await requestStrict({ url: '/genealogy/app/earnings/summary', method: 'GET' }, { requestController: requestOptions.requestController ?? null }) return normalizeEarningSummary(earningSummary) }, async getEarningLedgerPage(query = {}, requestOptions = {}) { requireRemoteEarning('收益明细', '读取') const ledgerPage = await requestStrict({ url: '/genealogy/app/earnings/ledger', method: 'GET', data: normalizeEarningPageQuery(query) }, { requireData: false, requestController: requestOptions.requestController ?? null }) return normalizeEarningPage(ledgerPage, normalizeEarningLedger, '收益明细') }, async getEarningWithdrawalPage(query = {}, requestOptions = {}) { requireRemoteEarning('提现记录', '读取') const withdrawalPage = await requestStrict({ url: '/genealogy/app/earnings/withdrawals', method: 'GET', data: normalizeEarningPageQuery(query) }, { requireData: false, requestController: requestOptions.requestController ?? null }) return normalizeEarningPage(withdrawalPage, normalizeEarningWithdrawal, '提现记录') }, async requestEarningWithdrawal(payload, requestOptions = {}) { requireRemoteEarning('提现申请', '写入') const withdrawal = await requestStrict({ url: '/genealogy/app/earnings/withdrawals', method: 'POST', data: normalizeEarningWithdrawalPayload(payload) }, { requestController: requestOptions.requestController ?? null }) return normalizeEarningWithdrawal(withdrawal) }, async cancelEarningWithdrawal(withdrawalId, requestOptions = {}) { const normalizedWithdrawalId = normalizeResourcePathId(withdrawalId, '提现记录标识') requireRemoteEarning('取消提现', '写入') const withdrawal = await requestStrict({ url: `/genealogy/app/earnings/withdrawals/${normalizedWithdrawalId}/cancel`, method: 'POST' }, { requestController: requestOptions.requestController ?? null }) return normalizeEarningWithdrawal(withdrawal) } }