From 68cccbb822a58144f133986757fd83857ee57918 Mon Sep 17 00:00:00 2001 From: zyh Date: Tue, 22 Oct 2024 10:09:42 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=AE=A2=E9=98=85API=E4=BB=A5=E8=8E=B7=E5=8F=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=AE=A2=E9=98=85=E4=BF=A1=E6=81=AF=E5=92=8C=E4=BD=99?= =?UTF-8?q?=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/routes/api.user-subscription.ts | 38 +++++++++++++------ ...2180600_create_user_subscriptions_table.js | 21 ++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 db/migrations/20241022180600_create_user_subscriptions_table.js diff --git a/app/routes/api.user-subscription.ts b/app/routes/api.user-subscription.ts index 8910f5d..84f67dd 100644 --- a/app/routes/api.user-subscription.ts +++ b/app/routes/api.user-subscription.ts @@ -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 }); } } diff --git a/db/migrations/20241022180600_create_user_subscriptions_table.js b/db/migrations/20241022180600_create_user_subscriptions_table.js new file mode 100644 index 0000000..eedc853 --- /dev/null +++ b/db/migrations/20241022180600_create_user_subscriptions_table.js @@ -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'); +}