Intro.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 分销
  7. */
  8. class Intro extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. protected $wallet = [];//登录用户的钱包
  13. public function __construct(){
  14. parent::__construct();
  15. $this->wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  16. }
  17. //首页
  18. public function index(){
  19. $result = [
  20. 'user_id' => $this->auth->id,
  21. 'nickname' => $this->auth->nickname,
  22. 'avatar' => localpath_to_netpath($this->auth->avatar),
  23. 'intro_level' => $this->wallet['intro_level'],
  24. ];
  25. //累计佣金
  26. $result['leijiyongjin'] = Db::name('user_intromoney_log')->where('user_id',$this->auth->id)->where('log_type','IN',[101,102,111])->sum('change_value');
  27. //下线总数
  28. $zhitui_ids = Db::name('user_wallet')->where('intro_uid',$this->auth->id)->column('user_id');//直推
  29. $jiantui_ids = Db::name('user_wallet')->where('intro_uid','IN',$zhitui_ids)->column('user_id');//间推
  30. $result['xiaxianzongshu'] = count($zhitui_ids) + count($jiantui_ids);
  31. //可提现金额
  32. $result['ketixian'] = $this->wallet['intromoney'];
  33. //待入账金额,下面计算
  34. $result['dairuzhang'] = 0;
  35. //提现中
  36. $result['tixianzhong'] = Db::name('intro_user_take_cash')->where('user_id',$this->auth->id)->where('status',0)->sum('money');
  37. //已提现
  38. $result['yitixian'] = Db::name('intro_user_take_cash')->where('user_id',$this->auth->id)->where('status',1)->sum('money');
  39. //直推
  40. $list = $this->get_my_down_list();
  41. $down_uids = array_column($list,'user_id');
  42. //间推
  43. $list2 = $this->get_my_downdown_list($down_uids);
  44. $down_down_uids = array_column($list2,'user_id');
  45. //团队ids
  46. $tuandui_ids = array_merge($down_uids,$down_down_uids);
  47. //我的团队人数
  48. $result['wodetuandui'] = count($tuandui_ids);
  49. //邀请好友,直推人数
  50. $result['yaoqinghaoyou'] = count($down_uids);
  51. //分销订单
  52. $tuandui_ids[] = $this->auth->id;
  53. $result['fenxiaodingdan'] = Db::name('pay_order')->where('user_id','IN',$tuandui_ids)->where('table_name','money_recharge')->where('order_status',1)->whereTime('createtime','month')->count();
  54. //团队总业绩
  55. $result['tuanduizongyeji'] = Db::name('pay_order')->where('user_id','IN',$tuandui_ids)->where('table_name','money_recharge')->where('order_status',1)->whereTime('createtime','month')->sum('order_amount');
  56. //我所在等级的比例
  57. $bili = Db::name('intro_level_config')->where('id',$this->wallet['intro_level'])->value('intro_bili');
  58. $bili = $bili ?: 0;
  59. //待入账
  60. $result['dairuzhang'] = bcdiv(bcmul($result['tuanduizongyeji'],$bili,2),100,2);
  61. //我的佣金余额
  62. $result['intromoney'] = $this->wallet['intromoney'];
  63. $this->success('success',$result);
  64. }
  65. //获取我的直推
  66. private function get_my_down_list(){
  67. $chuju = config('site.intro_chuju_min_money');
  68. //直推,下级,入金,等级比我低,未出局
  69. $list = Db::name('user_wallet')->alias('uw')
  70. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid')
  71. ->where('uw.intro_uid',$this->auth->id) //我邀请的
  72. ->where('uw.intro_level','neq','-1') //入金了
  73. ->where('uw.intro_level','elt',$this->wallet['intro_level']) //等级比我低
  74. ->where('uw.money','egt',$chuju) //金额小于500,出局
  75. ->select();
  76. return !empty($list) ? $list : [];
  77. }
  78. //获取我的间推
  79. private function get_my_downdown_list($down_uids = false){
  80. if($down_uids === false){
  81. $list = $this->get_my_down_list();
  82. $down_uids = array_column($list,'user_id');
  83. }
  84. $chuju = config('site.intro_chuju_min_money');
  85. //间推,下下级,入金,未出局,等级比我低,等级比自己的上级低
  86. $list2 = Db::name('user_wallet')->alias('uw')
  87. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid')
  88. ->join('user_wallet intro','uw.intro_uid = intro.user_id','LEFT') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  89. ->whereIN('uw.intro_uid',$down_uids) //下下级
  90. ->where('uw.intro_level','neq','-1') //入金了
  91. ->where('uw.money','egt',$chuju) //金额小于500,出局
  92. ->where('uw.intro_level','elt',$this->wallet['intro_level']) //等级比我低
  93. ->where('uw.intro_level <= intro.intro_level') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  94. ->select();
  95. return !empty($list2) ? $list2 : [];
  96. }
  97. //分销订单列表
  98. public function fenxiao_order_list(){
  99. //直推
  100. $list = $this->get_my_down_list();
  101. $down_uids = array_column($list,'user_id');
  102. //间推
  103. $list2 = $this->get_my_downdown_list($down_uids);
  104. $down_down_uids = array_column($list2,'user_id');
  105. //团队ids
  106. $tuandui_ids = array_merge($down_uids,$down_down_uids);
  107. //加上我自己
  108. $tuandui_ids[] = $this->auth->id;
  109. //我所在等级的比例
  110. $bili = Db::name('intro_level_config')->where('id',$this->wallet['intro_level'])->value('intro_bili');
  111. $bili = $bili ?: 0;
  112. //分销订单
  113. $list = Db::name('pay_order')->alias('o')->field('o.id,user.id as user_id,user.nickname,user.avatar,user.mobile,o.createtime,o.order_amount')
  114. ->join('user','o.user_id = user.id','LEFT')
  115. ->where('o.user_id','IN',$tuandui_ids)
  116. ->where('o.table_name','money_recharge')
  117. ->where('o.order_status',1)
  118. ->order('o.id desc')->autopage()->select();
  119. foreach($list as $key => $val){
  120. $val['yujiyongjin'] = bcdiv(bcmul($val['order_amount'],$bili,2),100,2);
  121. unset($val['order_amount']);
  122. $list[$key] = $val;
  123. }
  124. $this->success(1,$list);
  125. }
  126. //我的团队
  127. public function my_team(){
  128. $type = input('type',1);
  129. $keyword = input('keyword','');
  130. $wheresearch = [];
  131. if(!empty($keyword)){
  132. $wheresearch['user.nickname'] = ['LIKE','%'.$keyword.'%'];
  133. }
  134. //普通成员
  135. if($type == 1){
  136. //普通直推
  137. $zhitui_list = Db::name('user_wallet')->alias('uw')
  138. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid,user.nickname')
  139. ->join('user','uw.user_id = user.id','LEFT')
  140. ->where('uw.intro_uid',$this->auth->id) //我邀请的
  141. ->where('uw.intro_level','-1') //没入金
  142. ->where($wheresearch)
  143. ->select();
  144. foreach($zhitui_list as $key => $val){
  145. $val['show_level'] = '直推普通成员';
  146. $val['tuanduiyeji'] = 0;
  147. $zhitui_list[$key] = $val;
  148. }
  149. $zhitui_ids = Db::name('user_wallet')->where('intro_uid',$this->auth->id)->column('user_id');//直推id。这里单独查不用上面的,是因为可能直推入金了,间推没入金,间推不能给丢了
  150. $jiantui_list = Db::name('user_wallet')->alias('uw')
  151. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid,user.nickname')
  152. ->join('user','uw.user_id = user.id','LEFT')
  153. ->whereIN('uw.intro_uid',$zhitui_ids)//下下级
  154. ->where('uw.intro_level','-1') //没入金
  155. ->where($wheresearch)
  156. ->select();
  157. foreach($jiantui_list as $key => $val){
  158. $val['show_level'] = '间推普通成员';
  159. $val['tuanduiyeji'] = 0;
  160. $jiantui_list[$key] = $val;
  161. }
  162. $result = array_merge($zhitui_list,$jiantui_list);
  163. $this->success(1,$result);
  164. }
  165. //一级成员
  166. if($type == 2){
  167. //出局
  168. $chuju = config('site.intro_chuju_min_money');
  169. //直推,下级,入金,等级比我低,未出局
  170. $list = Db::name('user_wallet')->alias('uw')
  171. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid,user.nickname')
  172. ->join('user','uw.user_id = user.id','LEFT')
  173. ->where('uw.intro_uid',$this->auth->id) //我邀请的
  174. ->where('uw.intro_level','neq','-1') //入金了
  175. ->where('uw.intro_level','elt',$this->wallet['intro_level']) //等级比我低
  176. ->where('uw.money','egt',$chuju) //金额小于500,出局
  177. ->where($wheresearch)
  178. ->select();
  179. $down_uids = array_column($list,'user_id');
  180. $pay_order = Db::name('pay_order')->field('user_id,sum(order_amount) as yeji')
  181. ->where('user_id','IN',$down_uids)->where('table_name','money_recharge')->where('order_status',1)->whereTime('createtime','month')
  182. ->group('user_id')->select();
  183. foreach($list as $key => $val){
  184. $val['show_level'] = '一级成员';
  185. $val['tuanduiyeji'] = '0.00';
  186. foreach($pay_order as $k => $v){
  187. if($val['user_id'] == $v['user_id']){
  188. $val['tuanduiyeji'] = $v['yeji'];
  189. }
  190. }
  191. $list[$key] = $val;
  192. }
  193. $this->success(1,$list);
  194. }
  195. //二级成员
  196. if($type == 3){
  197. //出局
  198. $chuju = config('site.intro_chuju_min_money');
  199. //直推,下级,入金,等级比我低,未出局
  200. $list = $this->get_my_down_list();
  201. $down_uids = array_column($list,'user_id');
  202. //间推,下下级,入金,未出局,等级比我低,等级比自己的上级低
  203. $list2 = Db::name('user_wallet')->alias('uw')
  204. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid,user.nickname')
  205. ->join('user','uw.user_id = user.id','LEFT')
  206. ->join('user_wallet intro','uw.intro_uid = intro.user_id','LEFT') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  207. ->whereIN('uw.intro_uid',$down_uids) //下下级
  208. ->where('uw.intro_level','neq','-1') //入金了
  209. ->where('uw.money','egt',$chuju) //金额小于500,出局
  210. ->where('uw.intro_level','elt',$this->wallet['intro_level']) //等级比我低
  211. ->where('uw.intro_level <= intro.intro_level') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  212. ->where($wheresearch)
  213. ->select();
  214. $down_down_uids = array_column($list2,'user_id');
  215. $pay_order = Db::name('pay_order')->field('user_id,sum(order_amount) as yeji')
  216. ->where('user_id','IN',$down_down_uids)->where('table_name','money_recharge')->where('order_status',1)->whereTime('createtime','month')
  217. ->group('user_id')->select();
  218. foreach($list2 as $key => $val){
  219. $val['show_level'] = '二级成员';
  220. $val['tuanduiyeji'] = '0.00';
  221. foreach($pay_order as $k => $v){
  222. if($val['user_id'] == $v['user_id']){
  223. $val['tuanduiyeji'] = $v['yeji'];
  224. }
  225. }
  226. $list[$key] = $val;
  227. }
  228. $this->success(1,$list);
  229. }
  230. }
  231. //
  232. //我的分销余额日志
  233. public function my_intromoney_log(){
  234. $type = input('type',0);
  235. $map = [
  236. 'user_id' => $this->auth->id,
  237. ];
  238. if($type == 1){
  239. $map['change_value'] = ['gt',0];
  240. }
  241. if($type == 2){
  242. $map['change_value'] = ['lt',0];
  243. }
  244. $list = Db::name('user_intromoney_log')
  245. ->field('id,log_type,before,change_value,remain,remark,createtime')
  246. ->where($map)->order('id desc')->autopage()->select();
  247. // $list = $this->list_appen_logtext($list);
  248. $this->success('success',$list);
  249. }
  250. //提现配置
  251. public function take_cash_config(){
  252. $plat_bilv = config('site.intro_takecash_plat_bili');
  253. $take_cash_days = config('site.intro_takecash_days');
  254. $take_cash_days_str = implode(',',$take_cash_days);
  255. $data = [
  256. 'intromoney' => model('wallet')->getwallet($this->auth->id,'intromoney'),
  257. 'plat_bilv' => $plat_bilv,
  258. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  259. 'take_cash_days' => $take_cash_days_str,
  260. 'remark' => '每月'.$take_cash_days_str.'日可以提现',
  261. 'take_cash_button' => in_array(date('d'),$take_cash_days) ? 1 : 0,
  262. ];
  263. $this->success('success',$data);
  264. }
  265. //提现before
  266. public function take_cash_before(){
  267. $freemoney = input('freemoney',0);
  268. if(!$freemoney){
  269. $this->error('请填写金额');
  270. }
  271. $money = floatval($freemoney);
  272. if($money<=0)
  273. {
  274. $this->error('金额必须大于0');
  275. }
  276. $min = config('site.intro_takecash_min_money');
  277. $max = config('site.intro_takecash_max_money');
  278. if($money < $min){
  279. $this->error('提现金额不能小于'.$min);
  280. }
  281. if($money > $max){
  282. $this->error('提现金额不能大于'.$max);
  283. }
  284. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  285. if($money > $user_money){
  286. $this->error('提现金额不能大于可提现余额');
  287. }
  288. //平台手续费
  289. $plat_bilv = config('site.intro_takecash_plat_bili');
  290. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  291. //减去手续费,得实得金额
  292. $get_money = bcsub($money,$plat_money,2);
  293. $data = [
  294. 'money' => $money,
  295. 'plat_bilv' => $plat_bilv,
  296. 'plat_money' => $plat_money,
  297. 'get_money' => $get_money,
  298. ];
  299. $this->success(1,$data);
  300. }
  301. //提现
  302. public function take_cash(){
  303. $freemoney = input('freemoney',0);
  304. // $type = input('type',1);
  305. $type = 2;
  306. if(!$freemoney){
  307. $this->error('请填写金额');
  308. }
  309. if (!in_array($type,[1,2,3])) {
  310. $this->error('未知的提现类型');
  311. }
  312. //提现日期限制
  313. $take_cash_days = config('site.intro_takecash_days');
  314. if(!in_array(date('d'),$take_cash_days)){
  315. $take_cash_days_str = implode(',',$take_cash_days);
  316. $this->error('每月'.$take_cash_days_str.'日才可以提现');
  317. }
  318. //赋值money
  319. /*if($rc_id){
  320. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  321. $money = $recharge_config['money'] ?: 0;
  322. }*/
  323. //自由输入覆盖
  324. if(!empty($freemoney)){
  325. $rc_id = 0;
  326. $money = floatval($freemoney);
  327. }
  328. //
  329. if($money<=0)
  330. {
  331. $this->error('金额必须大于0');
  332. }
  333. $min = config('site.intro_takecash_min_money');
  334. $max = config('site.intro_takecash_max_money');
  335. if($money < $min){
  336. $this->error('提现金额不能小于'.$min);
  337. }
  338. if($money > $max){
  339. $this->error('提现金额不能大于'.$max);
  340. }
  341. $check = Db::name('intro_user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  342. if($check){
  343. $this->error('您已经申请了提现,请等待审核');
  344. }
  345. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  346. if($money > $user_money){
  347. $this->error('提现金额不能大于可提现余额');
  348. }
  349. if($type == 1){
  350. $table_name = 'user_alipay';
  351. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  352. if(empty($account_json)){
  353. $this->error('未绑定对应的提现账号');
  354. }
  355. }elseif($type == 2){
  356. $table_name = 'user_bank';
  357. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  358. if(empty($account_json)){
  359. $this->error('未绑定对应的提现账号');
  360. }
  361. }elseif($type == 3){
  362. //微信支付
  363. $table_name = 'user_wechat';
  364. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  365. if(empty($account_json)){
  366. $this->error('未绑定对应的提现账号');
  367. }
  368. }
  369. //平台手续费
  370. $plat_bilv = config('site.intro_takecash_plat_bili');
  371. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  372. //减去手续费,得实得金额
  373. $get_money = bcsub($money,$plat_money,2);
  374. $data = [
  375. 'user_id' => $this->auth->id,
  376. 'money' => $money,
  377. 'plat_bilv' => $plat_bilv,
  378. 'plat_money' => $plat_money,
  379. 'get_money' => $get_money,
  380. 'type' => $type,
  381. 'acount_json' => json_encode($account_json),
  382. 'createtime' => time(),
  383. 'updatetime' => time(),
  384. 'status' => 0,
  385. ];
  386. Db::startTrans();
  387. $log_id = Db::name('intro_user_take_cash')->insertGetId($data);
  388. if(!$log_id){
  389. Db::rollback();
  390. $this->error('提现失败');
  391. }
  392. //扣除money
  393. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'intromoney',-$money,121,'提现(审核中)','intro_user_take_cash',$log_id);
  394. if($rs_wallet['status']===false)
  395. {
  396. Db::rollback();
  397. $this->error($rs_wallet['msg']);
  398. }
  399. Db::commit();
  400. $this->success('申请成功请等待审核');
  401. }
  402. //提现记录
  403. public function take_cash_log(){
  404. $list = Db::name('intro_user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  405. foreach($list as $key => &$val){
  406. $val['remark'] = '';
  407. if($val['type'] == 1){
  408. $val['remark'] = '支付宝提现';
  409. }elseif($val['type'] == 2){
  410. $val['remark'] = '银行卡提现';
  411. }else{
  412. $val['remark'] = '微信提现';
  413. }
  414. }
  415. $this->success('success',$list);
  416. }
  417. }