import type { SubscriptionStatus } from '~/types' /** * 订阅状态校验 * 通过 /api/subscription/status 查询当前租户应用的订阅状态 */ export function useSubscription() { const status = useState('subscription-status', () => null) const loading = useState('subscription-loading', () => false) const expired = computed(() => { if (!status.value) return false return status.value.expired === true || status.value.status === 'expired' }) async function fetchStatus() { loading.value = true try { const res = await $fetch('/api/subscription/status') status.value = res return res } catch { // 接口异常时默认允许访问 status.value = { subscribed: true, status: 'active', expired: false } return status.value } finally { loading.value = false } } /** 检查是否过期,过期则跳转续费页 */ async function checkAndRedirect() { await fetchStatus() if (expired.value) { await navigateTo('/renewal') } } return { status, loading, expired, fetchStatus, checkAndRedirect } }