Files
soul-yongping/开发文档/8、部署/佣金问题-快速诊断和修复.md

233 lines
5.0 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: 检查管理后台配置
1. 登录管理后台:`https://soul.quwanzhi.com/admin`
2. 进入「推广设置」页面:`/admin/referral-settings`
3. 查看「分销比例」输入框中的数值
**如果显示 10** → 配置错误,应该改为 **90**
**如果显示 90** → 配置正确,问题在其他地方
---
### 方法2: 检查实际佣金
1. 找一个推荐关系的订单
2. 查看推荐人获得的佣金
**示例**:
- 用户B购买1元商品无折扣
- 推荐人A应得0.90元90%
- 如果实际只得0.10元 → 说明比例算反了
---
### 方法3: 检查小程序显示
打开小程序「分销中心」,查看推广规则:
**应该显示**:
```
好友成功付款后,你获得 90% 收益
```
**如果显示**:
```
好友成功付款后,你获得 10% 收益
```
→ 说明后端返回的 `shareRate` 值错误
---
## 🔧 修复方案
### 修复1: 如果管理后台配置值错误
**步骤**:
1. 进入管理后台 `/admin/referral-settings`
2. 将「分销比例」改为 **90**
3. 点击「保存配置」
4. 刷新小程序验证
---
### 修复2: 如果数据库配置值错误
**手动修复SQL**:
```sql
-- 1. 查看当前配置
SELECT config_value FROM system_config WHERE config_key = 'referral_config';
-- 2. 如果 distributorShare 不是 90手动更新
UPDATE system_config
SET config_value = JSON_SET(
config_value,
'$.distributorShare',
90
)
WHERE config_key = 'referral_config';
-- 3. 验证修改
SELECT config_value FROM system_config WHERE config_key = 'referral_config';
```
---
### 修复3: 如果计算公式错误
**检查文件**: `app/api/miniprogram/pay/notify/route.ts`
**第395行当前代码应该是**:
```typescript
const commission = Math.round(amount * distributorShare * 100) / 100
```
**如果错误写成了**:
```typescript
// ❌ 错误1算反了
const commission = Math.round(amount * (1 - distributorShare) * 100) / 100
// ❌ 错误2没有先除100
const distributorShare = config.distributorShare // 90没除100
const commission = amount * distributorShare / 100 // 1 * 90 / 100 = 0.9(看似对,但后续会错)
```
---
## 🧪 验证步骤
### 验证1: 手动计算
假设配置 `distributorShare = 90`:
```javascript
// 读取配置
const configValue = 90
// 转换为小数
const distributorShare = configValue / 100 // = 0.9
// 计算佣金购买1元
const commission = 1.00 * 0.9 // = 0.90元
// 返回给小程序
const shareRate = distributorShare * 100 // = 90
```
**预期**:
- 购买1元 → 推荐人得 0.90元
- 小程序显示90% 返利
---
### 验证2: 查看实际订单
**SQL查询**:
```sql
SELECT
o.order_sn,
o.amount as 订单金额,
rb.total_commission as 累计佣金,
rb.purchase_count as 购买次数,
o.amount * 0.9 as 预期佣金90percent,
o.amount * 0.1 as 如果是10percent
FROM orders o
JOIN referral_bindings rb ON o.user_id = rb.referee_id
WHERE o.status = 'paid'
AND rb.purchase_count > 0
ORDER BY o.pay_time DESC
LIMIT 5;
```
**对比**:
- 如果 `total_commission ≈ 预期佣金90percent` → 计算正确
- 如果 `total_commission ≈ 如果是10percent` → 计算错误(算反了)
---
## 🔍 代码审查
### 关键代码1: 读取配置
**文件**: `app/api/miniprogram/pay/notify/route.ts` 第357-360行
```typescript
const config = await getConfig('referral_config')
if (config?.distributorShare) {
distributorShare = config.distributorShare / 100 // ✅ 应该是这样
}
```
**如果错误写成**:
```typescript
distributorShare = config.distributorShare // ❌ 没除100
```
---
### 关键代码2: 计算佣金
**文件**: `app/api/miniprogram/pay/notify/route.ts` 第395行
```typescript
const commission = Math.round(amount * distributorShare * 100) / 100
// ✅ 正确1 * 0.9 = 0.9
```
**如果错误写成**:
```typescript
const commission = Math.round(amount * (1 - distributorShare) * 100) / 100
// ❌ 错误1 * (1 - 0.9) = 0.1(算反了)
```
---
### 关键代码3: 返回比例
**文件**: `app/api/referral/data/route.ts` 第198行
```typescript
shareRate: Math.round(distributorShare * 100)
// ✅ 正确0.9 * 100 = 90
```
---
## 🚀 立即检查
请你帮我确认一下:
### 问题1: 管理后台的配置值
进入 `https://soul.quwanzhi.com/admin/referral-settings`,看看「分销比例」输入框中显示的是:
- [ ] 90正确
- [ ] 10错误
- [ ] 0.9(错误)
### 问题2: 小程序显示的比例
打开小程序「分销中心」,查看推广规则显示的是:
- [ ] "你获得 90% 收益"(正确)
- [ ] "你获得 10% 收益"(错误)
### 问题3: 实际佣金金额
如果有测试订单,查看:
- 购买金额1.00元
- 推荐人获得_____ 元
**如果是 0.90元** → 计算正确
**如果是 0.10元** → 计算错误
---
**请告诉我上述三个问题的实际情况,我会立即定位并修复!**