fix: pricing_display 不乘unit_val + 添加可读display_text字段

1. 新格式 unit_prices 已是展示价,不再乘以 unit_val
2. tiered 定价同步修复
3. 返回增加 display_text 字段,格式如官网价格表
This commit is contained in:
yumoqing 2026-06-05 18:26:30 +08:00
parent 2706815dee
commit 3eefe1a67f

View File

@ -842,12 +842,8 @@ async def get_pricing_display(ppid):
fdef = fields.get(factor_name, {}) fdef = fields.get(factor_name, {})
factor_label = fdef.get('label', factor_name) if isinstance(fdef, dict) else factor_name factor_label = fdef.get('label', factor_name) if isinstance(fdef, dict) else factor_name
# 单位换算unit_price 存储的是原始单价(如 4e-06 元/token # 新格式 unit_prices 已是展示价(如 6.0 元/百万),无需再乘 unit_val
# 展示时需要乘以单位值(如 百万=1000000得到人类可读价格如 4 元/百万token display_price = unit_price
unit_val = unit_values.get(unit_str, 1)
if isinstance(unit_val, str):
unit_val = float(unit_val)
display_price = unit_price * unit_val
# 构建 unit_label (元/单位) # 构建 unit_label (元/单位)
unit_label = f"元/{unit_str}" if unit_str else "" unit_label = f"元/{unit_str}" if unit_str else ""
@ -869,7 +865,7 @@ async def get_pricing_display(ppid):
fi_copy.pop('unit_prices', None) fi_copy.pop('unit_prices', None)
tiered_pricing.append({ tiered_pricing.append({
'filters': fi_copy, 'filters': fi_copy,
'unit_prices': raw_tier_price * unit_val 'unit_prices': raw_tier_price # 新格式已是展示价
}) })
if tiered_pricing: if tiered_pricing:
price_factors_display[0]['tiered'] = tiered_pricing price_factors_display[0]['tiered'] = tiered_pricing
@ -907,11 +903,31 @@ async def get_pricing_display(ppid):
'formula': p.get('formula', '') 'formula': p.get('formula', '')
}) })
# 生成可读价格表
display_lines = [f"{getattr(r, 'name', '')}】定价:"]
for item in items:
prefix = ''
if item.get('filter_labels'):
labels = ', '.join(f"{k}: {v}" for k, v in item['filter_labels'].items())
prefix = f" ({labels})"
for pf in item.get('price_factors', []):
label = pf.get('label', pf.get('factor', ''))
up = pf.get('unit_price')
ul = pf.get('unit_label', '')
if up is not None:
display_lines.append(f" - {label}: {up} {ul}{prefix}")
if pf.get('tiered'):
for t in pf['tiered']:
t_filters = ', '.join(f"{k}={v}" for k, v in t.get('filters', {}).items())
t_price = t.get('unit_prices', '')
display_lines.append(f" · {t_filters}: {t_price} {ul}")
return { return {
'ppid': ppid, 'ppid': ppid,
'name': getattr(r, 'name', ''), 'name': getattr(r, 'name', ''),
'pricing_type': pricing_type, 'pricing_type': pricing_type,
'items': items 'items': items,
'display_text': '\n'.join(display_lines)
} }