优惠码Bug修复

This commit is contained in:
2025-11-07 07:53:07 +08:00
parent dcc88251df
commit 83c6abdfba
2 changed files with 200 additions and 36 deletions

28
app.py
View File

@@ -706,11 +706,38 @@ class SubscriptionPlan(db.Model):
monthly_price = db.Column(db.Numeric(10, 2), nullable=False)
yearly_price = db.Column(db.Numeric(10, 2), nullable=False)
features = db.Column(db.Text, nullable=True)
pricing_options = db.Column(db.Text, nullable=True) # JSON格式[{"months": 1, "price": 99}, {"months": 12, "price": 999}]
is_active = db.Column(db.Boolean, default=True)
sort_order = db.Column(db.Integer, default=0)
created_at = db.Column(db.DateTime, default=beijing_now)
def to_dict(self):
# 解析pricing_options如果存在
pricing_opts = None
if self.pricing_options:
try:
pricing_opts = json.loads(self.pricing_options)
except:
pricing_opts = None
# 如果没有pricing_options则从monthly_price和yearly_price生成默认选项
if not pricing_opts:
pricing_opts = [
{
'months': 1,
'price': float(self.monthly_price) if self.monthly_price else 0,
'label': '月付',
'cycle_key': 'monthly'
},
{
'months': 12,
'price': float(self.yearly_price) if self.yearly_price else 0,
'label': '年付',
'cycle_key': 'yearly',
'discount_percent': 20 # 年付默认20%折扣
}
]
return {
'id': self.id,
'name': self.name,
@@ -718,6 +745,7 @@ class SubscriptionPlan(db.Model):
'description': self.description,
'monthly_price': float(self.monthly_price) if self.monthly_price else 0,
'yearly_price': float(self.yearly_price) if self.yearly_price else 0,
'pricing_options': pricing_opts, # 新增:灵活计费周期选项
'features': json.loads(self.features) if self.features else [],
'is_active': self.is_active,
'sort_order': self.sort_order