Intro.php 21 KB

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