Files
soul-yongping/开发文档/8、部署/佣金计算逻辑检查.md

308 lines
5.6 KiB
Markdown
Raw 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.

# 佣金计算逻辑检查
## 🔍 用户反馈
**问题**: "推广者应该获取支付金额的90%但却是10%"
---
## 📊 配置值流转
### 1. 管理后台保存(/admin/referral-settings
**输入**:
```
分销比例90 (表示90%)
```
**保存代码**:
```typescript
const safeConfig = {
distributorShare: Number(config.distributorShare) || 0
}
// 保存到数据库distributorShare = 90
```
**数据库存储**:
```json
{
"distributorShare": 90
}
```
---
### 2. 后端读取配置(/api/miniprogram/pay/notify
**读取代码**:
```typescript
const config = await getConfig('referral_config')
const distributorShare = config.distributorShare / 100
// 结果90 / 100 = 0.9
```
**佣金计算**:
```typescript
const commission = Math.round(amount * distributorShare * 100) / 100
// 例如1元 * 0.9 = 0.9元
```
---
### 3. 返回给小程序(/api/referral/data
**返回代码**:
```typescript
shareRate: Math.round(distributorShare * 100)
// 结果0.9 * 100 = 90
```
**小程序显示**:
```xml
你获得 {{shareRate}}% 收益
<!-- 显示:你获得 90% 收益 -->
```
---
## ⚠️ 可能的问题点
### 问题1: 配置值保存错误
**检查点**:
- 管理后台输入的是 90 还是 0.9
- 数据库实际保存的值是多少?
**验证SQL**:
```sql
SELECT config_value FROM system_config WHERE config_key = 'referral_config';
```
**预期结果**:
```json
{
"distributorShare": 90
}
```
**如果看到**:
```json
{
"distributorShare": 0.1 // ❌ 错误!应该是 90
}
```
---
### 问题2: 计算公式错误
**检查点**: 是否有地方用错了公式?
**错误示例**:
```typescript
// ❌ 错误:用了减法
const commission = amount * (1 - distributorShare)
// 1 * (1 - 0.9) = 0.1 元10%
// ✅ 正确:直接乘
const commission = amount * distributorShare
// 1 * 0.9 = 0.9 元90%
```
---
### 问题3: 除以100的位置错误
**错误示例**:
```typescript
// ❌ 错误没有除以100
const distributorShare = config.distributorShare
const commission = amount * distributorShare / 100
// 1 * 90 / 100 = 0.9 元(看起来对,但下一步就错了)
```
**正确方式**:
```typescript
// ✅ 正确先除以100
const distributorShare = config.distributorShare / 100 // 90 → 0.9
const commission = amount * distributorShare // 1 * 0.9 = 0.9
```
---
## 🧪 测试用例
### 测试1: 购买1元无折扣
**输入**:
- 支付金额: 1.00元
- distributorShare: 90
**计算过程**:
```typescript
const distributorShare = 90 / 100 = 0.9
const commission = 1.00 * 0.9 = 0.90
```
**预期结果**: 推荐人获得 0.90元
---
### 测试2: 购买1元5%折扣)
**输入**:
- 原价: 1.00元
- 好友优惠: 5%
- 实付: 0.95元
- distributorShare: 90
**计算过程**:
```typescript
const finalAmount = 1.00 * (1 - 0.05) = 0.95
const commission = 0.95 * 0.9 = 0.855 0.86
```
**预期结果**: 推荐人获得 0.86元
---
### 测试3: 如果配置错误保存为0.9
**输入**:
- 支付金额: 1.00元
- distributorShare: 0.9 (❌ 错误的保存值)
**计算过程**:
```typescript
const distributorShare = 0.9 / 100 = 0.009
const commission = 1.00 * 0.009 = 0.009 0.01
```
**错误结果**: 推荐人只获得 0.01元1%)❌
---
## 🔍 排查步骤
### 步骤1: 检查数据库配置值
**SQL查询**:
```sql
SELECT config_key, config_value
FROM system_config
WHERE config_key = 'referral_config';
```
**检查要点**:
- `distributorShare` 应该是 **90**(不是 0.9
- 如果是其他值(如 10说明保存时出错了
---
### 步骤2: 检查实际佣金记录
**SQL查询**:
```sql
SELECT
rb.referrer_id,
rb.referee_id,
rb.purchase_count,
rb.total_commission,
o.amount,
o.order_sn
FROM referral_bindings rb
JOIN orders o ON o.user_id = rb.referee_id AND o.status = 'paid'
WHERE rb.purchase_count > 0
ORDER BY rb.last_purchase_date DESC
LIMIT 5;
```
**检查要点**:
- 订单金额 1.00元 → 佣金应该约 0.90元
- 如果佣金是 0.10元,说明计算错误
---
### 步骤3: 检查控制台日志
**查看PM2日志**:
```bash
pm2 logs soul --lines 100 | grep "处理分佣"
```
**预期输出**:
```
[PayNotify] 处理分佣: {
amount: 0.95,
commission: 0.855,
shareRate: '90%'
}
```
**如果看到**:
```
shareRate: '10%' // ❌ 错误!
```
---
## 🔧 可能的修复方案
### 修复1: 如果配置值错误
**检查数据库**:
```sql
SELECT config_value FROM system_config WHERE config_key = 'referral_config';
```
**如果显示**:
```json
{"distributorShare": 10} // ❌ 错误
```
**手动修复**:
```sql
UPDATE system_config
SET config_value = '{"distributorShare":90,"minWithdrawAmount":10,"bindingDays":30,"userDiscount":5,"enableAutoWithdraw":false}'
WHERE config_key = 'referral_config';
```
**或者在管理后台重新保存** 90%。
---
### 修复2: 如果计算公式错误
**检查位置**: `app/api/miniprogram/pay/notify/route.ts` 第395行
**当前代码**:
```typescript
const commission = Math.round(amount * distributorShare * 100) / 100
```
**验证**:
- 如果 distributorShare = 0.9commission = 0.9元 ✅
- 如果 distributorShare = 0.009commission = 0.009元 ❌
---
## 📝 诊断建议
请提供以下信息以便诊断:
1. **管理后台显示的值**:
- 进入 `/admin/referral-settings`
- 查看"分销比例"输入框中的值是多少?
2. **实际佣金金额**:
- 用户A购买1元商品
- 推荐人B实际获得多少佣金
3. **小程序显示的比例**:
- 分销中心显示的是"你获得 xx% 收益"
- 这个 xx 是多少?
---
**根据你的反馈,我会立即定位并修复问题!**