Intro.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. //转余额。减少intromoney ,充值到 money,走一遍notify
  232. public function transform(){
  233. $freemoney = input('freemoney',0);
  234. if(!$freemoney){
  235. $this->error('请填写金额');
  236. }
  237. $money = floatval($freemoney);
  238. $min = 1;
  239. if($money < $min){
  240. $this->error('转换金额不能小于'.$min);
  241. }
  242. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  243. if($money > $user_money){
  244. $this->error('转换金额不能大于可提现余额');
  245. }
  246. Db::startTrans();
  247. //扣除money
  248. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'intromoney',-$money,115,'转为充值余额');
  249. if($rs_wallet['status']===false)
  250. {
  251. Db::rollback();
  252. $this->error($rs_wallet['msg']);
  253. }
  254. //创建充值订单
  255. $data = [];
  256. $data['user_id'] = $this->auth->id;
  257. $data['out_trade_no'] = createUniqueNo('TP',$this->auth->id); // 数据库订单号加密
  258. $data['order_amount'] = $money;
  259. $data['createtime'] = time();
  260. $data['pay_type'] = 'transform'; //区别正常充值,唯一字段
  261. $data['platform'] = '';
  262. $data['order_status'] = 0;
  263. $data['table_name'] = 'money_recharge';
  264. $data['table_id'] = 0;
  265. $data['args'] = '';
  266. $orderid = Db::name('pay_order')->insertGetId($data);
  267. if(!$orderid){
  268. Db::rollback();
  269. $this->error('转入失败');
  270. }
  271. Db::commit();
  272. //立刻开始回调
  273. $notify = controller('Notify')->recharge_notify_do($data['out_trade_no'],15,'佣金转入充值');
  274. if(!$notify){
  275. $this->error('充值失败,请联系客服');
  276. }
  277. $this->success('操作成功');
  278. }
  279. //我的分销余额日志
  280. public function my_intromoney_log(){
  281. $type = input('type',0);
  282. $map = [
  283. 'user_id' => $this->auth->id,
  284. ];
  285. if($type == 1){
  286. $map['change_value'] = ['gt',0];
  287. }
  288. if($type == 2){
  289. $map['change_value'] = ['lt',0];
  290. }
  291. $list = Db::name('user_intromoney_log')
  292. ->field('id,log_type,before,change_value,remain,remark,createtime')
  293. ->where($map)->order('id desc')->autopage()->select();
  294. // $list = $this->list_appen_logtext($list);
  295. $this->success('success',$list);
  296. }
  297. //提现配置
  298. public function take_cash_config(){
  299. $plat_bilv = config('site.intro_takecash_plat_bili');
  300. $take_cash_days = config('site.intro_takecash_days');
  301. $take_cash_days_str = implode(',',$take_cash_days);
  302. $data = [
  303. 'intromoney' => model('wallet')->getwallet($this->auth->id,'intromoney'),
  304. 'plat_bilv' => $plat_bilv,
  305. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  306. 'take_cash_days' => $take_cash_days_str,
  307. 'remark' => '每月'.$take_cash_days_str.'日可以提现',
  308. 'take_cash_button' => in_array(date('d'),$take_cash_days) ? 1 : 0,
  309. ];
  310. $this->success('success',$data);
  311. }
  312. //提现before
  313. public function take_cash_before(){
  314. $freemoney = input('freemoney',0);
  315. if(!$freemoney){
  316. $this->error('请填写金额');
  317. }
  318. $money = floatval($freemoney);
  319. if($money<=0)
  320. {
  321. $this->error('金额必须大于0');
  322. }
  323. $min = config('site.intro_takecash_min_money');
  324. $max = config('site.intro_takecash_max_money');
  325. if($money < $min){
  326. $this->error('提现金额不能小于'.$min);
  327. }
  328. if($money > $max){
  329. $this->error('提现金额不能大于'.$max);
  330. }
  331. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  332. if($money > $user_money){
  333. $this->error('提现金额不能大于可提现余额');
  334. }
  335. //平台手续费
  336. $plat_bilv = config('site.intro_takecash_plat_bili');
  337. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  338. //减去手续费,得实得金额
  339. $get_money = bcsub($money,$plat_money,2);
  340. $data = [
  341. 'money' => $money,
  342. 'plat_bilv' => $plat_bilv,
  343. 'plat_money' => $plat_money,
  344. 'get_money' => $get_money,
  345. ];
  346. $this->success(1,$data);
  347. }
  348. //提现
  349. public function take_cash(){
  350. $freemoney = input('freemoney',0);
  351. // $type = input('type',1);
  352. $type = 2;
  353. if(!$freemoney){
  354. $this->error('请填写金额');
  355. }
  356. if (!in_array($type,[1,2,3])) {
  357. $this->error('未知的提现类型');
  358. }
  359. //提现日期限制
  360. $take_cash_days = config('site.intro_takecash_days');
  361. if(!in_array(date('d'),$take_cash_days)){
  362. $take_cash_days_str = implode(',',$take_cash_days);
  363. $this->error('每月'.$take_cash_days_str.'日才可以提现');
  364. }
  365. //赋值money
  366. /*if($rc_id){
  367. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  368. $money = $recharge_config['money'] ?: 0;
  369. }*/
  370. //自由输入覆盖
  371. if(!empty($freemoney)){
  372. $rc_id = 0;
  373. $money = floatval($freemoney);
  374. }
  375. //
  376. if($money<=0)
  377. {
  378. $this->error('金额必须大于0');
  379. }
  380. $min = config('site.intro_takecash_min_money');
  381. $max = config('site.intro_takecash_max_money');
  382. if($money < $min){
  383. $this->error('提现金额不能小于'.$min);
  384. }
  385. if($money > $max){
  386. $this->error('提现金额不能大于'.$max);
  387. }
  388. $check = Db::name('intro_user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  389. if($check){
  390. $this->error('您已经申请了提现,请等待审核');
  391. }
  392. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  393. if($money > $user_money){
  394. $this->error('提现金额不能大于可提现余额');
  395. }
  396. if($type == 1){
  397. $table_name = 'user_alipay';
  398. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  399. if(empty($account_json)){
  400. $this->error('未绑定对应的提现账号');
  401. }
  402. }elseif($type == 2){
  403. $table_name = 'user_bank';
  404. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  405. if(empty($account_json)){
  406. $this->error('未绑定对应的提现账号');
  407. }
  408. }elseif($type == 3){
  409. //微信支付
  410. $table_name = 'user_wechat';
  411. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  412. if(empty($account_json)){
  413. $this->error('未绑定对应的提现账号');
  414. }
  415. }
  416. //平台手续费
  417. $plat_bilv = config('site.intro_takecash_plat_bili');
  418. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  419. //减去手续费,得实得金额
  420. $get_money = bcsub($money,$plat_money,2);
  421. $data = [
  422. 'user_id' => $this->auth->id,
  423. 'money' => $money,
  424. 'plat_bilv' => $plat_bilv,
  425. 'plat_money' => $plat_money,
  426. 'get_money' => $get_money,
  427. 'type' => $type,
  428. 'acount_json' => json_encode($account_json),
  429. 'createtime' => time(),
  430. 'updatetime' => time(),
  431. 'status' => 0,
  432. ];
  433. Db::startTrans();
  434. $log_id = Db::name('intro_user_take_cash')->insertGetId($data);
  435. if(!$log_id){
  436. Db::rollback();
  437. $this->error('提现失败');
  438. }
  439. //扣除money
  440. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'intromoney',-$money,121,'提现(审核中)','intro_user_take_cash',$log_id);
  441. if($rs_wallet['status']===false)
  442. {
  443. Db::rollback();
  444. $this->error($rs_wallet['msg']);
  445. }
  446. Db::commit();
  447. $this->success('申请成功请等待审核');
  448. }
  449. //提现记录
  450. public function take_cash_log(){
  451. $list = Db::name('intro_user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  452. foreach($list as $key => &$val){
  453. $val['remark'] = '';
  454. if($val['type'] == 1){
  455. $val['remark'] = '支付宝提现';
  456. }elseif($val['type'] == 2){
  457. $val['remark'] = '银行卡提现';
  458. }else{
  459. $val['remark'] = '微信提现';
  460. }
  461. }
  462. $this->success('success',$list);
  463. }
  464. }