WithdrawController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. namespace Modules\AgentApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Jobs\SendTgWithdrawMegJob;
  5. use App\Repositories\AgentBankInfoRepository;
  6. use App\Repositories\AgentRepository;
  7. use App\Repositories\AnnounceRepository;
  8. use App\Repositories\BalanceLogRepository;
  9. use App\Repositories\ConfigRepoistory;
  10. use App\Repositories\Criterias\FinanceWithdrawCriteria;
  11. use App\Repositories\Criterias\OrderLimitCriteria;
  12. use App\Repositories\WithDrawalLogRepository;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Validator;
  16. use Modules\AgentApi\Responses\ConfigSuccessResponse;
  17. use Modules\AgentApi\Responses\WithdrawBankInfoResponse;
  18. use Modules\AgentApi\Responses\WithdrawLogResponse;
  19. class WithdrawController extends BaseController
  20. {
  21. /*public function __construct()
  22. {
  23. $this->accountInfo = $this->getCurrentAgent();
  24. }*/
  25. /**
  26. * 代理提现方式列表
  27. *
  28. * @param AgentBankInfoRepository $bankRepo
  29. * @return WithdrawBankInfoResponse
  30. */
  31. public function bankInfo(AgentBankInfoRepository $bankRepo)
  32. {
  33. $this->accountInfo = $this->getCurrentAgent();
  34. $bankInfo = $bankRepo->agentBankList($this->accountInfo['uid']);
  35. /**
  36. * @var ConfigRepoistory $confRepo
  37. */
  38. $confRepo = app(ConfigRepoistory::class);
  39. $serviceLink = $confRepo->get('servicese_link', 'sys');
  40. return new WithdrawBankInfoResponse($bankInfo, $serviceLink);
  41. }
  42. /**
  43. * 新增提现方式
  44. *
  45. * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse
  46. */
  47. public function bankCreate()
  48. {
  49. $inputData = $this->getJsonData();
  50. $rules = [
  51. 'bank_card' => 'nullable|string',
  52. 'bank_account' => 'nullable|string',
  53. 'bank_mark' => 'nullable|string',
  54. 'type' => 'required|in:1,2,3',
  55. 'ustd_network' => 'nullable|string',
  56. 'ustd_account' => 'nullable|string',
  57. 'alipay_account' => 'nullable|string',
  58. 'alipay_username' => 'nullable|string',
  59. ];
  60. $validator = Validator::make($inputData, $rules);
  61. if ($validator->fails()) {
  62. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  63. return json_fail($validator->errors()->first());
  64. }
  65. $this->accountInfo = $this->getCurrentAgent();
  66. /**
  67. * @var AgentBankInfoRepository $bankRepo
  68. */
  69. $bankRepo = app(AgentBankInfoRepository::class);
  70. $findBank = $bankRepo->findWhere(['type' => $inputData['type'], 'agent_uid' => $this->accountInfo['uid']],
  71. ['id']);
  72. if ($findBank->isNotEmpty()) {
  73. return json_fail('请勿重复添加同一种提现方式!');
  74. }
  75. $setarr['type'] = $inputData['type'];
  76. $setarr['agent_uid'] = $this->accountInfo['uid'];
  77. if (!empty($inputData['alipay_account'])) {
  78. $setarr['alipay_account'] = $inputData['alipay_account'];
  79. }
  80. if (!empty($inputData['alipay_username'])) {
  81. $setarr['alipay_username'] = $inputData['alipay_username'];
  82. }
  83. if (!empty($inputData['bank_card'])) {
  84. $setarr['bank_card'] = $inputData['bank_card'];
  85. }
  86. if (!empty($inputData['bank_account'])) {
  87. $setarr['bank_account'] = $inputData['bank_account'];
  88. }
  89. if (!empty($inputData['bank_mark'])) {
  90. $setarr['bank_mark'] = $inputData['bank_mark'];
  91. }
  92. if (!empty($inputData['ustd_network'])) {
  93. $setarr['ustd_network'] = $inputData['ustd_network'];
  94. }
  95. if (!empty($inputData['ustd_account'])) {
  96. $setarr['ustd_account'] = $inputData['ustd_account'];
  97. }
  98. $result = $bankRepo->create($setarr);
  99. if (!$result) {
  100. return json_fail('新增提现方式失败');
  101. } else {
  102. $logSubject = '新增一个提现银行卡,:' . $result->uid;
  103. $logSvalue = $result->uid;
  104. $logDbData = [
  105. 'after_items' => [
  106. $setarr,
  107. ],
  108. 'before_items' => [
  109. ],
  110. ];
  111. $this->log($logSubject, $logSvalue, $logDbData);
  112. return new ConfigSuccessResponse();
  113. }
  114. }
  115. /**
  116. * 提现列表
  117. *
  118. * @return \Illuminate\Http\JsonResponse|WithdrawLogResponse
  119. */
  120. public function lists()
  121. {
  122. $inputData = $this->getJsonData();
  123. $rules = [
  124. 'page' => 'nullable|integer',
  125. 'page_size' => 'nullable|integer',
  126. 'start_date' => 'nullable|string',
  127. 'end_date' => 'nullable|string',
  128. ];
  129. $validator = Validator::make($inputData, $rules);
  130. if ($validator->fails()) {
  131. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  132. return json_fail($validator->errors()->first());
  133. }
  134. $this->accountInfo = $this->getCurrentAgent();
  135. // 针对layui table自动分页请求参数,优先GET值.
  136. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  137. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  138. if (!is_null($queryPage)) {
  139. $inputData['page'] = $queryPage;
  140. }
  141. if (!is_null($queryPageSize)) {
  142. $inputData['page_size'] = $queryPageSize;
  143. }
  144. $orders = [
  145. ['created_at', 'DESC']
  146. ];
  147. if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
  148. $pageSize = CommonConstant::PAGE_SIZE;
  149. } else {
  150. $pageSize = $inputData['page_size'];
  151. }
  152. $page = format_page($inputData['page'] ?? 0);
  153. /**
  154. * @var WithDrawalLogRepository $witRepo
  155. */
  156. $witRepo = app(WithDrawalLogRepository::class);
  157. $inputData['agent_uid'] = $this->accountInfo['uid'];
  158. $field = ['created_at', 'paid_at', 'status', 'uid', 'type', 'amount', 'usdt_rate'];
  159. $res = $witRepo->pushCriteria(new FinanceWithdrawCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize,
  160. $field, $page);
  161. /**
  162. * @var AgentRepository $agentRepo
  163. */
  164. $agentRepo = app(AgentRepository::class);
  165. $accountInfo = $agentRepo->findBy('uid', $this->accountInfo['uid'], ['frozen', 'balance']);
  166. /**
  167. * @var AnnounceRepository $annRespo
  168. */
  169. $annRespo = app(AnnounceRepository::class);
  170. $annData = $annRespo->findWhere(['status' => 1, 'type' => 2], ['content', 'title']);
  171. return new WithdrawLogResponse($res, $accountInfo, $annData);
  172. }
  173. /**
  174. * 提现账户详情
  175. *
  176. * @return mixed
  177. */
  178. public function accountInfo()
  179. {
  180. $this->accountInfo = $this->getCurrentAgent();
  181. /**
  182. * @var AgentRepository $agentRepo
  183. */
  184. $agentRepo = app(AgentRepository::class);
  185. $accountInfo = $agentRepo->findBy('uid', $this->accountInfo['uid'], ['balance']);
  186. /**
  187. * @var AgentBankInfoRepository $bankList
  188. */
  189. $bankRepo = app(AgentBankInfoRepository::class);
  190. $bankList = $bankRepo->agentBankList($this->accountInfo['uid'],
  191. ['type', 'bank_account', 'ustd_account', 'alipay_account', 'uid']);
  192. /**
  193. * @var ConfigRepoistory $confRepo
  194. */
  195. $confRepo = app(ConfigRepoistory::class);
  196. $usdtRate = $confRepo->get('usdt_rate', 'sys');
  197. if ($this->accountInfo['withdraw_usdt']) {
  198. $minUsdt = $this->accountInfo['withdraw_usdt'];
  199. } else {
  200. $minUsdt = $confRepo->get('withdraw_min_usdt', 'sys');
  201. }
  202. if ($this->accountInfo['withdraw_amount']) {
  203. $minMoney = $this->accountInfo['withdraw_amount'];
  204. } else {
  205. $minMoney = $confRepo->get('withdraw_min_money', 'sys');
  206. }
  207. $res['balance'] = $accountInfo->balance;
  208. $res['usdt_rate'] = $usdtRate;
  209. $res['bank_list'] = $bankList;
  210. $res['withdraw_min_usdt'] = $minUsdt;
  211. $res['withdraw_min_money'] = $minMoney;
  212. return json_success($res);
  213. }
  214. /**
  215. * 提现申请
  216. *
  217. * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse
  218. * @throws \Psr\SimpleCache\InvalidArgumentException
  219. */
  220. public function apply()
  221. {
  222. $inputData = $this->getJsonData();
  223. $rules = [
  224. 'bank_uid' => 'required|string',
  225. 'type' => 'required|in:1,2,3',
  226. 'amount' => 'required|integer',
  227. 'withdraw_pwd' => 'required',
  228. ];
  229. $validator = Validator::make($inputData, $rules);
  230. if ($validator->fails()) {
  231. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  232. return json_fail($validator->errors()->first());
  233. }
  234. $this->accountInfo = $this->getCurrentAgent();
  235. /**
  236. * @var ConfigRepoistory $confRepo
  237. */
  238. $confRepo = app(ConfigRepoistory::class);
  239. $rate = 0;
  240. if ($inputData['type'] == 2) {
  241. $rate = $confRepo->get('usdt_rate', 'sys');
  242. $amount = intval($inputData['amount'] * $rate);
  243. } else {
  244. $amount = $inputData['amount'];
  245. }
  246. /**
  247. * @var AgentRepository $agentRepo
  248. */
  249. $agentRepo = app(AgentRepository::class);
  250. $accountInfo = $agentRepo->findBy('uid', $this->accountInfo['uid']);
  251. $oldAccountInfo = clone $accountInfo;
  252. //最低提现金额
  253. if ($inputData['type'] == 2) {
  254. if ($accountInfo['withdraw_usdt']) {
  255. $minUsdt = $accountInfo['withdraw_usdt'];
  256. } else {
  257. $minUsdt = $confRepo->get('withdraw_min_usdt', 'sys');
  258. }
  259. if ($minUsdt > $amount) {
  260. return json_fail('最低提现为' . $minUsdt . 'U');
  261. }
  262. } else {
  263. if ($accountInfo['withdraw_amount']) {
  264. $minMoney = $accountInfo['withdraw_amount'];
  265. } else {
  266. $minMoney = $confRepo->get('withdraw_min_money', 'sys');
  267. }
  268. if ($minMoney > $amount) {
  269. return json_fail('最低提现金额为' . $minMoney . '元');
  270. }
  271. }
  272. if ($accountInfo->balance < $amount) {
  273. return json_fail('提现余额不足,请重新申请!');
  274. }
  275. if ($accountInfo->withdraw_pwd != $inputData['withdraw_pwd']) {
  276. return json_fail('提现密码错误!');
  277. }
  278. /**
  279. * @var BalanceLogRepository $bLogRepo
  280. */
  281. $bLogRepo = app(BalanceLogRepository::class);
  282. /**
  283. * @var WithDrawalLogRepository $witRepo
  284. */
  285. $witRepo = app(WithDrawalLogRepository::class);
  286. /**
  287. * @var AgentBankInfoRepository $bankList
  288. */
  289. $bankRepo = app(AgentBankInfoRepository::class);
  290. $bankInfo = $bankRepo->findByWhere(['uid' => $inputData['bank_uid']]);
  291. if (!$bankInfo) {
  292. return json_fail('提现账户异常,请联系客服!');
  293. }
  294. DB::beginTransaction();
  295. try {
  296. //帐变操作
  297. $accountInfo->balance -= $amount;
  298. $accountInfo->frozen += $amount;
  299. $res1 = $accountInfo->save();
  300. if (!$res1) {
  301. DB::rollBack();
  302. return json_fail('提现申请失败!');
  303. }
  304. //写入提现记录
  305. $instData = [
  306. 'status' => 2,
  307. 'agent_uid' => $this->accountInfo['uid'],
  308. 'amount' => $amount,
  309. 'type' => $inputData['type'],
  310. 'before_amount' => $oldAccountInfo->balance,
  311. 'after_amount' => $oldAccountInfo->balance - $amount,
  312. 'bank_account' => $bankInfo->bank_account,
  313. 'bank_card' => $bankInfo->bank_card,
  314. 'bank_mark' => $bankInfo->bank_mark,
  315. 'ustd_network' => $bankInfo->ustd_network,
  316. 'ustd_account' => $bankInfo->ustd_account,
  317. 'alipay_account' => $bankInfo->alipay_account,
  318. 'alipay_username' => $bankInfo->alipay_username,
  319. 'usdt_rate' => $rate,
  320. ];
  321. $res2 = $witRepo->create($instData);
  322. if (!$res2) {
  323. DB::rollBack();
  324. return json_fail('提现异常!');
  325. }
  326. $res3 = $bLogRepo->record($accountInfo, $oldAccountInfo);
  327. if (!$res3) {
  328. DB::rollBack();
  329. return json_fail('提现失败!');
  330. }
  331. DB::commit();
  332. } catch (\Throwable $e) {
  333. var_dump($e->getMessage());
  334. DB::rollBack();
  335. return json_fail('提现异常,请联系客服!');
  336. }
  337. $this->dispatch(new SendTgWithdrawMegJob(
  338. $this->accountInfo['uid'],
  339. $this->accountInfo['username'],
  340. $inputData['type'],
  341. $inputData['amount'],
  342. Carbon::now()->toDateTimeString()
  343. ));
  344. return new ConfigSuccessResponse();
  345. }
  346. public function usdtNetWork()
  347. {
  348. $this->accountInfo = $this->getCurrentAgent();
  349. $confRepo = app(ConfigRepoistory::class);
  350. $usdtAddress = $confRepo->get('usdt_address', 'sys');
  351. return json_success(explode(',', $usdtAddress));
  352. }
  353. }