UserExtractController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v1\user;
  12. use app\model\user\User;
  13. use app\model\user\UserExtract;
  14. use app\Request;
  15. use app\services\user\UserExtractServices;
  16. use crmeb\services\WithdrawService;
  17. use think\facade\Config;
  18. /**
  19. * 提现类
  20. * Class UserExtractController
  21. * @package app\controller\api\user
  22. */
  23. class UserExtractController
  24. {
  25. protected $services = NUll;
  26. /**
  27. * UserExtractController constructor.
  28. * @param UserExtractServices $services
  29. */
  30. public function __construct(UserExtractServices $services)
  31. {
  32. $this->services = $services;
  33. }
  34. /**
  35. * 提现银行
  36. * @param Request $request
  37. * @return mixed
  38. */
  39. public function bank(Request $request)
  40. {
  41. $uid = (int)$request->uid();
  42. return app('json')->successful($this->services->bank($uid));
  43. }
  44. public function cash_calculator(Request $request)
  45. {
  46. $fee = sys_data('withdraw_fee');
  47. $user = User::where('uid', $request->uid())->find();
  48. if ($user['is_auth'] != 2) return app('json')->fail('请先完成实名认证');
  49. $uids = User::where('card_id', $user['card_id'])->column('uid');
  50. $money = $request->post('money');
  51. $user_type = $request->post('user_type', 0);
  52. $extract_fee = 0;
  53. if ($user_type == 0) {
  54. $sum_money = UserExtract::where('uid', 'in', $uids)
  55. ->whereTime('add_time', 'month')
  56. ->where('user_type', 0)
  57. ->where('extract_type', '<>', 'balance')
  58. ->where('status', 1)->sum('extract_price');
  59. $sum_fee = UserExtract::where('uid', 'in', $uids)
  60. ->whereTime('add_time', 'month')
  61. ->where('user_type', 0)
  62. ->where('extract_type', '<>', 'balance')
  63. ->where('status', 1)->sum('extract_fee');
  64. $money_sum = bcadd(bcadd((string)$sum_money, (string)$sum_fee, 2), (string)$money, 2);
  65. $max_range = 0;
  66. $range = [];
  67. foreach ($fee as $v) {
  68. if ($money_sum > $v['month_number']) {
  69. if ($max_range < $v['month_number']) {
  70. $range = $v;
  71. $max_range = $v['month_number'];
  72. }
  73. }
  74. }
  75. if ($range != []) {
  76. if ($range['free_type'] == 1) {
  77. $free = bcdiv(bcmul((string)$money_sum, (string)$range['free']), '100', 2);
  78. } else {
  79. $free = $range['free'];
  80. }
  81. $the_fee = bcdiv(bcmul(bcsub((string)$money_sum, (string)$free, 2), (string)$range['fee']), '100', 2);
  82. $the_fee = bcsub((string)$the_fee, (string)$range['dec'], 2);
  83. $extract_fee = bcsub((string)$the_fee, (string)$sum_fee, 2);
  84. }
  85. }
  86. return app('json')->success('ok', ['fee' => $extract_fee > 0 ? $extract_fee : 0]);
  87. }
  88. /**
  89. * 提现申请
  90. * @param Request $request
  91. * @return mixed
  92. */
  93. public function cash(Request $request)
  94. {
  95. $extractInfo = $request->postMore([
  96. ['alipay_code', ''],
  97. ['extract_type', 'bank'],
  98. ['money', 0],
  99. ['image', 0],
  100. ['name', ''],
  101. ['bankname', ''],
  102. ['cardnum', ''],
  103. ['weixin', ''],
  104. ['qrcode_url', ''],
  105. ['user_type', 0],
  106. ]);
  107. $extractType = Config::get('pay.extractType', []);
  108. if (!in_array($extractInfo['extract_type'], $extractType))
  109. return app('json')->fail('提现方式不存在');
  110. if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', (float)$extractInfo['money'])) return app('json')->fail('提现金额输入有误');
  111. if (!$extractInfo['cardnum'] == '')
  112. if (!preg_match('/^([1-9]{1})(\d{8}|\d{11}|\d{14}|\d{15}|\d{16}|\d{18})$/', $extractInfo['cardnum']))
  113. return app('json')->fail('银行卡号输入有误');
  114. if (!$extractInfo['image'])
  115. return app('json')->fail('请上传发票凭证');
  116. if ($extractInfo['extract_type'] == 'alipay') {
  117. if (!$extractInfo['alipay_code']) return app('json')->fail('请输入支付宝账号');
  118. } else if ($extractInfo['extract_type'] == 'bank') {
  119. if (!$extractInfo['cardnum']) return app('json')->fail('请输入银行卡账号');
  120. if (!$extractInfo['bankname']) return app('json')->fail('请输入开户行信息');
  121. // $bank_info = WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
  122. // if ($bank_info['is_contract']) {
  123. // return app('json')->fail('签约已完成');
  124. // }
  125. } else if ($extractInfo['extract_type'] == 'weixin' && sys_config('brokerage_type') == 0) {
  126. if (!$extractInfo['weixin']) return app('json')->fail('请输入微信账号');
  127. }
  128. $uid = (int)$request->uid();
  129. if ($this->services->cash($uid, $extractInfo))
  130. return app('json')->successful('申请提现成功!');
  131. else
  132. return app('json')->fail('提现失败');
  133. }
  134. }