直播开发中关于三级分销功能源码的实现介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yun_bao_2144899870/article/details/87074245

用户在使用APP的时候可以通过填写邀请用户的邀请码来绑定用户与用户之间的上下级关系,通过这层关系用户在送礼物或者进行其他消费的时候他的上级用户就可以得到收益,下面就直播开发中关于三级分销功能源码的实现介绍。
首先,我们需要生成邀请码:

public function createCode($len=6,$format='ALL2'){  //传递生成邀请码的长度以及生成的类型
$is_abc = $is_numer = 0;
$password = $tmp =''; 

switch($format){  //通过参数来选择生成的邀请码类型
case 'ALL':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
case 'ALL2':
break;
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case 'NUMBER':
$chars='0123456789';
break;
default :
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
}
while(strlen($password)<$len){  //判断邀请码的长度,直到邀请码长度够了停止循环
$tmp =substr($chars,(mt_rand()%strlen($chars)),1); //每次在$chars中取一位
if(($is_numer <> 1 && is_numeric($tmp) && $tmp > 0 )|| $format == 'CHAR'){
$is_numer = 1;
}
if(($is_abc <> 1 && preg_match('/[a-zA-Z]/',$tmp)) || $format == 'NUMBER'){
$is_abc = 1;
}
$password.= $tmp;
}
if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){
$password = createCode($len,$format); //判断邀请码是否生成成功,不成功重新生成
}
if($password!=''){  
$oneinfo=DI()->notorm->users_agent_code
     ->select("uid")
     ->where("code=?",$password)
     ->fetchOne();
if(!$oneinfo){ //判断邀请码是否存在
return $password;
} 
}
$password = createCode($len,$format);
return $password;
}

通过以上直播源码开发我们就可以灵活的实现邀请码,可以通过传递的数值来控制邀请码的位数以及邀请码的组成,在代码中检索这个生成的邀请码是否有其他用户正在使用,如果有在使用那么我们就再重新生成一个,直到邀请码唯一为止,邀请码既然生成了,我们就需要用户填写邀请码并生成上下级。
public function setDistribut( u i d , uid, code){ //传递被邀请用户的id和邀请人的邀请码

$isexist=DI()->notorm->users_agent   //查看用户是否存在
                ->select("*")
                ->where('uid=?',$uid)
                ->fetchOne();
if($isexist){
return 1004;
}
       $oneinfo=DI()->notorm->users_agent_code  //检索邀请码是否存在
                ->select("uid")
                ->where('code=? and uid!=?',$code,$uid)
                ->fetchOne();
        if(!$oneinfo){
            return 1002;
        }
        $agentinfo=DI()->notorm->users_agent
                ->select("*")
                ->where('uid=?',$oneinfo['uid'])
                ->fetchOne();
        if(!$agentinfo){
            $agentinfo=array(
                'uid'=>$oneinfo['uid'],
                'one_uid'=>0,
                'two_uid'=>0,
            );
        }
/* 判断对方是否自己下级 */
if($agentinfo['one_uid']==$uid || $agentinfo['two_uid']==$uid ){
return 1003;
}
        $data=array(
            'uid'=>$uid,
            'one_uid'=>$agentinfo['uid'],
            'two_uid'=>$agentinfo['one_uid'],
            'three_uid'=>$agentinfo['two_uid'],
            'addtime'=>time(),
        );
        DI()->notorm->users_agent->insert($data);
        return 0;
}

生成上下级关系后,用户在消费的时候就可以进行分红了。

 function setAgentProfit($uid,$total){  //传递消费用户的id和消费数额
        /* 分销 */
        $distribut1=0;
        $distribut2=0;
        $distribut3=0;
        $configpri=getConfigPri();
        if($configpri['agent_switch']==1){ //判断分销开关是否开启
            $agent=DI()->notorm->users_agent
                ->select("*")
                ->where('uid=?',$uid)
                ->fetchOne();
            $isinsert=0;
            /* 一级分销 */
            if($agent['one_uid'] && $configpri['distribut1']){ 
                $distribut1=$total*$configpri['distribut1']*0.01; //分销比例*分销数额
                $profit=DI()->notorm->users_agent_profit
                    ->select("*")
                    ->where('uid=?',$agent['one_uid'])
                    ->fetchOne();
                if($profit){
                    DI()->notorm->users_agent_profit //收益添加到个人账户中
                        ->where('uid=?',$agent['one_uid'])
                        ->update(array('one_profit' => new NotORM_Literal("one_profit + {$distribut1}")));
                }else{
                    DI()->notorm->users_agent_profit
                        ->insert(array('uid'=>$agent['one_uid'],'one_profit' =>$distribut1 ));
                }
                DI()->notorm->users
                        ->where('id=?',$agent['one_uid'])
                        ->update(array('votes' => new NotORM_Literal("votes + {$distribut1}")));
                $isinsert=1;
            }
            /* 二级分销 */
            if($agent['two_uid'] && $configpri['distribut2']){
                $distribut2=$total*$configpri['distribut2']*0.01;
                $profit=DI()->notorm->users_agent_profit
                    ->select("*")
                    ->where('uid=?',$agent['two_uid'])
                    ->fetchOne();
                if($profit){
                    DI()->notorm->users_agent_profit
                        ->where('uid=?',$agent['two_uid'])
                        ->update(array('two_profit' => new NotORM_Literal("two_profit + {$distribut2}")));
                }else{
                    DI()->notorm->users_agent_profit
                        ->insert(array('uid'=>$agent['two_uid'],'two_profit' =>$distribut2 ));
                }
                DI()->notorm->users
                        ->where('id=?',$agent['two_uid'])
                        ->update(array('votes' => new NotORM_Literal("votes + {$distribut2}")));
                $isinsert=1;
            }
            /* 三级分销 */
              if($agent['three_uid'] && $configpri['distribut3']){
                $distribut3=$total*$configpri['distribut3']*0.01;
                $profit=DI()->notorm->users_agent_profit
                    ->select("*")
                    ->where('uid=?',$agent['three_uid'])
                    ->fetchOne();
                if($profit){
                    DI()->notorm->users_agent_profit
                        ->where('uid=?',$agent['three_uid'])
                        ->update(array('three_profit' => new NotORM_Literal("three_profit + {$distribut3}")));
                }else{
                    DI()->notorm->users_agent_profit
                        ->insert(array('uid'=>$agent['three_uid'],'three_profit' =>$distribut3 ));
                }
                DI()->notorm->users
                        ->where('id=?',$agent['three_uid'])
                        ->update(array('votes' => new NotORM_Literal("votes + {$distribut3}")));
                $isinsert=1;
            } 
            if($isinsert==1){
                $data=array(
                    'uid'=>$uid,
                    'total'=>$total,
                    'one_uid'=>$agent['one_uid'],
                    'two_uid'=>$agent['two_uid'],
                    'three_uid'=>$agent['three_uid'],
                    'one_profit'=>$distribut1,
                    'two_profit'=>$distribut2,
                    'three_profit'=>$distribut3,
                    'addtime'=>time(),
                );
                DI()->notorm->users_agent_profit_recode->insert( $data );    
            }
        }
        return 1;
}

通过以上直播源代码开发我们就可以实现用户与用户之间的三级分销了。

猜你喜欢

转载自blog.csdn.net/yun_bao_2144899870/article/details/87074245