/** * 累计收益统计口径与用户钱包余额统一专项测试 * 覆盖范围: * 1. IdentityService attachAgent / attachProfile 累计收益口径统一与兜底 * 2. User profile 接口统一映射 balance = money 及累计收益补偿 * 3. partner/Index index / rewards 接口统一返回 total_income 与 month_income * 4. WalletLog 收益统计扣除互转重复项,真实反映收益与扣减(防误导) * 5. uniapp 视图端全面对齐: * - rewards.vue 累计已结算收益卡片与本月统计 * - account-info.vue 累计收益与钱包余额 * - commission.vue 累计总收益、周期收益与钱包可用余额 * - withdraw.vue 钱包可用余额提示与收益入账说明 * 6. 算法逻辑模拟:无重复统计、真实收益判定与资金守恒 */ const fs = require('fs'); const path = require('path'); let passCount = 0; let failCount = 0; function assert(condition, message) { if (condition) { console.log(` \x1b[32m[PASS]\x1b[0m ${message}`); passCount++; } else { console.error(` \x1b[31m[FAIL]\x1b[0m ${message}`); failCount++; } } console.log('========================================================'); console.log(' 累计收益统计口径与用户钱包余额统一专项测试'); console.log('========================================================\n'); // ---------------------------------------------------- // 1. IdentityService 累计收益与资产聚合验证 // ---------------------------------------------------- console.log('--- 1. IdentityService 累计收益口径与资产聚合验证 ---'); const identityServicePath = path.resolve(__dirname, '../../addons/shopro/service/partner/IdentityService.php'); assert(fs.existsSync(identityServicePath), 'IdentityService.php 文件存在'); if (fs.existsSync(identityServicePath)) { const content = fs.readFileSync(identityServicePath, 'utf-8'); assert(content.includes('max($agentTotalIncome, floatval($rewardCash))'), 'attachAgent 取分销商总收益与收益中心奖励最大值对齐'); assert(content.includes("'total_income' => self::formatMoney($unifiedTotalIncome)"), 'attachAgent 返回格式化 total_income'); assert(content.includes("'income_total_cash' => self::formatMoney($unifiedTotalIncome)"), 'attachAgent 返回格式化 income_total_cash'); assert(content.includes("'total_income' => $full['total_income'] ?? '0.00'"), 'attachProfile 包含 total_income'); assert(content.includes("'income_total_cash' => $full['income_total_cash'] ?? '0.00'"), 'attachProfile 包含 income_total_cash'); } // ---------------------------------------------------- // 2. User profile 接口验证 // ---------------------------------------------------- console.log('\n--- 2. User.php 个人详情接口 balance 与收益对齐验证 ---'); const userControllerPath = path.resolve(__dirname, '../../addons/shopro/controller/user/User.php'); assert(fs.existsSync(userControllerPath), 'addons/shopro/controller/user/User.php 文件存在'); if (fs.existsSync(userControllerPath)) { const content = fs.readFileSync(userControllerPath, 'utf-8'); assert(content.includes("$data['balance'] = $user->money"), 'profile 明确将 balance 映射为钱包余额 money'); assert(content.includes('$userRewardCash > 0'), '非分销商身份但有分成收益时,执行累计收益兜底统计'); } // ---------------------------------------------------- // 3. partner/Index 接口对齐验证 // ---------------------------------------------------- console.log('\n--- 3. partner/Index 概览与明细接口 total_income 对齐验证 ---'); const partnerIndexPath = path.resolve(__dirname, '../../addons/shopro/controller/partner/Index.php'); assert(fs.existsSync(partnerIndexPath), 'partner/Index.php 文件存在'); if (fs.existsSync(partnerIndexPath)) { const content = fs.readFileSync(partnerIndexPath, 'utf-8'); assert(content.includes("'total_income' => $totalIncome"), 'index 概览接口返回 total_income'); assert(content.includes("'month_income' => IdentityService::formatMoney($monthIncome)"), 'index 概览接口返回本月收益 month_income'); assert(content.includes("'total_income' => $totalIncome"), 'rewards 明细接口返回 total_income'); } // ---------------------------------------------------- // 4. WalletLog 收益统计防误导口径验证 // ---------------------------------------------------- console.log('\n--- 4. WalletLog 收入与支出统计口径防误导验证 ---'); const walletLogPath = path.resolve(__dirname, '../../addons/shopro/controller/user/WalletLog.php'); assert(fs.existsSync(walletLogPath), 'WalletLog.php 文件存在'); if (fs.existsSync(walletLogPath)) { const content = fs.readFileSync(walletLogPath, 'utf-8'); assert(content.includes("whereNotIn('event', ['transfer_by_commission'])"), 'commission 收入统计排除了转余额入账 transfer_by_commission (防重复计算)'); assert(content.includes("whereNotIn('event', ['transfer_to_money', 'commission_transfer'])"), 'commission 支出统计排除了结转至余额 transfer_to_money (防伪支出误导)'); assert(content.includes('$actualRewardEvents'), '定义了真实业务奖励事件清单 actualRewardEvents'); } // ---------------------------------------------------- // 5. uniapp 页面端到端对齐验证 // ---------------------------------------------------- console.log('\n--- 5. uniapp 视图端全面对齐验证 ---'); // 5.1 rewards.vue const rewardsVuePath = path.resolve(__dirname, '../../uniapp/pages/commission/rewards.vue'); assert(fs.existsSync(rewardsVuePath), 'rewards.vue 文件存在'); if (fs.existsSync(rewardsVuePath)) { const content = fs.readFileSync(rewardsVuePath, 'utf-8'); assert(content.includes('累计已结算收益(元)'), 'rewards.vue 卡片主标题变更为「累计已结算收益(元)」'); assert(content.includes('displayTotalIncome'), 'rewards.vue 卡片主金额使用 displayTotalIncome 绑定'); assert(content.includes('本月已结算: ¥{{ state.overview.month_income'), '副指标清晰标注本月已结算'); assert(content.includes('state.overview.total_income'), 'overview 状态机与接口对齐包含 total_income'); } // 5.2 account-info.vue const accountInfoVuePath = path.resolve(__dirname, '../../uniapp/pages/commission/components/account-info.vue'); assert(fs.existsSync(accountInfoVuePath), 'account-info.vue 文件存在'); if (fs.existsSync(accountInfoVuePath)) { const content = fs.readFileSync(accountInfoVuePath, 'utf-8'); assert(content.includes("agentInfo?.total_income || userInfo?.total_income"), '累计收益具备 agentInfo 与 userInfo 双重对齐兜底'); assert(content.includes("userInfo?.money || '0.00'"), '钱包余额绑定 userInfo.money'); } // 5.3 commission.vue const commissionVuePath = path.resolve(__dirname, '../../uniapp/pages/user/wallet/commission.vue'); assert(fs.existsSync(commissionVuePath), 'commission.vue 文件存在'); if (fs.existsSync(commissionVuePath)) { const content = fs.readFileSync(commissionVuePath, 'utf-8'); assert(content.includes("agentInfo.total_income || userInfo.total_income"), '累计总收益具备双重兜底'); assert(content.includes('周期收益¥'), '日期区间统计描述对齐为「周期收益」,明确收益口径'); } // 5.4 withdraw.vue const withdrawVuePath = path.resolve(__dirname, '../../uniapp/pages/pay/withdraw.vue'); assert(fs.existsSync(withdrawVuePath), 'withdraw.vue 文件存在'); if (fs.existsSync(withdrawVuePath)) { const content = fs.readFileSync(withdrawVuePath, 'utf-8'); assert(content.includes('钱包可用余额:¥{{ userInfo.money'), 'withdraw.vue 同步展示钱包可用余额'); assert(content.includes('收益分成已全额直接入账钱包余额'), 'withdraw.vue 显著提示收益已全额直接入账钱包余额'); } // ---------------------------------------------------- // 6. 统计口径算法精准性断言 // ---------------------------------------------------- console.log('\n--- 6. 统计口径算法精准性模拟断言 ---'); const actualRewardEvents = [ 'push3', 'maker_diff', 'maker_peer', 'maker_overtaken', 'team_venue', 'team_county', 'venue_peer', 'county_peer', 'venue_share', 'county_share', 'reward_income' ]; // 模拟典型用户流水: // 1) 历史获得推三返一:+398 (commission) // 2) 结转转出至余额:-398 (commission, event=transfer_to_money) // 3) 结转入账至余额:+398 (money, event=transfer_by_commission) // 4) 产生新订单直入余额奖:+50 (money, event=team_venue) // 5) 新订单发生售后退回:-50 (money, event=team_venue_back) const userTestLogs = [ { type: 'commission', event: 'push3', amount: 398.00 }, { type: 'commission', event: 'transfer_to_money', amount: -398.00 }, { type: 'money', event: 'transfer_by_commission', amount: 398.00 }, { type: 'money', event: 'team_venue', amount: 50.00 }, { type: 'money', event: 'team_venue_back', amount: -50.00 }, ]; function calculateAlignedIncomeAndExpense(logs) { let income = 0; let expense = 0; for (const log of logs) { if (log.amount > 0) { // 排除互转入账,只统计真正创造的收益 if (log.type === 'money' && actualRewardEvents.includes(log.event)) { income += log.amount; } else if (log.type === 'commission' && log.event !== 'transfer_by_commission') { income += log.amount; } } else if (log.amount < 0) { // 排除结转内部转出,只统计真实的业务扣减与退款 if (!['transfer_to_money', 'commission_transfer'].includes(log.event)) { expense += log.amount; } } } return { income, expense }; } const calculated = calculateAlignedIncomeAndExpense(userTestLogs); assert(calculated.income === (398.00 + 50.00), `总收益收入精准为 ¥${calculated.income} (398+50=448,未被 398 结转重复统计为 846)`); assert(calculated.expense === -50.00, `总支出精准为 ¥${calculated.expense} (仅扣款-50,未被 -398 结转误统计为 -448)`); const netGain = calculated.income + calculated.expense; assert(netGain === 398.00, `实际净收益精准守恒为 ¥${netGain}`); console.log('\n========================================================'); console.log(`测试完成!通过: \x1b[32m${passCount}\x1b[0m, 失败: \x1b[31m${failCount}\x1b[0m`); console.log('========================================================'); if (failCount > 0) { process.exit(1); } else { process.exit(0); }