分销事件
本页面介绍分销相关的事件类型及处理示例。
事件列表
| 事件标识 | 说明 | payload |
|---|---|---|
| DISTRIBUTION_APPLY | 申请成为分销商 | 申请ID |
| DISTRIBUTION_AUDIT_PASS | 分销商审核通过 | 分销商ID |
| DISTRIBUTION_WITHDRAW_APPLY | 佣金提现申请 | 提现记录ID |
| DISTRIBUTION_WITHDRAW_SUCCESS | 佣金提现打款成功 | 提现记录ID |
| DISTRIBUTION_WITHDRAW_FAIL | 佣金提现申请失败 | 提现记录ID |
事件数据结构
分销事件的payload通常是记录ID:
json
{
"eventKey": "DISTRIBUTION_WITHDRAW_SUCCESS",
"payload": 12345, // 提现记录ID
"timestamp": 1704067200000
}DISTRIBUTION_APPLY - 申请成为分销商
用户申请成为分销商时触发。
处理示例:申请通知审核
javascript
// 接口路径: /custom/distribution/apply-notify
// 处理 DISTRIBUTION_APPLY 事件
var event = payload.event;
var applyId = event.payload;
log.info("处理分销商申请事件: applyId={}", applyId);
// 查询申请信息
var apply = db.selectOne(`
SELECT da.*, u.nickname, u.phone, u.avatar
FROM eb_distribution_apply da
LEFT JOIN eb_user u ON da.uid = u.uid
WHERE da.id = ?
`, applyId);
// 发送审核通知给运营
http.post(env.get('DINGTALK_WEBHOOK'), {
body: {
msgtype: 'markdown',
markdown: {
title: '分销商申请',
text: '### 新分销商申请\n' +
'- **申请人**: ' + apply.nickname + '\n' +
'- **手机号**: ' + apply.phone + '\n' +
'- **申请时间**: ' + apply.create_time + '\n' +
'- **申请理由**: ' + (apply.remark || '无') + '\n\n' +
'请及时审核!'
}
}
});
log.info("分销商申请通知已发送: applyId={}, user={}", applyId, apply.nickname);
return { success: true };DISTRIBUTION_AUDIT_PASS - 分销商审核通过
分销商申请审核通过时触发。
处理示例:分销商入驻通知
javascript
// 接口路径: /custom/distribution/audit-pass
// 处理 DISTRIBUTION_AUDIT_PASS 事件
var event = payload.event;
var distributorId = event.payload;
log.info("处理分销商审核通过事件: distributorId={}", distributorId);
// 查询分销商信息
var distributor = db.selectOne(`
SELECT d.*, u.nickname, u.phone
FROM eb_distributor d
LEFT JOIN eb_user u ON d.uid = u.uid
WHERE d.id = ?
`, distributorId);
// 发送审核通过短信
if (distributor.phone) {
http.post(env.get('SMS_API_URL') + '/send', {
body: {
phone: distributor.phone,
template: 'distribution_audit_pass',
params: {
nickname: distributor.nickname
}
}
});
}
// 同步到CRM
http.post(env.get('CRM_API_URL') + '/distributor/create', {
headers: { 'Authorization': 'Bearer ' + env.get('CRM_TOKEN') },
body: {
externalId: 'miseb_dist_' + distributorId,
userId: 'miseb_' + distributor.uid,
name: distributor.nickname,
phone: distributor.phone,
level: distributor.level,
status: 'active',
approveTime: distributor.update_time
}
});
log.info("分销商审核通过处理完成: distributorId={}", distributorId);
return { success: true };DISTRIBUTION_WITHDRAW_APPLY - 佣金提现申请
分销商申请提现佣金时触发。
处理示例:提现审核通知
javascript
// 接口路径: /custom/distribution/withdraw-apply
// 处理 DISTRIBUTION_WITHDRAW_APPLY 事件
var event = payload.event;
var withdrawId = event.payload;
log.info("处理佣金提现申请事件: withdrawId={}", withdrawId);
// 查询提现记录
var withdraw = db.selectOne(`
SELECT uc.*, u.nickname, u.phone
FROM eb_user_closing uc
LEFT JOIN eb_user u ON uc.uid = u.uid
WHERE uc.id = ?
`, withdrawId);
// 大额提现告警
var alertThreshold = 100000; // 1000元
if (withdraw.closing_price > alertThreshold) {
http.post(env.get('DINGTALK_WEBHOOK'), {
body: {
msgtype: 'text',
text: {
content: '【大额提现申请】\n' +
'用户:' + withdraw.nickname + '\n' +
'手机:' + withdraw.phone + '\n' +
'提现金额:¥' + (withdraw.closing_price / 100).toFixed(2) + '\n' +
'提现方式:' + withdraw.closing_type + '\n' +
'请及时审核!'
}
}
});
}
// 记录提现日志
db.insert(`
INSERT INTO withdraw_log (withdraw_id, uid, amount, status, created_at)
VALUES (?, ?, ?, 'apply', NOW())
`, withdrawId, withdraw.uid, withdraw.closing_price);
log.info("提现申请通知已发送: withdrawId={}, amount={}",
withdrawId, withdraw.closing_price / 100);
return { success: true };DISTRIBUTION_WITHDRAW_SUCCESS - 佣金提现打款成功
提现审核通过并打款成功时触发。
处理示例:打款成功通知
javascript
// 接口路径: /custom/distribution/withdraw-success
// 处理 DISTRIBUTION_WITHDRAW_SUCCESS 事件
var event = payload.event;
var withdrawId = event.payload;
log.info("处理提现成功事件: withdrawId={}", withdrawId);
// 查询提现记录
var withdraw = db.selectOne(`
SELECT uc.*, u.nickname, u.phone
FROM eb_user_closing uc
LEFT JOIN eb_user u ON uc.uid = u.uid
WHERE uc.id = ?
`, withdrawId);
// 发送提现成功短信
if (withdraw.phone) {
http.post(env.get('SMS_API_URL') + '/send', {
body: {
phone: withdraw.phone,
template: 'withdraw_success',
params: {
amount: (withdraw.closing_price / 100).toFixed(2),
withdrawType: withdraw.closing_type
}
}
});
}
// 同步到财务系统
http.post(env.get('FINANCE_API_URL') + '/payout/create', {
headers: { 'Authorization': 'Bearer ' + env.get('FINANCE_API_KEY') },
body: {
type: 'commission_withdraw',
orderId: 'WD' + withdrawId,
userId: withdraw.uid,
amount: withdraw.closing_price / 100,
payoutType: withdraw.closing_type,
accountInfo: withdraw.alipay_code || withdraw.wechat_code || withdraw.bank_code,
time: withdraw.update_time
}
});
// 更新提现日志状态
db.update(`
UPDATE withdraw_log SET status = 'success', updated_at = NOW()
WHERE withdraw_id = ?
`, withdrawId);
log.info("提现成功处理完成: withdrawId={}, amount={}",
withdrawId, withdraw.closing_price / 100);
return { success: true };DISTRIBUTION_WITHDRAW_FAIL - 佣金提现申请失败
提现申请被拒绝或打款失败时触发。
处理示例:打款失败通知
javascript
// 接口路径: /custom/distribution/withdraw-fail
// 处理 DISTRIBUTION_WITHDRAW_FAIL 事件
var event = payload.event;
var withdrawId = event.payload;
log.info("处理提现失败事件: withdrawId={}", withdrawId);
// 查询提现记录
var withdraw = db.selectOne(`
SELECT uc.*, u.nickname, u.phone
FROM eb_user_closing uc
LEFT JOIN eb_user u ON uc.uid = u.uid
WHERE uc.id = ?
`, withdrawId);
// 发送提现失败短信
if (withdraw.phone) {
http.post(env.get('SMS_API_URL') + '/send', {
body: {
phone: withdraw.phone,
template: 'withdraw_fail',
params: {
amount: (withdraw.closing_price / 100).toFixed(2),
reason: withdraw.fail_msg || '审核未通过'
}
}
});
}
// 更新提现日志状态
db.update(`
UPDATE withdraw_log SET status = 'fail', fail_reason = ?, updated_at = NOW()
WHERE withdraw_id = ?
`, withdraw.fail_msg, withdrawId);
// 发送告警(多次失败可能有问题)
var failCount = db.selectInt(`
SELECT COUNT(*) FROM eb_user_closing
WHERE uid = ? AND status = 3
AND create_time > DATE_SUB(NOW(), INTERVAL 30 DAY)
`, withdraw.uid);
if (failCount >= 3) {
http.post(env.get('DINGTALK_WEBHOOK'), {
body: {
msgtype: 'text',
text: {
content: '【提现异常告警】\n' +
'用户:' + withdraw.nickname + '\n' +
'手机:' + withdraw.phone + '\n' +
'30天内提现失败次数:' + failCount + '\n' +
'请关注账户状态!'
}
}
});
}
log.info("提现失败处理完成: withdrawId={}", withdrawId);
return { success: true };数据库表参考
eb_distributor 分销商表
| 字段 | 说明 |
|---|---|
| id | 分销商ID |
| uid | 用户ID |
| level | 分销商等级 |
| status | 状态 |
| total_brokerage | 累计佣金 |
eb_user_closing 提现表
| 字段 | 说明 |
|---|---|
| id | 提现ID |
| uid | 用户ID |
| closing_price | 提现金额(分) |
| closing_type | 提现方式 |
| status | 状态:0待审核 1审核通过 2已打款 3已拒绝 |
| fail_msg | 失败原因 |
