feat: 套餐权益列表从后台UserTypeConfig动态读取

- 权益列表从数据库配置读取
- 支持按用户类型显示/状态
- 更新各等级用户权益配置
This commit is contained in:
2026-04-14 18:42:21 +08:00
parent ee5e672901
commit 174801c4a3
2 changed files with 68 additions and 32 deletions

82
app.py
View File

@@ -209,37 +209,71 @@ def pricing():
"""会员定价页""" """会员定价页"""
user = get_current_user() user = get_current_user()
# 权益名称映射
feature_names = {
'basic_translate': '基础翻译功能',
'history': '翻译历史记录',
'retranslate': '不满意重新翻译',
'export_pdf': '导出PDF格式',
'compare_view': '原文译文对比查看',
'batch_translate': '批量翻译',
'custom_terms': '自定义术语库',
'priority_queue': '优先处理队列',
'custom_instruction': '自定义翻译要求',
'api_access': 'API接口调用',
}
# 从数据库读取动态配置的会员套餐 # 从数据库读取动态配置的会员套餐
db_plans = MembershipPlanConfig.query.filter_by(is_active=True)\ db_plans = MembershipPlanConfig.query.filter_by(is_active=True)\
.order_by(MembershipPlanConfig.sort_order).all() .order_by(MembershipPlanConfig.sort_order).all()
# 如果数据库没有配置,使用默认配置 # 读取用户类型配置获取权益
if not db_plans: user_types = UserTypeConfig.query.filter_by(is_active=True).all()
plans = MEMBERSHIP_PLANS user_type_map = {ut.type_key: ut for ut in user_types}
else:
# 转换为字典格式供模板使用 # 为每个套餐添加权益列表
plans = {} plan_features = {}
for plan in db_plans: for plan in db_plans:
plans[plan.plan_key] = { ut = user_type_map.get(plan.user_type_key)
'name': plan.display_name, if ut:
'price': plan.price, features = ut.get_features()
'original_price': plan.original_price, plan_features[plan.plan_key] = [
'period': plan.period, {'key': f, 'name': feature_names.get(f, f), 'has': True}
'period_days': plan.period_days, for f in features
'description': plan.description, ]
'is_recommended': plan.is_recommended, # 添加限制信息
'user_type_key': plan.user_type_key, plan_features[plan.plan_key].insert(0, {
} 'key': 'daily_translations',
'name': f"每日翻译{ut.daily_translations if ut.daily_translations > 0 else '无限'}",
'has': True
})
plan_features[plan.plan_key].insert(1, {
'key': 'max_pages',
'name': f"单文件最大{ut.max_pages if ut.max_pages > 0 else '无限'}",
'has': True
})
# 添加免费用户套餐(固定) # 免费用户权益
plans['free'] = { free_ut = user_type_map.get('free')
'name': '免费用户', free_features = []
'price': 0, if free_ut:
'period': 'forever', free_features = [
'description': '基础翻译功能', {'key': 'daily_translations', 'name': f"每日翻译{free_ut.daily_translations}", 'has': True},
} {'key': 'max_pages', 'name': f"单文件最大{free_ut.max_pages}", 'has': True},
]
for f in free_ut.get_features():
free_features.append({'key': f, 'name': feature_names.get(f, f), 'has': True})
# 添加没有的功能
all_features = ['compare_view', 'batch_translate', 'custom_terms']
for f in all_features:
if f not in free_ut.get_features():
free_features.append({'key': f, 'name': feature_names.get(f, f), 'has': False})
return render_template('pricing.html', plans=plans, user=user, db_plans=db_plans) return render_template('pricing.html',
plans=db_plans,
plan_features=plan_features,
free_features=free_features,
user=user)
@app.route('/profile') @app.route('/profile')

View File

@@ -49,13 +49,9 @@
<div class="price my-3">¥0<small>/永久</small></div> <div class="price my-3">¥0<small>/永久</small></div>
<ul class="features text-start"> <ul class="features text-start">
<li>✅ 每日翻译10次</li> {% for feat in free_features %}
<li>✅ 单文件最大50页</li> <li>{% if feat.has %}✅{% else %}<span class="text-muted"></span>{% endif %} {{ feat.name }}</li>
<li>✅ 翻译历史记录</li> {% endfor %}
<li>✅ 不满意重新翻译</li>
<li>✅ 导出PDF格式</li>
<li class="text-muted">❌ 原文译文对比</li>
<li class="text-muted">❌ 批量翻译</li>
</ul> </ul>
{% if user and user.user_type in ['free', 'vip_basic', 'vip_pro', 'vip_enterprise', 'admin'] %} {% if user and user.user_type in ['free', 'vip_basic', 'vip_pro', 'vip_enterprise', 'admin'] %}
@@ -89,6 +85,12 @@
<p class="text-muted">{{ plan.description }}</p> <p class="text-muted">{{ plan.description }}</p>
<ul class="features text-start">
{% for feat in plan_features.get(plan.plan_key, []) %}
<li>✅ {{ feat.name }}</li>
{% endfor %}
</ul>
{% if user and user.user_type == plan.user_type_key %} {% if user and user.user_type == plan.user_type_key %}
<span class="btn btn-success w-100 mt-3 disabled">当前套餐</span> <span class="btn btn-success w-100 mt-3 disabled">当前套餐</span>
{% elif user and user.user_type in ['vip_enterprise', 'admin'] %} {% elif user and user.user_type in ['vip_enterprise', 'admin'] %}