首先咱们要晓得什么是分销零碎?
分销零碎指通过互联网将供应商与经销商有机地分割在一起,为企业的业务经营及与贸易搭档的单干提供了一种全新的模式。供应商、分支机构和经销商之间能够实现实时地提交业务单据、查问产品供给和库存情况、并取得市场、销售信息及客户反对,实现了供应商、分支机构与经销商之间端到端的供应链治理,无效地缩短了供销链。
常识付费的分销性能次要体现在专题课程及会员模块;用户购买专题课程、会员后会给用户的下级和上下级返佣。
那么分销零碎是如何实现的呢?
常识付费零碎中在用户进入零碎时若是带有下级的信息,那么这个用户就是别的用户倒退的上级,咱们就须要记录该用户的下级信息。数据库用户表eb_user表中有字段spread_uid(推广员id),该字段记录的是用户的下级id。
首先确定什么时候执行分销性能?分销性能执行必定是须要订单完结后,这样能够防止大多数用户退款后的佣金返还问题。
而后就是分销代码的书写了,常识付费分销分为人人分销和指定分销;人人分销是指零碎中的所有用户均可参加分销流动,并且在上级用户购买后能够取得佣金。指定分销是指只有零碎设置推广权限的用户能力在上级用户购买后取得佣金。因为常识付费的分销是二级分销,所有咱们是间接写了一级返佣和二级返佣的办法,在执行完一级返佣后执行二级返佣,这是分销的最简略逻辑了。
以专题课程为例,在专题课程购买胜利后执行返佣。(注:以下代码为局部代码,具体见常识付费https://gitee.com/ZhongBangKe...)
/**

  • //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;User::bcInc($order['uid'], 'pay_count', 1, 'uid');$res = self::where('order_id', $orderId)->where('type',0)->update(['paid' => 1, 'pay_time' => time()]);try {    //专题返佣    User::backOrderBrokerage($order);} catch (\Throwable $e) {}StoreOrderStatus::status($order->id, 'pay_success', '用户付款胜利');return false !== $res;

}

/**

  • 一级推广 专题
  • @param $orderInfo
  • @return bool
    */

public static function backOrderBrokerage($orderInfo)
{

$userInfo = User::getUserInfo($orderInfo['uid']);if (!$userInfo || !$userInfo['spread_uid']) return true;$course_distribution_switch = SystemConfigService::get('course_distribution_switch');//课程分销开关if($course_distribution_switch==0) return true;$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后盾分销类型if ($storeBrokerageStatu == 1) {    if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) 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);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');$res = $res1 && $res2;self::checkTrans($res);if ($res) self::backOrderBrokerageTwo($orderInfo);return $res;

}

/**

  • 二级推广 专题
  • @param $orderInfo
  • @return bool
    */

public static function backOrderBrokerageTwo($orderInfo)
{

$userInfo = User::getUserInfo($orderInfo['uid']);$userInfoTwo = User::getUserInfo($userInfo['spread_uid']);if (!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;$course_distribution_switch = SystemConfigService::get('course_distribution_switch');//课程分销开关if($course_distribution_switch==0) return true;$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后盾分销类型if ($storeBrokerageStatu == 1) {    if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) 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);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');$res = $res1 && $res2;self::checkTrans($res);return $res;

}
这样专题课程的二级分销就实现了,若是想再加第三级分销,咱们一样的逻辑,获取二级的下级,而后判断返佣即可。
如果你感觉这篇文章对你有点用的话,麻烦请给咱们的开源我的项目点点star: http://github.crmeb.net/u/defu 不胜感激 !