| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- /**
- * 分销
- */
- namespace app\appapi\controller;
- use cmf\controller\HomeBaseController;
- use think\facade\Db;
- use think\db\Query;
- class AgentController extends HomebaseController {
-
- public function index(){
- $data = $this->request->param();
- $uid=checkNull($data['uid']);
- $token=checkNull($data['token']);
-
- if(checkToken($uid,$token)==700){
- $this->assign("reason",lang('您的登陆状态失效,请重新登陆!'));
- return $this->fetch(':error');
- }
-
- $nowtime=time();
-
- //当天0点
- $today=date("Ymd",$nowtime);
- $today_start=strtotime($today);
- //昨天0点
- $yes_start=strtotime("{$today} - 1 day");
- $agentinfo=array();
-
- $count=Db::name("agent")->where(['one'=>$uid])->count();
-
- $total=Db::name("votes_record")
- ->where(['touid'=>$uid,'action'=>1])
- ->where('addtime','between',[$yes_start,$today_start])
- ->sum("votes");
-
- if(!$total){
- $total=0;
- }
-
-
- $agentprofit=Db::name("agent_profit")->where(["uid"=>$uid])->find();
-
- if($agentprofit){
- $one_p=$agentprofit['one_p'];
- if(!$one_p){
- $one_p=0;
- }
- }else{
- $one_p=0;
- }
- $agnet_profit=array(
- 'count'=>$count,
- 'total'=>$total,
- 'one_p'=>$one_p,
- );
- $configpub=getConfigPub();
- $configpri=getConfigPri();
- $agnet_code=Db::name("user")->where("id={$uid}")->value("code");
- $name_votes=$configpub['name_votes'];
- $agent_reward=$configpri['agent_reward'];
-
-
-
- /* 是否是分销下级 */
- $users_agent=Db::name("agent")->where(["uid"=>$uid])->find();
- if($users_agent){
- $agentinfo= getUserInfo($users_agent['one']);
- }
- $this->assign("uid",$uid);
- $this->assign("token",$token);
- $this->assign("agnet_profit",$agnet_profit);
- $this->assign("agnet_code",$agnet_code);
- $this->assign("agent_reward",$agent_reward);
- $this->assign("name_votes",$name_votes);
- $this->assign('agentinfo', $agentinfo);
- return $this->fetch();
-
- }
-
-
- public function agent(){
- $data = $this->request->param();
- $uid=isset($data['uid']) ? $data['uid']: '';
- $token=isset($data['token']) ? $data['token']: '';
- $uid=(int)checkNull($uid);
- $token=checkNull($token);
-
- $checkToken=checkToken($uid,$token);
- if($checkToken==700){
- $reason=lang('您的登陆状态失效,请重新登陆!');
- $this->assign('reason', $reason);
- return $this->fetch(':error');
- }
-
- $agentinfo=array();
-
- $users_agent=Db::name('agent')->where(["uid"=>$uid])->find();
- if($users_agent){
- $agentinfo=getUserInfo($users_agent['one']);
-
- $code=Db::name('user')->where("id={$users_agent['one']}")->value('code');
-
- $agentinfo['code']=$code;
- $code_a=str_split($code);
- $this->assign("code_a",$code_a);
- }
-
-
- $this->assign("uid",$uid);
- $this->assign("token",$token);
- $this->assign("agentinfo",$agentinfo);
- return $this->fetch();
- }
-
- public function setAgent(){
- $data = $this->request->param();
- $uid=isset($data['uid']) ? $data['uid']: '';
- $token=isset($data['token']) ? $data['token']: '';
- $code=isset($data['code']) ? $data['code']: '';
- $uid=(int)checkNull($uid);
- $token=checkNull($token);
- $code=checkNull($code);
-
- $rs=array('code'=>0,'info'=>array(),'msg'=>lang('设置成功'));
-
- if(checkToken($uid,$token)==700){
- $rs['code']=700;
- $rs['msg']=lang('您的登陆状态失效,请重新登陆!');
- echo json_encode($rs);
- return;
- }
- if($code==""){
- $rs['code']=1001;
- $rs['msg']=lang('邀请码不能为空');
- echo json_encode($rs);
- return;
- }
-
- $isexist=Db::name('agent')->where(["uid"=>$uid])->find();
- if($isexist){
- $rs['code']=1001;
- $rs['msg']=lang('已设置');
- echo json_encode($rs);
- return;
- }
-
- $oneinfo=Db::name('user')->field("id")->where(["code"=>$code])->find();
- if(!$oneinfo){
- $rs['code']=1002;
- $rs['msg']=lang('邀请码错误');
- echo json_encode($rs);
- return;
- }
-
- if($oneinfo['id']==$uid){
- $rs['code']=1003;
- $rs['msg']=lang('不能填写自己的邀请码');
- echo json_encode($rs);
- return;
- }
-
- $one_agent=Db::name('agent')->where("uid={$oneinfo['id']}")->find();
- if(!$one_agent){
- $one_agent=array(
- 'uid'=>$oneinfo['id'],
- 'one'=>0,
- );
- }else{
- if($one_agent['one']==$uid){
- $rs['code']=1004;
- $rs['msg']=lang('您已经是该用户的上级');
- echo json_encode($rs);
- return;
- }
- }
-
- $data=array(
- 'uid'=>$uid,
- 'one'=>$one_agent['uid'],
- 'addtime'=>time(),
- );
- Db::name('agent')->insert($data);
-
- /* 邀请奖励 */
- $one_uid=$oneinfo['id'];
-
-
- $configPri=getConfigPri();
- $nowtime=time();
- $agent_reward=$configPri['agent_reward'];
- if($agent_reward){
- $data=[
- 'action'=>'1', //邀请奖励
- 'uid'=>$uid,
- 'touid'=>$one_uid,
- 'votes'=>$agent_reward,
- 'addtime'=>$nowtime,
- ];
-
- //写入映票收入记录
- setVoteRecord($data);
- //更新用户的映票
- changeUserVotes($one_uid,$agent_reward,1);
- $rs2= Db::name('agent_profit')
- ->where("uid={$one_uid}")
- ->find();
- if(!$rs2){
- Db::name('agent_profit')
- ->insert(array('uid'=> $one_uid,'one_p'=> $agent_reward));
- }else{
- Db::name('agent_profit')
- ->where("uid={$one_uid}")
- ->inc("one_p",$agent_reward)
- ->update();
- }
- $rs3=Db::name('agent_profit')
- ->where("uid={$uid}")
- ->find();
- if(!$rs3){
- Db::name('agent_profit')->insert(array('uid'=> $uid,'one'=> $agent_reward));
- }else{
- Db::name('agent_profit')
- ->where("uid={$uid}")
- ->inc("one",$agent_reward)
- ->update();
- }
- }
- echo json_encode($rs);
- }
-
- public function one(){
- $data = $this->request->param();
- $uid=checkNull($data['uid']);
- $token=checkNull($data['token']);
-
- if(checkToken($uid,$token)==700){
- $this->assign("reason",lang('您的登陆状态失效,请重新登陆!'));
- $this->display(':error');
- return;
- }
-
- $Agent_profit=Db::name("user_agent_profit_recode");
-
- $list=$Agent_profit
- ->field("uid,sum(one_profit) as total")
- ->where(["one_uid"=>$uid])
- ->group("uid")
- ->order("addtime desc")
- ->limit(0,50)
- ->select()
- ->toArray();
-
-
- foreach($list as $k=>$v){
- $v['userinfo']=getUserInfo($v['uid']);
- $v['total']=NumberFormat($v['total']);
-
- $list[$k]=$v;
- }
- $this->assign("uid",$uid);
- $this->assign("token",$token);
- $this->assign("list",$list);
- return $this->fetch();
- }
- public function one_more(){
- $data = $this->request->param();
- $uid=checkNull($data['uid']);
- $token=checkNull($data['token']);
-
- $result=array(
- 'data'=>array(),
- 'nums'=>0,
- 'isscroll'=>0,
- );
-
- if(checkToken($uid,$token)==700){
- echo json_encode($result);
- return;
- }
-
-
- $p=$data['page'];
- $pnums=50;
- $start=($p-1)*$pnums;
- $Agent_profit=Db::name("user_agent_profit_recode");
-
- $list=$Agent_profit->field("uid,sum(one_profit) as total")
- ->where(["one_uid"=>$uid])
- ->group("uid")
- ->order("addtime desc")
- ->limit($start,$pnums)
- ->select()
- ->toArray();
-
-
- foreach($list as $k=>$v){
- $v['userinfo']=getUserInfo($v['uid']);
- $v['total']=NumberFormat($v['total']);
-
- $list[$k]=$v;
- }
-
- $nums=count($list);
- if($nums<$pnums){
- $isscroll=0;
- }else{
- $isscroll=1;
- }
-
- $result=array(
- 'data'=>$list,
- 'nums'=>$nums,
- 'isscroll'=>$isscroll,
- );
- echo json_encode($result);
- }
- }
|