Files
soul-yongping/next-project/scripts/test-referral-config.js
2026-02-09 14:43:35 +08:00

146 lines
4.7 KiB
JavaScript
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.

/**
* 测试推广配置读取和佣金计算
* 用于验证配置值是否正确
*/
const mysql = require('mysql2/promise')
const DB_CONFIG = {
host: 'gz-cynosdbmysql-grp-kfcvxbby.sql.tencentcdb.com',
port: 27815,
user: 'root',
password: 'Aa112211',
database: 'soul_miniprogram',
charset: 'utf8mb4'
}
async function testConfig() {
let connection
try {
console.log('=' .repeat(60))
console.log('推广配置测试')
console.log('=' .repeat(60))
console.log()
// 1. 连接数据库
console.log('步骤 1: 连接数据库...')
connection = await mysql.createConnection(DB_CONFIG)
console.log('✅ 已连接到数据库:', DB_CONFIG.database)
console.log()
// 2. 读取配置
console.log('步骤 2: 读取推广配置...')
console.log('-' .repeat(60))
const [configRows] = await connection.execute(
`SELECT config_key, config_value FROM system_config WHERE config_key = 'referral_config'`
)
if (configRows.length === 0) {
console.log('⚠️ 数据库中没有 referral_config使用默认值')
console.log()
console.log('默认配置:')
console.log(' distributorShare: 90 (90%)')
console.log(' minWithdrawAmount: 10')
console.log(' bindingDays: 30')
console.log(' userDiscount: 5')
console.log()
} else {
const configValue = configRows[0].config_value
let config
try {
config = typeof configValue === 'string' ? JSON.parse(configValue) : configValue
} catch (e) {
console.error('❌ 配置解析失败:', e.message)
return
}
console.log('✅ 读取到的配置:')
console.log(' distributorShare:', config.distributorShare, `(${config.distributorShare}%)`)
console.log(' minWithdrawAmount:', config.minWithdrawAmount, '元')
console.log(' bindingDays:', config.bindingDays, '天')
console.log(' userDiscount:', config.userDiscount, `(${config.userDiscount}%)`)
console.log(' enableAutoWithdraw:', config.enableAutoWithdraw)
console.log()
// 3. 测试佣金计算
console.log('步骤 3: 测试佣金计算...')
console.log('-' .repeat(60))
const distributorShareRate = config.distributorShare / 100
console.log('分成比例(计算用):', distributorShareRate)
console.log()
// 测试用例
const testCases = [
{ amount: 1.00, desc: '购买1元商品' },
{ amount: 0.95, desc: '购买1元商品5%折扣后)' },
{ amount: 9.90, desc: '购买全书9.9元' }
]
console.log('佣金计算结果:')
testCases.forEach(test => {
const commission = Math.round(test.amount * distributorShareRate * 100) / 100
console.log(` ${test.desc}:`)
console.log(` 支付金额: ¥${test.amount.toFixed(2)}`)
console.log(` 推荐人佣金: ¥${commission.toFixed(2)} (${(distributorShareRate * 100).toFixed(0)}%)`)
console.log()
})
// 4. 验证返回给小程序的值
console.log('步骤 4: 验证返回给小程序的值...')
console.log('-' .repeat(60))
const shareRate = Math.round(distributorShareRate * 100)
console.log('返回给小程序的 shareRate:', shareRate, '%')
console.log('小程序显示:', `"你获得 ${shareRate}% 收益"`)
console.log()
// 5. 检查是否有异常
console.log('步骤 5: 检查配置合理性...')
console.log('-' .repeat(60))
const issues = []
if (config.distributorShare < 0 || config.distributorShare > 100) {
issues.push(`❌ distributorShare 不在有效范围: ${config.distributorShare}`)
}
if (config.distributorShare < 50) {
issues.push(`⚠️ distributorShare 偏低: ${config.distributorShare}% (通常应该 >= 50%)`)
}
if (config.distributorShare === 10) {
issues.push(`❌ distributorShare = 10% 可能是配置错误!应该是 90%`)
}
if (config.minWithdrawAmount < 0) {
issues.push(`❌ minWithdrawAmount 不能为负数: ${config.minWithdrawAmount}`)
}
if (config.bindingDays < 1) {
issues.push(`❌ bindingDays 至少为1天: ${config.bindingDays}`)
}
if (issues.length > 0) {
console.log('发现问题:')
issues.forEach(issue => console.log(' ' + issue))
} else {
console.log('✅ 所有配置值都在合理范围内')
}
console.log()
}
console.log('=' .repeat(60))
console.log('测试完成')
console.log('=' .repeat(60))
} catch (error) {
console.error('测试失败:', error)
} finally {
if (connection) {
await connection.end()
}
}
}
testConfig()