Intro.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. public function __construct(){
  13. parent::__construct();
  14. }
  15. //首页
  16. public function index(){
  17. $user = Db::name('user')->field('id,avatar,nickname')->where('id',$this->auth->id)->find();
  18. $wallet = Db::name('user_wallet')->field('intro_level')->where('user_id',$this->auth->id)->find();
  19. //团队总业绩
  20. $chuju = config('site.intro_chuju_min_money');
  21. //下级
  22. $list = Db::name('user_wallet')->alias('uw')
  23. ->field('user.id,user.nickname,user.avatar,uw.intro_level')
  24. ->join('user','uw.user_id = user.id','LEFT')
  25. ->where('uw.intro_uid',$this->auth->id) //我邀请的
  26. ->where('uw.intro_level','neq','-1') //入金了
  27. ->where('uw.intro_level','elt',$wallet['intro_level']) //等级比我低
  28. // ->where('uw.money','lt',$chuju) //金额小于500,出局
  29. ->select();
  30. dump($list);
  31. $down_uids = array_column($list,'id');
  32. //下下级
  33. $list2 = Db::name('user_wallet')->alias('uw')
  34. ->field('user.id,user.nickname,user.avatar,uw.intro_level,intro.id as intro_id,intro.intro_level as kkk')
  35. ->join('user','uw.user_id = user.id','LEFT')
  36. ->join('user_wallet intro','uw.intro_uid = intro.user_id','LEFT') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  37. ->whereIN('uw.intro_uid',$down_uids) //下下级
  38. ->where('uw.intro_level','neq','-1') //入金了
  39. ->where('uw.intro_level','elt',$wallet['intro_level']) //等级比我低
  40. ->where('uw.intro_level <= intro.intro_level') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  41. ->select(false);
  42. dump($list2);
  43. $down_down_uids = array_column($list2,'id');
  44. // $this->success('success',$list);
  45. }
  46. //我的分销余额日志
  47. public function my_intromoney_log(){
  48. $type = input('type',0);
  49. $map = [
  50. 'user_id' => $this->auth->id,
  51. ];
  52. if($type == 1){
  53. $map['change_value'] = ['gt',0];
  54. }
  55. if($type == 2){
  56. $map['change_value'] = ['lt',0];
  57. }
  58. $list = Db::name('user_intromoney_log')
  59. ->field('id,log_type,before,change_value,remain,remark,createtime')
  60. ->where($map)->order('id desc')->autopage()->select();
  61. // $list = $this->list_appen_logtext($list);
  62. $this->success('success',$list);
  63. }
  64. //提现配置
  65. public function take_cash_config(){
  66. $plat_bilv = config('site.intro_takecash_plat_bili');
  67. $take_cash_days = config('site.intro_takecash_days');
  68. $take_cash_days_str = implode(',',$take_cash_days);
  69. $data = [
  70. 'intromoney' => model('wallet')->getwallet($this->auth->id,'intromoney'),
  71. 'plat_bilv' => $plat_bilv,
  72. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  73. 'take_cash_days' => $take_cash_days_str,
  74. 'remark' => '每月'.$take_cash_days_str.'日可以提现',
  75. 'take_cash_button' => in_array(date('d'),$take_cash_days) ? 1 : 0,
  76. ];
  77. $this->success('success',$data);
  78. }
  79. //提现before
  80. public function take_cash_before(){
  81. $freemoney = input('freemoney',0);
  82. if(!$freemoney){
  83. $this->error('请填写金额');
  84. }
  85. $money = floatval($freemoney);
  86. if($money<=0)
  87. {
  88. $this->error('金额必须大于0');
  89. }
  90. $min = config('site.intro_takecash_min_money');
  91. $max = config('site.intro_takecash_max_money');
  92. if($money < $min){
  93. $this->error('提现金额不能小于'.$min);
  94. }
  95. if($money > $max){
  96. $this->error('提现金额不能大于'.$max);
  97. }
  98. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  99. if($money > $user_money){
  100. $this->error('提现金额不能大于可提现余额');
  101. }
  102. //平台手续费
  103. $plat_bilv = config('site.intro_takecash_plat_bili');
  104. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  105. //减去手续费,得实得金额
  106. $get_money = bcsub($money,$plat_money,2);
  107. $data = [
  108. 'money' => $money,
  109. 'plat_bilv' => $plat_bilv,
  110. 'plat_money' => $plat_money,
  111. 'get_money' => $get_money,
  112. ];
  113. $this->success(1,$data);
  114. }
  115. //提现
  116. public function take_cash(){
  117. $freemoney = input('freemoney',0);
  118. // $type = input('type',1);
  119. $type = 2;
  120. if(!$freemoney){
  121. $this->error('请填写金额');
  122. }
  123. if (!in_array($type,[1,2,3])) {
  124. $this->error('未知的提现类型');
  125. }
  126. //提现日期限制
  127. $take_cash_days = config('site.intro_takecash_days');
  128. if(!in_array(date('d'),$take_cash_days)){
  129. $take_cash_days_str = implode(',',$take_cash_days);
  130. $this->error('每月'.$take_cash_days_str.'日才可以提现');
  131. }
  132. //赋值money
  133. /*if($rc_id){
  134. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  135. $money = $recharge_config['money'] ?: 0;
  136. }*/
  137. //自由输入覆盖
  138. if(!empty($freemoney)){
  139. $rc_id = 0;
  140. $money = floatval($freemoney);
  141. }
  142. //
  143. if($money<=0)
  144. {
  145. $this->error('金额必须大于0');
  146. }
  147. $min = config('site.intro_takecash_min_money');
  148. $max = config('site.intro_takecash_max_money');
  149. if($money < $min){
  150. $this->error('提现金额不能小于'.$min);
  151. }
  152. if($money > $max){
  153. $this->error('提现金额不能大于'.$max);
  154. }
  155. $check = Db::name('intro_user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  156. if($check){
  157. $this->error('您已经申请了提现,请等待审核');
  158. }
  159. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  160. if($money > $user_money){
  161. $this->error('提现金额不能大于可提现余额');
  162. }
  163. if($type == 1){
  164. $table_name = 'user_alipay';
  165. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  166. if(empty($account_json)){
  167. $this->error('未绑定对应的提现账号');
  168. }
  169. }elseif($type == 2){
  170. $table_name = 'user_bank';
  171. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  172. if(empty($account_json)){
  173. $this->error('未绑定对应的提现账号');
  174. }
  175. }elseif($type == 3){
  176. //微信支付
  177. $table_name = 'user_wechat';
  178. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  179. if(empty($account_json)){
  180. $this->error('未绑定对应的提现账号');
  181. }
  182. }
  183. //平台手续费
  184. $plat_bilv = config('site.intro_takecash_plat_bili');
  185. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  186. //减去手续费,得实得金额
  187. $get_money = bcsub($money,$plat_money,2);
  188. $data = [
  189. 'user_id' => $this->auth->id,
  190. 'money' => $money,
  191. 'plat_bilv' => $plat_bilv,
  192. 'plat_money' => $plat_money,
  193. 'get_money' => $get_money,
  194. 'type' => $type,
  195. 'acount_json' => json_encode($account_json),
  196. 'createtime' => time(),
  197. 'updatetime' => time(),
  198. 'status' => 0,
  199. ];
  200. Db::startTrans();
  201. $log_id = Db::name('intro_user_take_cash')->insertGetId($data);
  202. if(!$log_id){
  203. Db::rollback();
  204. $this->error('提现失败');
  205. }
  206. //扣除money
  207. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'intromoney',-$money,121,'提现(审核中)','intro_user_take_cash',$log_id);
  208. if($rs_wallet['status']===false)
  209. {
  210. Db::rollback();
  211. $this->error($rs_wallet['msg']);
  212. }
  213. Db::commit();
  214. $this->success('申请成功请等待审核');
  215. }
  216. //提现记录
  217. public function take_cash_log(){
  218. $list = Db::name('intro_user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  219. foreach($list as $key => &$val){
  220. $val['remark'] = '';
  221. if($val['type'] == 1){
  222. $val['remark'] = '支付宝提现';
  223. }elseif($val['type'] == 2){
  224. $val['remark'] = '银行卡提现';
  225. }else{
  226. $val['remark'] = '微信提现';
  227. }
  228. }
  229. $this->success('success',$list);
  230. }
  231. }