UserExtractController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\admin\model\system\SystemConfig;
  4. use app\models\store\StoreOrder;
  5. use app\models\tree\Tree;
  6. use app\models\user\User;
  7. use app\models\user\UserBill;
  8. use app\models\user\UserExtract;
  9. use app\models\user\UserTransfer;
  10. use app\Request;
  11. use crmeb\services\UtilService;
  12. /**
  13. * 提现类
  14. * Class UserExtractController
  15. * @package app\api\controller\user
  16. */
  17. class UserExtractController
  18. {
  19. /**
  20. * 提现银行
  21. * @param Request $request
  22. * @return mixed
  23. */
  24. public function bank(Request $request)
  25. {
  26. $user = $request->user();
  27. $broken_time = intval(sys_config('extract_time'));
  28. $search_time = time() - 86400 * $broken_time;
  29. //可提现佣金
  30. //返佣 +
  31. $brokerage_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  32. ->where('add_time', '>', $search_time)
  33. ->where('pm', 1)
  34. ->sum('number');
  35. //退款退的佣金 -
  36. $refund_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  37. ->where('add_time', '>', $search_time)
  38. ->where('pm', 0)
  39. ->sum('number');
  40. $data['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  41. if ($data['broken_commission'] < 0)
  42. $data['broken_commission'] = 0;
  43. // return $data;
  44. $data['brokerage_price'] = $user['brokerage_price'];
  45. //可提现佣金
  46. $data['commissionCount'] = $data['brokerage_price'] - $data['broken_commission'];
  47. $extractBank = sys_config('user_extract_bank') ?? []; //提现银行
  48. $extractBank = str_replace("\r\n", "\n", $extractBank);//防止不兼容
  49. $data['extractBank'] = explode("\n", is_array($extractBank) ? (isset($extractBank[0]) ? $extractBank[0] : $extractBank) : $extractBank);
  50. $data['minPrice'] = sys_config('user_extract_min_price');//提现最低金额
  51. $data['commission'] = sys_config('commission', 10);//提现手续费
  52. return app('json')->successful($data);
  53. }
  54. /**
  55. * 提现申请
  56. * @param Request $request
  57. * @return mixed
  58. */
  59. public function cash(Request $request)
  60. {
  61. $extractInfo = UtilService::postMore([
  62. ['alipay_code', ''],
  63. ['extract_type', ''],
  64. ['money', 0],
  65. ['name', ''],
  66. ['bankname', ''],
  67. ['cardnum', ''],
  68. ['weixin', ''],
  69. ['balance_type', 0],
  70. ], $request);
  71. $uids = User::where('spread_uid', $request->user()['uid'])->column('uid');
  72. if (Tree::where('uid', 'in', $uids)->count() < 1) {
  73. return app('json')->fail('需要直推一人购买才能提现');
  74. }
  75. if (!preg_match('/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/', $extractInfo['money'])) return app('json')->fail('提现金额输入有误');
  76. //提现设置最低金额
  77. if ($extractInfo['money'] < sys_config('user_extract_min_price')) return app('json')->fail('金额小于最低提现金额');
  78. $user = $request->user();
  79. $broken_time = intval(sys_config('extract_time'));
  80. $search_time = time() - 86400 * $broken_time;
  81. //可提现佣金
  82. //返佣 +
  83. $brokerage_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  84. ->where('add_time', '>', $search_time)
  85. ->where('pm', 1)
  86. ->sum('number');
  87. //退款退的佣金 -
  88. $refund_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  89. ->where('add_time', '>', $search_time)
  90. ->where('pm', 0)
  91. ->sum('number');
  92. $data['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  93. if ($data['broken_commission'] < 0)
  94. $data['broken_commission'] = 0;
  95. $data['brokerage_price'] = $user['brokerage_price'];
  96. //可提现佣金
  97. $commissionCount = $data['brokerage_price'] - $data['broken_commission'];
  98. if ($extractInfo['money'] > $commissionCount) return app('json')->fail('可提现佣金不足');
  99. /*
  100. if (!$extractInfo['cardnum'] == '')
  101. if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $extractInfo['cardnum']))
  102. return app('json')->fail('银行卡号输入有误');*/
  103. if (UserExtract::userExtract($request->user(), $extractInfo))
  104. return app('json')->successful('申请提现成功!');
  105. else
  106. return app('json')->fail(UserExtract::getErrorInfo('提现失败'));
  107. }
  108. /**
  109. * 余额转账
  110. * @param Request $request
  111. */
  112. public function transfer(Request $request)
  113. {
  114. list($to_uid, $pass, $merber, $type) = UtilService::postMore([
  115. ['to_uid', ''],
  116. ['pass', ''],
  117. ['merber', 0],
  118. ['type', 0],
  119. ], $request, true);
  120. if (!User::be(['uid' => $to_uid])) return app('json')->fail('用户不存在');
  121. if (strlen($pass) != 6) return app('json')->fail('交易密码只能输入6位数字');
  122. $user = $request->user();
  123. $money = $user['now_money'];
  124. $in = "转出余额";
  125. $out = "转入余额";
  126. $field = "now_money";
  127. if ($type == 1) {
  128. $money = $user['consume'];
  129. $in = "转入钻石";
  130. $out = "转出钻石";
  131. $field = "consume";
  132. }
  133. if (md5(md5($user['salt'] . $pass) . $user['salt']) != $user['transaction']) return app('json')->fail('交易密码错误');
  134. if (bcsub($money, $merber, 2) >= 0) {
  135. $user = User::where('uid', $request->uid())->lock(true)->find();
  136. User::where('uid', $request->uid())->dec($field, $merber)->update();
  137. $touser = User::where('uid', $to_uid)->find();
  138. User::where('uid', $to_uid)->inc($field, $merber)->update();
  139. $data['uid'] = $request->uid();
  140. $data['touid'] = $touser['uid'];
  141. $data['money'] = $merber;
  142. $data['order_id'] = UserTransfer::getNewOrderId();
  143. $data['add_time'] = time();
  144. $data['type'] = $type;
  145. $data['paid'] = 1;
  146. UserTransfer::create($data);
  147. UserBill::expend($out, $request->uid(), 'now_money', 'transfer_out', $merber, 0, bcsub($user['now_money'], $merber, 2), '转给' . $touser['nickname'] . "用户", 1);
  148. UserBill::income($in, $touser['uid'], 'now_money', 'transfer_in', $merber, 0, bcadd($touser['now_money'], $merber, 2), '收到用户' . $user['uid'] . '转账号');
  149. return app('json')->successful('转账成功!');
  150. } else {
  151. return app('json')->fail($out . '不足');
  152. }
  153. }
  154. /**
  155. * 转账记录
  156. * @param Request $request
  157. */
  158. public function transfer_list(Request $request)
  159. {
  160. list($page, $limit, $pm, $type) = UtilService::postMore([
  161. ['page', 1],
  162. ['limit', 20],
  163. ['pm', 0],
  164. ['type', 0]
  165. ], $request, true);
  166. if ($pm == 0) {
  167. $count = UserTransfer::where('uid', $request->uid())->where('type', $type)->count();
  168. $data = UserTransfer::where('uid', $request->uid())->where('type', $type)->page($page, $limit)->order("id desc")->select();
  169. foreach ($data as &$v) {
  170. $v['to_user'] = User::where('uid', $v['touid'])->value('nickname');
  171. $v['_add_time'] = date("Y-m-d H:i:s", $v['add_time']);
  172. }
  173. } else {
  174. $count = UserTransfer::where('touid', $request->uid())->where('type', $type)->count();
  175. $data = UserTransfer::where('touid', $request->uid())->where('type', $type)->page($page, $limit)->order("id desc")->select();
  176. foreach ($data as &$v) {
  177. $v['to_user'] = User::where('uid', $v['uid'])->value('nickname');
  178. $v['_add_time'] = date("Y-m-d H:i:s", $v['add_time']);
  179. }
  180. }
  181. return app('json')->successful(compact('count', 'data'));
  182. }
  183. }