Files
sub2api/frontend/src/router
feeeei 9d475f9edb feat(settings): 新增「站点类型」三态开关,关闭订阅后用户端收起所有「订阅」入口与文案
后台功能开关页新增「站点类型」单选:充值 & 订阅(默认,即现状)/ 仅充值 / 仅订阅。
它由两个后端开关派生并在保存时同时写回:
- subscription_enabled(新增;默认 true,缺省/空值视为开启,仅显式 false 关闭),按既有
  开关模式贯通常量、公共设置、SSR 注入载荷、默认值、更新写入、DTO、审计 diff 与前端注册表;
- payment_balance_disabled(支付配置里既有的 BALANCE_PAYMENT_DISABLED,此前只能走接口/数据库
  改),补进公共设置与注入载荷,让用户端在任何鉴权请求之前就能推导站点类型。
两者都关闭是接口层可表达但 UI 不提供的组合,前端归为「仅充值」,后台保存时会把余额充值重新打开。

「仅充值」时的表现:
- 用户侧边栏隐藏「我的订阅」,「充值/订阅」入口收窄为「充值」;管理端侧边栏隐藏「订阅管理」
  入口(页面仍可通过地址访问);
- 购买页不再渲染「订阅」tab,只剩单个 tab 时顶部切换器随之隐藏;tab 列表变化时当前 tab
  自动收敛到可用项(关闭订阅回到充值、仅订阅站点重新打开订阅进入订阅),列表为空时展示
  「充值与订阅均暂未开放」而不是一个必定被后端拒绝的充值表单;
- /purchase 的页头标题/描述与 document.title 改走 resolveRouteMetaKeys 按站点类型切换;
- /subscriptions 路由新增 requiresSubscription 守卫,直达时重定向到仪表盘;
- 顶栏订阅进度徽章不再挂载,App.vue、PaymentView 与徽章组件都不再预加载/轮询订阅接口;
- 用量页隐藏「计费类型(余额/订阅)」筛选,Key 用量公开页「订阅类型」行改为「计费方式」。
「仅订阅」时侧边栏入口与页头显示为「订阅」,购买页只剩订阅 tab(余额下单由后端既有逻辑拒绝)。

购买订阅接口、已有订阅的计费、兑换码发放订阅与管理端订阅管理页面本身均不受影响。
featureFlags 抽出纯函数 resolveFeatureFlag,新增 utils/siteBillingMode 承载三态派生与反解,
PaymentView 测试改用响应式 store mock 覆盖开关双向切换。
2026-09-11 00:50:17 +08:00
..

Vue Router Configuration

Overview

This directory contains the Vue Router configuration for the Sub2API frontend application. The router implements a comprehensive navigation system with authentication guards, role-based access control, and lazy loading.

Files

  • index.ts: Main router configuration with route definitions and navigation guards
  • meta.d.ts: TypeScript type definitions for route meta fields

Route Structure

Public Routes (No Authentication Required)

Path Component Description
/login LoginView User login page
/register RegisterView User registration page

User Routes (Authentication Required)

Path Component Description
/ - Redirects to /dashboard
/dashboard DashboardView User dashboard with stats
/keys KeysView API key management
/usage UsageView Usage records and statistics
/redeem RedeemView Redeem code interface
/profile ProfileView User profile settings

Admin Routes (Admin Role Required)

Path Component Description
/admin - Redirects to /admin/dashboard
/admin/dashboard AdminDashboardView Admin dashboard
/admin/users AdminUsersView User management
/admin/groups AdminGroupsView Group management
/admin/accounts AdminAccountsView Account management
/admin/proxies AdminProxiesView Proxy management
/admin/redeem AdminRedeemView Redeem code management

Special Routes

Path Component Description
/:pathMatch(.*) NotFoundView 404 error page

Navigation Guards

Authentication Guard (beforeEach)

The router implements a comprehensive navigation guard that:

  1. Sets Page Title: Updates document title based on route meta
  2. Checks Authentication:
    • Public routes (requiresAuth: false) are accessible without login
    • Protected routes require authentication
    • Redirects to /login if not authenticated
  3. Prevents Double Login:
    • Redirects authenticated users away from login/register pages
  4. Role-Based Access Control:
    • Admin routes (requiresAdmin: true) require admin role
    • Non-admin users are redirected to /dashboard
  5. Preserves Intended Destination:
    • Saves original URL in query parameter for post-login redirect

Flow Diagram

User navigates to route
        ↓
Set page title from meta
        ↓
Is route public? ──Yes──→ Already authenticated? ──Yes──→ Redirect to /dashboard
        ↓ No                                        ↓ No
        ↓                                      Allow access
        ↓
Is user authenticated? ──No──→ Redirect to /login with redirect query
        ↓ Yes
        ↓
Requires admin role? ──Yes──→ Is user admin? ──No──→ Redirect to /dashboard
        ↓ No                                  ↓ Yes
        ↓                                     ↓
Allow access ←────────────────────────────────┘

Route Meta Fields

Each route can define the following meta fields:

interface RouteMeta {
  requiresAuth?: boolean // Default: true (requires authentication)
  requiresAdmin?: boolean // Default: false (admin access only)
  title?: string // Page title
  breadcrumbs?: Array<{
    // Breadcrumb navigation
    label: string
    to?: string
  }>
  icon?: string // Icon for navigation menu
  hideInMenu?: boolean // Hide from navigation menu
}

Lazy Loading

All route components use dynamic imports for code splitting:

component: () => import('@/views/user/DashboardView.vue')

Benefits:

  • Reduced initial bundle size
  • Faster initial page load
  • Components loaded on-demand
  • Automatic code splitting by Vite

Authentication Store Integration

The router integrates with the Pinia auth store (@/stores/auth):

const authStore = useAuthStore()

// Check authentication status
authStore.isAuthenticated

// Check admin role
authStore.isAdmin

Usage Examples

Programmatic Navigation

import { useRouter } from 'vue-router'

const router = useRouter()

// Navigate to a route
router.push('/dashboard')

// Navigate with query parameters
router.push({
  path: '/usage',
  query: { filter: 'today' }
})

// Navigate to admin route (will be blocked if not admin)
router.push('/admin/users')
<template>
  <!-- Simple link -->
  <router-link to="/dashboard">Dashboard</router-link>

  <!-- Named route -->
  <router-link :to="{ name: 'Keys' }">API Keys</router-link>

  <!-- With query parameters -->
  <router-link :to="{ path: '/usage', query: { page: 1 } }"> Usage </router-link>
</template>

Checking Current Route

import { useRoute } from 'vue-router'

const route = useRoute()

// Check if on admin page
const isAdminPage = route.path.startsWith('/admin')

// Get route meta
const requiresAdmin = route.meta.requiresAdmin

Scroll Behavior

The router implements automatic scroll management:

  • Browser Navigation: Restores saved scroll position
  • New Routes: Scrolls to top of page
  • Hash Links: Scrolls to anchor (when implemented)

Error Handling

The router includes error handling for navigation failures:

router.onError((error) => {
  console.error('Router error:', error)
})

Testing Routes

To test navigation guards and route access:

  1. Public Route Access: Visit /login without authentication
  2. Protected Route: Try accessing /dashboard without login (should redirect)
  3. Admin Access: Login as regular user, try /admin/users (should redirect to dashboard)
  4. Admin Success: Login as admin, access /admin/users (should succeed)
  5. 404 Handling: Visit non-existent route (should show 404 page)

Development Tips

Adding New Routes

  1. Add route definition in routes array
  2. Create corresponding view component
  3. Set appropriate meta fields (requiresAuth, requiresAdmin)
  4. Use lazy loading with () => import()
  5. Update this README with route documentation

Debugging Navigation

Enable Vue Router debug mode:

// In browser console
window.__VUE_ROUTER__ = router

// Check current route
router.currentRoute.value

Common Issues

Issue: 404 on page refresh

  • Cause: Server not configured for SPA
  • Solution: Configure server to serve index.html for all routes

Issue: Navigation guard runs twice

  • Cause: Multiple next() calls
  • Solution: Ensure only one next() call per code path

Issue: User data not loaded

  • Cause: Auth store not initialized
  • Solution: Call authStore.checkAuth() in App.vue or main.ts

Security Considerations

  1. Client-Side Only: Navigation guards are client-side; server must also validate
  2. Token Validation: API should verify JWT token on every request
  3. Role Checking: Backend must verify admin role, not just frontend
  4. XSS Protection: Vue automatically escapes template content
  5. CSRF Protection: Use CSRF tokens for state-changing operations

Performance Optimization

  1. Lazy Loading: All routes use dynamic imports
  2. Code Splitting: Vite automatically splits route chunks
  3. Prefetching: Consider adding route prefetch for common paths
  4. Route Caching: Vue Router caches component instances

Future Enhancements

  • Add breadcrumb navigation system
  • Implement route-based permissions beyond admin/user
  • Add route transition animations
  • Implement route prefetching for anticipated navigation
  • Add navigation analytics tracking