Intro.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. }
  129. //
  130. //我的分销余额日志
  131. public function my_intromoney_log(){
  132. $type = input('type',0);
  133. $map = [
  134. 'user_id' => $this->auth->id,
  135. ];
  136. if($type == 1){
  137. $map['change_value'] = ['gt',0];
  138. }
  139. if($type == 2){
  140. $map['change_value'] = ['lt',0];
  141. }
  142. $list = Db::name('user_intromoney_log')
  143. ->field('id,log_type,before,change_value,remain,remark,createtime')
  144. ->where($map)->order('id desc')->autopage()->select();
  145. // $list = $this->list_appen_logtext($list);
  146. $this->success('success',$list);
  147. }
  148. //提现配置
  149. public function take_cash_config(){
  150. $plat_bilv = config('site.intro_takecash_plat_bili');
  151. $take_cash_days = config('site.intro_takecash_days');
  152. $take_cash_days_str = implode(',',$take_cash_days);
  153. $data = [
  154. 'intromoney' => model('wallet')->getwallet($this->auth->id,'intromoney'),
  155. 'plat_bilv' => $plat_bilv,
  156. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  157. 'take_cash_days' => $take_cash_days_str,
  158. 'remark' => '每月'.$take_cash_days_str.'日可以提现',
  159. 'take_cash_button' => in_array(date('d'),$take_cash_days) ? 1 : 0,
  160. ];
  161. $this->success('success',$data);
  162. }
  163. //提现before
  164. public function take_cash_before(){
  165. $freemoney = input('freemoney',0);
  166. if(!$freemoney){
  167. $this->error('请填写金额');
  168. }
  169. $money = floatval($freemoney);
  170. if($money<=0)
  171. {
  172. $this->error('金额必须大于0');
  173. }
  174. $min = config('site.intro_takecash_min_money');
  175. $max = config('site.intro_takecash_max_money');
  176. if($money < $min){
  177. $this->error('提现金额不能小于'.$min);
  178. }
  179. if($money > $max){
  180. $this->error('提现金额不能大于'.$max);
  181. }
  182. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  183. if($money > $user_money){
  184. $this->error('提现金额不能大于可提现余额');
  185. }
  186. //平台手续费
  187. $plat_bilv = config('site.intro_takecash_plat_bili');
  188. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  189. //减去手续费,得实得金额
  190. $get_money = bcsub($money,$plat_money,2);
  191. $data = [
  192. 'money' => $money,
  193. 'plat_bilv' => $plat_bilv,
  194. 'plat_money' => $plat_money,
  195. 'get_money' => $get_money,
  196. ];
  197. $this->success(1,$data);
  198. }
  199. //提现
  200. public function take_cash(){
  201. $freemoney = input('freemoney',0);
  202. // $type = input('type',1);
  203. $type = 2;
  204. if(!$freemoney){
  205. $this->error('请填写金额');
  206. }
  207. if (!in_array($type,[1,2,3])) {
  208. $this->error('未知的提现类型');
  209. }
  210. //提现日期限制
  211. $take_cash_days = config('site.intro_takecash_days');
  212. if(!in_array(date('d'),$take_cash_days)){
  213. $take_cash_days_str = implode(',',$take_cash_days);
  214. $this->error('每月'.$take_cash_days_str.'日才可以提现');
  215. }
  216. //赋值money
  217. /*if($rc_id){
  218. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  219. $money = $recharge_config['money'] ?: 0;
  220. }*/
  221. //自由输入覆盖
  222. if(!empty($freemoney)){
  223. $rc_id = 0;
  224. $money = floatval($freemoney);
  225. }
  226. //
  227. if($money<=0)
  228. {
  229. $this->error('金额必须大于0');
  230. }
  231. $min = config('site.intro_takecash_min_money');
  232. $max = config('site.intro_takecash_max_money');
  233. if($money < $min){
  234. $this->error('提现金额不能小于'.$min);
  235. }
  236. if($money > $max){
  237. $this->error('提现金额不能大于'.$max);
  238. }
  239. $check = Db::name('intro_user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  240. if($check){
  241. $this->error('您已经申请了提现,请等待审核');
  242. }
  243. $user_money = model('wallet')->getwallet($this->auth->id,'intromoney');
  244. if($money > $user_money){
  245. $this->error('提现金额不能大于可提现余额');
  246. }
  247. if($type == 1){
  248. $table_name = 'user_alipay';
  249. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  250. if(empty($account_json)){
  251. $this->error('未绑定对应的提现账号');
  252. }
  253. }elseif($type == 2){
  254. $table_name = 'user_bank';
  255. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  256. if(empty($account_json)){
  257. $this->error('未绑定对应的提现账号');
  258. }
  259. }elseif($type == 3){
  260. //微信支付
  261. $table_name = 'user_wechat';
  262. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  263. if(empty($account_json)){
  264. $this->error('未绑定对应的提现账号');
  265. }
  266. }
  267. //平台手续费
  268. $plat_bilv = config('site.intro_takecash_plat_bili');
  269. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  270. //减去手续费,得实得金额
  271. $get_money = bcsub($money,$plat_money,2);
  272. $data = [
  273. 'user_id' => $this->auth->id,
  274. 'money' => $money,
  275. 'plat_bilv' => $plat_bilv,
  276. 'plat_money' => $plat_money,
  277. 'get_money' => $get_money,
  278. 'type' => $type,
  279. 'acount_json' => json_encode($account_json),
  280. 'createtime' => time(),
  281. 'updatetime' => time(),
  282. 'status' => 0,
  283. ];
  284. Db::startTrans();
  285. $log_id = Db::name('intro_user_take_cash')->insertGetId($data);
  286. if(!$log_id){
  287. Db::rollback();
  288. $this->error('提现失败');
  289. }
  290. //扣除money
  291. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'intromoney',-$money,121,'提现(审核中)','intro_user_take_cash',$log_id);
  292. if($rs_wallet['status']===false)
  293. {
  294. Db::rollback();
  295. $this->error($rs_wallet['msg']);
  296. }
  297. Db::commit();
  298. $this->success('申请成功请等待审核');
  299. }
  300. //提现记录
  301. public function take_cash_log(){
  302. $list = Db::name('intro_user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  303. foreach($list as $key => &$val){
  304. $val['remark'] = '';
  305. if($val['type'] == 1){
  306. $val['remark'] = '支付宝提现';
  307. }elseif($val['type'] == 2){
  308. $val['remark'] = '银行卡提现';
  309. }else{
  310. $val['remark'] = '微信提现';
  311. }
  312. }
  313. $this->success('success',$list);
  314. }
  315. }