Prmbr/docs/subscription-pro-detection.md
2025-08-05 22:43:18 +08:00

108 lines
2.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Pro 套餐判定逻辑
## 概述
系统使用统一的价格判定逻辑来确定套餐是否为 Pro 级别,而不是依赖硬编码的套餐 ID。这提供了更大的灵活性允许动态创建不同价格的套餐。
## 判定规则
### Pro 套餐判定
```typescript
function isPlanPro(plan): boolean {
return plan.price > 19
}
```
**规则**: 任何价格超过 $19 的套餐都被视为 Pro 级别
### 套餐层级
```typescript
function getPlanTier(plan): 'free' | 'pro' | 'enterprise' {
if (!plan || plan.price === 0) return 'free'
if (plan.price > 50) return 'enterprise' // 为未来预留
if (plan.price > 19) return 'pro'
return 'free'
}
```
**层级划分**:
- **Free**: $0
- **Basic**: $0.01 - $19.00 (仍被视为免费层级)
- **Pro**: $19.01 - $50.00
- **Enterprise**: $50.01+ (为未来扩展预留)
## 实际应用示例
| 套餐名称 | 价格 | isPro() | getPlanTier() | 说明 |
|---------|------|---------|---------------|------|
| Free | $0 | false | free | 免费套餐 |
| Basic | $9.99 | false | free | 低价套餐,仍为免费层级 |
| Pro | $19.99 | true | pro | 标准 Pro 套餐 |
| Premium | $29.99 | true | pro | 高级 Pro 套餐 |
| Enterprise | $99.99 | true | enterprise | 企业级套餐 |
## 使用方式
### 在组件中使用
```typescript
import { isPlanPro, getPlanTier, getPlanTheme } from '@/lib/subscription-utils'
// 判断是否为 Pro
const isPro = isPlanPro(plan)
// 获取套餐层级
const tier = getPlanTier(plan)
// 获取对应的主题样式
const theme = getPlanTheme(plan)
```
### 在服务端使用
```typescript
import { SubscriptionService } from '@/lib/subscription-service'
// 判断用户是否为 Pro
const isUserPro = await SubscriptionService.isUserPro(userId)
// 判断套餐是否为 Pro
const isPro = SubscriptionService.isPlanPro(plan)
```
## 优势
1. **灵活性**: 可以创建任意价格的套餐,系统自动判定层级
2. **一致性**: 所有 Pro 判定都使用统一的逻辑
3. **可扩展性**: 易于添加新的套餐层级(如 Enterprise
4. **维护性**: 修改判定规则只需要更新一个地方
## 样式主题
系统会根据套餐层级自动应用对应的主题样式:
- **Free**: 灰色主题Star 图标
- **Pro**: 橙色/琥珀色主题Crown 图标
- **Enterprise**: 紫色主题Building 图标
## 注意事项
1. 价格判定基于美元金额
2. 判定逻辑支持部分套餐数据(只需要 `price` 字段)
3. 所有显示逻辑都应该使用这些工具函数,而不是硬编码套餐 ID
4. 修改价格阈值时需要考虑现有套餐的影响
## 迁移指南
如果需要修改 Pro 套餐的价格阈值:
1. 更新 `src/lib/subscription-utils.ts` 中的判定逻辑
2. 运行测试确保所有现有套餐仍然正确分类
3. 更新文档中的示例
```typescript
// 例如,将 Pro 阈值改为 $25
export function isPlanPro(plan: PlanLike): boolean {
if (!plan) return false
return plan.price > 25 // 从 19 改为 25
}
```