Intro.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. $result['fenxiaodingdan'] = Db::name('pay_order')->where('user_id','IN',$tuandui_ids)->where('table_name','money_recharge')->where('order_status',1)->count();
  53. //待入账
  54. $this->success('success',$result);
  55. }
  56. //获取我的直推
  57. private function get_my_down_list(){
  58. $chuju = config('site.intro_chuju_min_money');
  59. //直推,下级,入金,等级比我低,未出局
  60. $list = Db::name('user_wallet')->alias('uw')
  61. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid')
  62. ->where('uw.intro_uid',$this->auth->id) //我邀请的
  63. ->where('uw.intro_level','neq','-1') //入金了
  64. ->where('uw.intro_level','elt',$this->wallet['intro_level']) //等级比我低
  65. ->where('uw.money','lt',$chuju) //金额小于500,出局
  66. ->select();
  67. return !empty($list) ? $list : [];
  68. }
  69. //获取我的间推
  70. private function get_my_downdown_list($down_uids = false){
  71. if($down_uids === false){
  72. $list = $this->get_my_down_list();
  73. $down_uids = array_column($list,'user_id');
  74. }
  75. $chuju = config('site.intro_chuju_min_money');
  76. //间推,下下级,入金,未出局,等级比我低,等级比自己的上级低
  77. $list2 = Db::name('user_wallet')->alias('uw')
  78. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid')
  79. ->join('user_wallet intro','uw.intro_uid = intro.user_id','LEFT') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  80. ->whereIN('uw.intro_uid',$down_uids) //下下级
  81. ->where('uw.intro_level','neq','-1') //入金了
  82. ->where('uw.money','lt',$chuju) //金额小于500,出局
  83. ->where('uw.intro_level','elt',$this->wallet['intro_level']) //等级比我低
  84. ->where('uw.intro_level <= intro.intro_level') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  85. ->select();
  86. return !empty($list2) ? $list2 : [];
  87. }
  88. //我的分销余额日志
  89. public function my_intromoney_log(){
  90. $type = input('type',0);
  91. $map = [
  92. 'user_id' => $this->auth->id,
  93. ];
  94. if($type == 1){
  95. $map['change_value'] = ['gt',0];
  96. }
  97. if($type == 2){
  98. $map['change_value'] = ['lt',0];
  99. }
  100. $list = Db::name('user_intromoney_log')
  101. ->field('id,log_type,before,change_value,remain,remark,createtime')
  102. ->where($map)->order('id desc')->autopage()->select();
  103. // $list = $this->list_appen_logtext($list);
  104. $this->success('success',$list);
  105. }
  106. //提现配置
  107. public function take_cash_config(){
  108. $plat_bilv = config('site.intro_takecash_plat_bili');
  109. $take_cash_days = config('site.intro_takecash_days');
  110. $take_cash_days_str = implode(',',$take_cash_days);
  111. $data = [
  112. 'intromoney' => model('wallet')->getwallet($this->auth->id,'intromoney'),
  113. 'plat_bilv' => $plat_bilv,
  114. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  115. 'take_cash_days' => $take_cash_days_str,
  116. 'remark' => '每月'.$take_cash_days_str.'日可以提现',
  117. 'take_cash_button' => in_array(date('d'),$take_cash_days) ? 1 : 0,
  118. ];
  119. $this->success('success',$data);
  120. }
  121. //提现before
  122. public function take_cash_before(){
  123. $freemoney = input('freemoney',0);
  124. if(!$freemoney){
  125. $this->error('请填写金额');
  126. }
  127. $money = floatval($freemoney);
  128. if($money<=0)
  129. {
  130. $this->error('金额必须大于0');
  131. }
  132. $min = config('site.intro_takecash_min_money');
  133. $max = config('site.intro_takecash_max_money');
  134. if($money < $min){
  135. $this->error('提现金额不能小于'.$min);
  136. }
  137. if($money > $max){
  138. $this->error('提现金额不能大于'.$max);
  139. }
  140. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  141. if($money > $user_money){
  142. $this->error('提现金额不能大于可提现余额');
  143. }
  144. //平台手续费
  145. $plat_bilv = config('site.intro_takecash_plat_bili');
  146. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  147. //减去手续费,得实得金额
  148. $get_money = bcsub($money,$plat_money,2);
  149. $data = [
  150. 'money' => $money,
  151. 'plat_bilv' => $plat_bilv,
  152. 'plat_money' => $plat_money,
  153. 'get_money' => $get_money,
  154. ];
  155. $this->success(1,$data);
  156. }
  157. //提现
  158. public function take_cash(){
  159. $freemoney = input('freemoney',0);
  160. // $type = input('type',1);
  161. $type = 2;
  162. if(!$freemoney){
  163. $this->error('请填写金额');
  164. }
  165. if (!in_array($type,[1,2,3])) {
  166. $this->error('未知的提现类型');
  167. }
  168. //提现日期限制
  169. $take_cash_days = config('site.intro_takecash_days');
  170. if(!in_array(date('d'),$take_cash_days)){
  171. $take_cash_days_str = implode(',',$take_cash_days);
  172. $this->error('每月'.$take_cash_days_str.'日才可以提现');
  173. }
  174. //赋值money
  175. /*if($rc_id){
  176. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  177. $money = $recharge_config['money'] ?: 0;
  178. }*/
  179. //自由输入覆盖
  180. if(!empty($freemoney)){
  181. $rc_id = 0;
  182. $money = floatval($freemoney);
  183. }
  184. //
  185. if($money<=0)
  186. {
  187. $this->error('金额必须大于0');
  188. }
  189. $min = config('site.intro_takecash_min_money');
  190. $max = config('site.intro_takecash_max_money');
  191. if($money < $min){
  192. $this->error('提现金额不能小于'.$min);
  193. }
  194. if($money > $max){
  195. $this->error('提现金额不能大于'.$max);
  196. }
  197. $check = Db::name('intro_user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  198. if($check){
  199. $this->error('您已经申请了提现,请等待审核');
  200. }
  201. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  202. if($money > $user_money){
  203. $this->error('提现金额不能大于可提现余额');
  204. }
  205. if($type == 1){
  206. $table_name = 'user_alipay';
  207. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  208. if(empty($account_json)){
  209. $this->error('未绑定对应的提现账号');
  210. }
  211. }elseif($type == 2){
  212. $table_name = 'user_bank';
  213. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  214. if(empty($account_json)){
  215. $this->error('未绑定对应的提现账号');
  216. }
  217. }elseif($type == 3){
  218. //微信支付
  219. $table_name = 'user_wechat';
  220. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  221. if(empty($account_json)){
  222. $this->error('未绑定对应的提现账号');
  223. }
  224. }
  225. //平台手续费
  226. $plat_bilv = config('site.intro_takecash_plat_bili');
  227. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  228. //减去手续费,得实得金额
  229. $get_money = bcsub($money,$plat_money,2);
  230. $data = [
  231. 'user_id' => $this->auth->id,
  232. 'money' => $money,
  233. 'plat_bilv' => $plat_bilv,
  234. 'plat_money' => $plat_money,
  235. 'get_money' => $get_money,
  236. 'type' => $type,
  237. 'acount_json' => json_encode($account_json),
  238. 'createtime' => time(),
  239. 'updatetime' => time(),
  240. 'status' => 0,
  241. ];
  242. Db::startTrans();
  243. $log_id = Db::name('intro_user_take_cash')->insertGetId($data);
  244. if(!$log_id){
  245. Db::rollback();
  246. $this->error('提现失败');
  247. }
  248. //扣除money
  249. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'intromoney',-$money,121,'提现(审核中)','intro_user_take_cash',$log_id);
  250. if($rs_wallet['status']===false)
  251. {
  252. Db::rollback();
  253. $this->error($rs_wallet['msg']);
  254. }
  255. Db::commit();
  256. $this->success('申请成功请等待审核');
  257. }
  258. //提现记录
  259. public function take_cash_log(){
  260. $list = Db::name('intro_user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  261. foreach($list as $key => &$val){
  262. $val['remark'] = '';
  263. if($val['type'] == 1){
  264. $val['remark'] = '支付宝提现';
  265. }elseif($val['type'] == 2){
  266. $val['remark'] = '银行卡提现';
  267. }else{
  268. $val['remark'] = '微信提现';
  269. }
  270. }
  271. $this->success('success',$list);
  272. }
  273. }