AgentController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * 分销
  4. */
  5. namespace app\appapi\controller;
  6. use cmf\controller\HomeBaseController;
  7. use think\facade\Db;
  8. use think\db\Query;
  9. class AgentController extends HomebaseController {
  10. public function index(){
  11. $data = $this->request->param();
  12. $uid=checkNull($data['uid']);
  13. $token=checkNull($data['token']);
  14. if(checkToken($uid,$token)==700){
  15. $this->assign("reason",lang('您的登陆状态失效,请重新登陆!'));
  16. return $this->fetch(':error');
  17. }
  18. $nowtime=time();
  19. //当天0点
  20. $today=date("Ymd",$nowtime);
  21. $today_start=strtotime($today);
  22. //昨天0点
  23. $yes_start=strtotime("{$today} - 1 day");
  24. $agentinfo=array();
  25. $count=Db::name("agent")->where(['one'=>$uid])->count();
  26. $total=Db::name("votes_record")
  27. ->where(['touid'=>$uid,'action'=>1])
  28. ->where('addtime','between',[$yes_start,$today_start])
  29. ->sum("votes");
  30. if(!$total){
  31. $total=0;
  32. }
  33. $agentprofit=Db::name("agent_profit")->where(["uid"=>$uid])->find();
  34. if($agentprofit){
  35. $one_p=$agentprofit['one_p'];
  36. if(!$one_p){
  37. $one_p=0;
  38. }
  39. }else{
  40. $one_p=0;
  41. }
  42. $agnet_profit=array(
  43. 'count'=>$count,
  44. 'total'=>$total,
  45. 'one_p'=>$one_p,
  46. );
  47. $configpub=getConfigPub();
  48. $configpri=getConfigPri();
  49. $agnet_code=Db::name("user")->where("id={$uid}")->value("code");
  50. $name_votes=$configpub['name_votes'];
  51. $agent_reward=$configpri['agent_reward'];
  52. /* 是否是分销下级 */
  53. $users_agent=Db::name("agent")->where(["uid"=>$uid])->find();
  54. if($users_agent){
  55. $agentinfo= getUserInfo($users_agent['one']);
  56. }
  57. $this->assign("uid",$uid);
  58. $this->assign("token",$token);
  59. $this->assign("agnet_profit",$agnet_profit);
  60. $this->assign("agnet_code",$agnet_code);
  61. $this->assign("agent_reward",$agent_reward);
  62. $this->assign("name_votes",$name_votes);
  63. $this->assign('agentinfo', $agentinfo);
  64. return $this->fetch();
  65. }
  66. public function agent(){
  67. $data = $this->request->param();
  68. $uid=isset($data['uid']) ? $data['uid']: '';
  69. $token=isset($data['token']) ? $data['token']: '';
  70. $uid=(int)checkNull($uid);
  71. $token=checkNull($token);
  72. $checkToken=checkToken($uid,$token);
  73. if($checkToken==700){
  74. $reason=lang('您的登陆状态失效,请重新登陆!');
  75. $this->assign('reason', $reason);
  76. return $this->fetch(':error');
  77. }
  78. $agentinfo=array();
  79. $users_agent=Db::name('agent')->where(["uid"=>$uid])->find();
  80. if($users_agent){
  81. $agentinfo=getUserInfo($users_agent['one']);
  82. $code=Db::name('user')->where("id={$users_agent['one']}")->value('code');
  83. $agentinfo['code']=$code;
  84. $code_a=str_split($code);
  85. $this->assign("code_a",$code_a);
  86. }
  87. $this->assign("uid",$uid);
  88. $this->assign("token",$token);
  89. $this->assign("agentinfo",$agentinfo);
  90. return $this->fetch();
  91. }
  92. public function setAgent(){
  93. $data = $this->request->param();
  94. $uid=isset($data['uid']) ? $data['uid']: '';
  95. $token=isset($data['token']) ? $data['token']: '';
  96. $code=isset($data['code']) ? $data['code']: '';
  97. $uid=(int)checkNull($uid);
  98. $token=checkNull($token);
  99. $code=checkNull($code);
  100. $rs=array('code'=>0,'info'=>array(),'msg'=>lang('设置成功'));
  101. if(checkToken($uid,$token)==700){
  102. $rs['code']=700;
  103. $rs['msg']=lang('您的登陆状态失效,请重新登陆!');
  104. echo json_encode($rs);
  105. return;
  106. }
  107. if($code==""){
  108. $rs['code']=1001;
  109. $rs['msg']=lang('邀请码不能为空');
  110. echo json_encode($rs);
  111. return;
  112. }
  113. $isexist=Db::name('agent')->where(["uid"=>$uid])->find();
  114. if($isexist){
  115. $rs['code']=1001;
  116. $rs['msg']=lang('已设置');
  117. echo json_encode($rs);
  118. return;
  119. }
  120. $oneinfo=Db::name('user')->field("id")->where(["code"=>$code])->find();
  121. if(!$oneinfo){
  122. $rs['code']=1002;
  123. $rs['msg']=lang('邀请码错误');
  124. echo json_encode($rs);
  125. return;
  126. }
  127. if($oneinfo['id']==$uid){
  128. $rs['code']=1003;
  129. $rs['msg']=lang('不能填写自己的邀请码');
  130. echo json_encode($rs);
  131. return;
  132. }
  133. $one_agent=Db::name('agent')->where("uid={$oneinfo['id']}")->find();
  134. if(!$one_agent){
  135. $one_agent=array(
  136. 'uid'=>$oneinfo['id'],
  137. 'one'=>0,
  138. );
  139. }else{
  140. if($one_agent['one']==$uid){
  141. $rs['code']=1004;
  142. $rs['msg']=lang('您已经是该用户的上级');
  143. echo json_encode($rs);
  144. return;
  145. }
  146. }
  147. $data=array(
  148. 'uid'=>$uid,
  149. 'one'=>$one_agent['uid'],
  150. 'addtime'=>time(),
  151. );
  152. Db::name('agent')->insert($data);
  153. /* 邀请奖励 */
  154. $one_uid=$oneinfo['id'];
  155. $configPri=getConfigPri();
  156. $nowtime=time();
  157. $agent_reward=$configPri['agent_reward'];
  158. if($agent_reward){
  159. $data=[
  160. 'action'=>'1', //邀请奖励
  161. 'uid'=>$uid,
  162. 'touid'=>$one_uid,
  163. 'votes'=>$agent_reward,
  164. 'addtime'=>$nowtime,
  165. ];
  166. //写入映票收入记录
  167. setVoteRecord($data);
  168. //更新用户的映票
  169. changeUserVotes($one_uid,$agent_reward,1);
  170. $rs2= Db::name('agent_profit')
  171. ->where("uid={$one_uid}")
  172. ->find();
  173. if(!$rs2){
  174. Db::name('agent_profit')
  175. ->insert(array('uid'=> $one_uid,'one_p'=> $agent_reward));
  176. }else{
  177. Db::name('agent_profit')
  178. ->where("uid={$one_uid}")
  179. ->inc("one_p",$agent_reward)
  180. ->update();
  181. }
  182. $rs3=Db::name('agent_profit')
  183. ->where("uid={$uid}")
  184. ->find();
  185. if(!$rs3){
  186. Db::name('agent_profit')->insert(array('uid'=> $uid,'one'=> $agent_reward));
  187. }else{
  188. Db::name('agent_profit')
  189. ->where("uid={$uid}")
  190. ->inc("one",$agent_reward)
  191. ->update();
  192. }
  193. }
  194. echo json_encode($rs);
  195. }
  196. public function one(){
  197. $data = $this->request->param();
  198. $uid=checkNull($data['uid']);
  199. $token=checkNull($data['token']);
  200. if(checkToken($uid,$token)==700){
  201. $this->assign("reason",lang('您的登陆状态失效,请重新登陆!'));
  202. $this->display(':error');
  203. return;
  204. }
  205. $Agent_profit=Db::name("user_agent_profit_recode");
  206. $list=$Agent_profit
  207. ->field("uid,sum(one_profit) as total")
  208. ->where(["one_uid"=>$uid])
  209. ->group("uid")
  210. ->order("addtime desc")
  211. ->limit(0,50)
  212. ->select()
  213. ->toArray();
  214. foreach($list as $k=>$v){
  215. $v['userinfo']=getUserInfo($v['uid']);
  216. $v['total']=NumberFormat($v['total']);
  217. $list[$k]=$v;
  218. }
  219. $this->assign("uid",$uid);
  220. $this->assign("token",$token);
  221. $this->assign("list",$list);
  222. return $this->fetch();
  223. }
  224. public function one_more(){
  225. $data = $this->request->param();
  226. $uid=checkNull($data['uid']);
  227. $token=checkNull($data['token']);
  228. $result=array(
  229. 'data'=>array(),
  230. 'nums'=>0,
  231. 'isscroll'=>0,
  232. );
  233. if(checkToken($uid,$token)==700){
  234. echo json_encode($result);
  235. return;
  236. }
  237. $p=$data['page'];
  238. $pnums=50;
  239. $start=($p-1)*$pnums;
  240. $Agent_profit=Db::name("user_agent_profit_recode");
  241. $list=$Agent_profit->field("uid,sum(one_profit) as total")
  242. ->where(["one_uid"=>$uid])
  243. ->group("uid")
  244. ->order("addtime desc")
  245. ->limit($start,$pnums)
  246. ->select()
  247. ->toArray();
  248. foreach($list as $k=>$v){
  249. $v['userinfo']=getUserInfo($v['uid']);
  250. $v['total']=NumberFormat($v['total']);
  251. $list[$k]=$v;
  252. }
  253. $nums=count($list);
  254. if($nums<$pnums){
  255. $isscroll=0;
  256. }else{
  257. $isscroll=1;
  258. }
  259. $result=array(
  260. 'data'=>$list,
  261. 'nums'=>$nums,
  262. 'isscroll'=>$isscroll,
  263. );
  264. echo json_encode($result);
  265. }
  266. }