mirror of
https://github.com/stackblitz/bolt.new
synced 2025-06-26 18:17:50 +00:00
feat: 更新用户订阅API以获取用户订阅信息和余额
This commit is contained in:
parent
4064317a1a
commit
68cccbb822
@ -11,20 +11,34 @@ export async function loader({ request }: { request: Request }) {
|
||||
}
|
||||
|
||||
try {
|
||||
const userSubscription = await db.select(
|
||||
'subscription_plans.*',
|
||||
'user_transactions.tokens as tokensLeft',
|
||||
db.raw('DATE_ADD(user_transactions._create, INTERVAL 1 MONTH) as nextReloadDate')
|
||||
)
|
||||
.from('user_transactions')
|
||||
.join('subscription_plans', 'user_transactions.plan_id', 'subscription_plans._id')
|
||||
.where('user_transactions.user_id', userId)
|
||||
.orderBy('user_transactions._create', 'desc')
|
||||
.first();
|
||||
const user = await db('users')
|
||||
.select('token_balance')
|
||||
.where('_id', userId)
|
||||
.first();
|
||||
|
||||
return json(userSubscription);
|
||||
const subscription = await db('user_subscriptions')
|
||||
.where('user_id', userId)
|
||||
.where('expiration_date', '>', db.fn.now())
|
||||
.orderBy('expiration_date', 'desc')
|
||||
.first();
|
||||
|
||||
const subscriptionPlan = subscription
|
||||
? await db('subscription_plans')
|
||||
.where('_id', subscription.plan_id)
|
||||
.first()
|
||||
: null;
|
||||
|
||||
return json({
|
||||
tokenBalance: user.token_balance,
|
||||
subscription: subscription
|
||||
? {
|
||||
planName: subscriptionPlan.name,
|
||||
expirationDate: subscription.expiration_date,
|
||||
}
|
||||
: null,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching user subscription:', error);
|
||||
return json({ error: 'Failed to fetch user subscription' }, { status: 500 });
|
||||
return json({ error: '获取用户订阅信息失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
export function up(knex) {
|
||||
return knex.schema.createTable('user_subscriptions', function(table) {
|
||||
table.increments('_id').primary().comment('主键ID');
|
||||
table.integer('user_id').unsigned().notNullable().comment('用户ID');
|
||||
table.foreign('user_id').references('users._id').onDelete('CASCADE');
|
||||
table.integer('plan_id').unsigned().notNullable().comment('订阅计划ID');
|
||||
table.foreign('plan_id').references('subscription_plans._id');
|
||||
table.date('start_date').notNullable().comment('订阅开始日期');
|
||||
table.date('expiration_date').notNullable().comment('订阅过期日期');
|
||||
table.enum('status', ['active', 'expired', 'cancelled']).notNullable().comment('订阅状态');
|
||||
table.timestamp('_create').defaultTo(knex.fn.now()).comment('创建时间');
|
||||
table.timestamp('_update').defaultTo(knex.fn.now()).comment('更新时间');
|
||||
table.index('user_id');
|
||||
table.index('plan_id');
|
||||
table.index('expiration_date');
|
||||
});
|
||||
}
|
||||
|
||||
export function down(knex) {
|
||||
return knex.schema.dropTable('user_subscriptions');
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user