feat(subscription): 对接后端订阅API,移除mock数据
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 订阅 API 模块
|
||||
* 提供订阅管理相关接口(当前使用 mock 数据,后端就绪后切换)
|
||||
* 对接后端订阅管理接口
|
||||
*/
|
||||
import apiClient from './client';
|
||||
|
||||
@@ -66,98 +66,34 @@ export interface ChangePlanResponse {
|
||||
new_subscription?: SubscriptionInfo;
|
||||
}
|
||||
|
||||
// ============ Mock 数据 ============
|
||||
|
||||
const MOCK_SUBSCRIPTION: SubscriptionInfo = {
|
||||
id: 'sub-001',
|
||||
plan_id: 'standard',
|
||||
plan_name: '标准版',
|
||||
status: 'active',
|
||||
billing_cycle: 'monthly',
|
||||
current_period_start: '2026-06-01T00:00:00Z',
|
||||
current_period_end: '2026-07-01T00:00:00Z',
|
||||
amount: 99,
|
||||
auto_renew: true,
|
||||
created_at: '2026-03-01T00:00:00Z',
|
||||
};
|
||||
|
||||
const MOCK_BILLING_RECORDS: BillingRecord[] = [
|
||||
{
|
||||
id: 'bill-001', plan_name: '标准版', amount: 99,
|
||||
billing_cycle: 'monthly', status: 'paid', payment_method: '微信支付',
|
||||
created_at: '2026-06-01T00:00:00Z', invoice_url: '#',
|
||||
},
|
||||
{
|
||||
id: 'bill-002', plan_name: '标准版', amount: 99,
|
||||
billing_cycle: 'monthly', status: 'paid', payment_method: '微信支付',
|
||||
created_at: '2026-05-01T00:00:00Z', invoice_url: '#',
|
||||
},
|
||||
{
|
||||
id: 'bill-003', plan_name: '标准版', amount: 99,
|
||||
billing_cycle: 'monthly', status: 'paid', payment_method: '支付宝',
|
||||
created_at: '2026-04-01T00:00:00Z', invoice_url: '#',
|
||||
},
|
||||
];
|
||||
|
||||
/** 是否使用 mock 数据(后端就绪后改为 false) */
|
||||
const USE_MOCK = true;
|
||||
|
||||
// ============ API 函数 ============
|
||||
|
||||
/** 获取当前订阅信息 */
|
||||
export const getCurrentSubscription = async (): Promise<SubscriptionInfo> => {
|
||||
if (USE_MOCK) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
return MOCK_SUBSCRIPTION;
|
||||
}
|
||||
const response = await apiClient.get('/subscription/current');
|
||||
return response.data;
|
||||
};
|
||||
|
||||
/** 获取账单记录列表 */
|
||||
export const getBillingRecords = async (): Promise<BillingRecord[]> => {
|
||||
if (USE_MOCK) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
return MOCK_BILLING_RECORDS;
|
||||
}
|
||||
const response = await apiClient.get('/subscription/billing');
|
||||
const response = await apiClient.get('/subscription/billing-records');
|
||||
return response.data;
|
||||
};
|
||||
|
||||
/** 升级/降级套餐 */
|
||||
export const changePlan = async (request: ChangePlanRequest): Promise<ChangePlanResponse> => {
|
||||
if (USE_MOCK) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
return {
|
||||
success: true,
|
||||
message: '套餐变更成功',
|
||||
new_subscription: {
|
||||
...MOCK_SUBSCRIPTION,
|
||||
plan_id: request.target_plan_id,
|
||||
plan_name: request.target_plan_id === 'pro' ? '专业版' : request.target_plan_id === 'standard' ? '标准版' : '体验版',
|
||||
},
|
||||
};
|
||||
}
|
||||
const response = await apiClient.post('/subscription/change', request);
|
||||
const response = await apiClient.post('/subscription/change-plan', request);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
/** 取消订阅 */
|
||||
export const cancelSubscription = async (): Promise<{ success: boolean; message: string }> => {
|
||||
if (USE_MOCK) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 800));
|
||||
return { success: true, message: '订阅已取消,当前周期结束后停止服务' };
|
||||
}
|
||||
const response = await apiClient.post('/subscription/cancel');
|
||||
return response.data;
|
||||
};
|
||||
|
||||
/** 切换自动续费 */
|
||||
export const toggleAutoRenew = async (enabled: boolean): Promise<{ success: boolean; message: string }> => {
|
||||
if (USE_MOCK) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
return { success: true, message: enabled ? '已开启自动续费' : '已关闭自动续费' };
|
||||
}
|
||||
const response = await apiClient.post('/subscription/auto-renew', { enabled });
|
||||
const response = await apiClient.post('/subscription/toggle-auto-renew', { enabled });
|
||||
return response.data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user