首先我们要知道什么是分销系统?
分销系统指通过互联网将供应商与经销商有机地联系在一起,为企业的业务经营及与贸易伙伴的合作提供了一种全新的模式。供应商、分支机构和经销商之间可以实现实时地提交业务单据、查询产品供应和库存状况、并获得市场、销售信息及客户支持,实现了供应商、分支机构与经销商之间端到端的供应链管理,有效地缩短了供销链。
知识付费的分销功能主要体现在专题课程及会员模块;用户购买专题课程、会员后会给用户的上级和上上级返佣。
那么分销系统是如何实现的呢?
知识付费系统中在用户进入系统时若是带有上级的信息,那么这个用户就是别的用户发展的下级,我们就需要记录该用户的上级信息。数据库用户表eb_user表中有字段spread_uid(推广员id),该字段记录的是用户的上级id。
首先确定什么时候执行分销功能?分销功能执行肯定是需要订单结束后,这样可以避免大多数用户退款后的佣金返还问题。
然后就是分销代码的书写了,知识付费分销分为人人分销和指定分销;人人分销是指系统中的所有用户均可参与分销活动,并且在下级用户购买后可以获得佣金。指定分销是指只有系统设置推广权限的用户才能在下级用户购买后获得佣金。因为知识付费的分销是二级分销,所有我们是直接写了一级返佣和二级返佣的方法,在执行完一级返佣后执行二级返佣,这是分销的最简单逻辑了。
以专题课程为例,在专题课程购买成功后执行返佣。
/**
* //TODO 专题支付成功后
* @param $orderId
* @param $notify
* @return bool
*/
public static function paySuccess($orderId)
{
$order = self::where('order_id', $orderId)->where('type', 0)->find();
if (!$order) return false;
$resPink = true;
$res2 = true;
$res3 = true;
User::bcInc($order['uid'], 'pay_count', 1, 'uid');
$res1 = self::where('order_id', $orderId)->where('type', 0)->update(['paid' => 1, 'pay_time' => time()]);
if ($res1 && $order['pay_type'] != 'yue') {
$res2 = UserBill::expend('购买专题', $order['uid'], $order['pay_type'], 'pay_product', $order['pay_price'], $order['id'], 0, '支付' . floatval($order['pay_price']) . '元购买专题');
}
if ($res1) {
$res3 = MerchantFlowingWater::setMerchantFlowingWater($order, 0);
}
if ($order['combination_id'] && $res1 && !$order['refund_status']) {
$resPink = StorePink::createPink($order);//创建拼团
} else {
if (!$order['is_gift']) {
//如果是专栏,记录专栏下所有专题购买。
SpecialBuy::setAllBuySpecial($orderId, $order['uid'], $order['cart_id']);
TestPaperObtain::setTestPaper($orderId, $order['uid'], $order['cart_id'], 2);
DataDownloadBuy::setDataDownload($orderId, $order['uid'], $order['cart_id'], 0);
try {
//专题返佣
User::backOrderBrokerage($order);
} catch (\Throwable $e) {
}
}
}
StoreOrderStatus::status($order->id, 'pay_success', '用户付款成功');
$site_url = SystemConfigService::get('site_url');
try {
$wechat_notification_message = SystemConfigService::get('wechat_notification_message');
if ($wechat_notification_message == 1) {
$title = Special::getName($order['cart_id']);
WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
'character_string2' => $orderId,
'thing3' => mb_substr($title, 0, 20, 'utf-8'),
'amount5' => $order['pay_price'],
'time4' => date('Y-m-d H:i:s', time())
], $site_url . Url::build('wap/special/grade_special'));
WechatTemplateService::sendAdminNoticeTemplate($order['mer_id'], [
'character_string2' => $orderId,
'thing3' => mb_substr($title, 0, 20, 'utf-8'),
'amount5' => $order['pay_price'],
'time4' => date('Y-m-d H:i:s', time())
]);
} else {
$data['character_string1']['value'] = $orderId;
$data['amount3']['value'] = $order['pay_price'];
$data['time2']['value'] = date('Y-m-d H:i:s', time());
$data['thing6']['value'] = '您购买的专题已支付成功!';
RoutineTemplate::sendOrderSuccess($data, $order['uid'], $site_url . Url::build('wap/special/grade_special'));
$dataAdmin['character_string1']['value'] = $orderId;
$dataAdmin['amount3']['value'] = $order['pay_price'];
$dataAdmin['time2']['value'] = date('Y-m-d H:i:s', time());
$dataAdmin['thing6']['value'] = '您有一个新的课程订单!';
RoutineTemplate::sendAdminNoticeTemplate($dataAdmin);
}
} catch (\Throwable $e) {
}
$res = $res1 && $resPink && $res2 && $res3;
return false !== $res;
}
/**
* 一级推广 专题
* @param $orderInfo
* @return bool
*/
public static function backOrderBrokerage($orderInfo)
{
$userInfo = self::getUserData($orderInfo['uid']);
if (!$userInfo || !$userInfo['spread_uid']) return true;
$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
if ($storeBrokerageStatu == 1) {
if (!self::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return true;
}
$data = Special::getIndividualDistributionSettings($orderInfo['cart_id']);
if (isset($data['is_alone']) && $data['is_alone']) {
if (!isset($data['brokerage_ratio']) || !$data['brokerage_ratio']) return true;
$brokerageRatio = bcdiv($data['brokerage_ratio'], 100, 2);
} else {
$course_distribution_switch = SystemConfigService::get('course_distribution_switch');//课程分销开关
if ($course_distribution_switch == 0) return true;
$brokerageRatio = bcdiv(SystemConfigService::get('store_brokerage_ratio'), 100, 2);
}
if ($brokerageRatio <= 0) return true;
$brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
if ($brokeragePrice <= 0) return true;
$mark = '一级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买专题,奖励推广佣金' . floatval($brokeragePrice);
$bill = UserBill::billDoesDataExist($userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id']);
if ($bill) return true;
self::beginTrans();
$res1 = UserBill::income('购买专题返佣', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
$res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
$User = self::getUserData($userInfo['spread_uid']);
if (!$User) {
self::checkTrans(false);
return true;
}
$wechat_notification_message = SystemConfigService::get('wechat_notification_message');
if (!$wechat_notification_message) {
$dat['thing8']['value'] = '返佣金额';
$dat['date4']['value'] = date('Y-m-d H:i:s', time());
$dat['amount1']['value'] = $brokeragePrice;
$dat['amount2']['value'] = $User['brokerage_price'];
$dat['thing5']['value'] = '您收到一笔专题返佣!';
RoutineTemplate::sendAccountChanges($dat, $userInfo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
}
$dats['brokerage_price'] = $brokeragePrice;
SmsTemplate::sendSms($userInfo['spread_uid'], $dats, 'ORDER_BROKERAGE');
$res = $res1 && $res2;
self::checkTrans($res);
if ($res) self::backOrderBrokerageTwo($orderInfo);
return $res;
}
/**
* 二级推广 专题
* @param $orderInfo
* @return bool
*/
public static function backOrderBrokerageTwo($orderInfo)
{
$userInfo = self::getUserData($orderInfo['uid']);
$userInfoTwo = self::getUserData($userInfo['spread_uid']);
if (!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;
$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
if ($storeBrokerageStatu == 1) {
if (!self::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
}
$data = Special::getIndividualDistributionSettings($orderInfo['cart_id']);
if (isset($data['is_alone']) && $data['is_alone']) {
if (!isset($data['brokerage_two']) || !$data['brokerage_two']) return true;
$brokerageRatio = bcdiv($data['brokerage_two'], 100, 2);
} else {
$course_distribution_switch = SystemConfigService::get('course_distribution_switch');//课程分销开关
if ($course_distribution_switch == 0) return true;
$brokerageRatio = bcdiv(SystemConfigService::get('store_brokerage_two'), 100, 2);
}
if ($brokerageRatio <= 0) return true;
$brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
if ($brokeragePrice <= 0) return true;
$mark = '二级推广人' . $userInfo['nickname'] . '消费' . floatval($orderInfo['pay_price']) . '元购买专题,奖励推广佣金' . floatval($brokeragePrice);
$bill = UserBill::billDoesDataExist($userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id']);
if ($bill) return true;
self::beginTrans();
$res1 = UserBill::income('购买专题返佣', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
$res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
$User = self::getUserData($userInfoTwo['spread_uid']);
if (!$User) {
self::checkTrans(false);
return true;
}
$wechat_notification_message = SystemConfigService::get('wechat_notification_message');
if (!$wechat_notification_message) {
$dat['thing8']['value'] = '返佣金额';
$dat['date4']['value'] = date('Y-m-d H:i:s', time());
$dat['amount1']['value'] = $brokeragePrice;
$dat['amount2']['value'] = $User['brokerage_price'];
$dat['thing5']['value'] = '您收到一笔专题返佣!';
RoutineTemplate::sendAccountChanges($dat, $userInfoTwo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
}
$dats['brokerage_price'] = $brokeragePrice;
SmsTemplate::sendSms($userInfoTwo['spread_uid'], $dats, 'ORDER_BROKERAGE');
$res = $res1 && $res2;
self::checkTrans($res);
return $res;
}
这样专题课程的二级分销就完成了,若是想再加第三级分销,我们一样的逻辑,获取二级的上级,然后判断返佣即可。