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