WalletController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Http\Controllers\agent;
  3. use App\Http\Controllers\AgentApiController;
  4. use App\Models\Agent\AgentBalanceLog;
  5. use App\Models\Agent\AgentWithdrawAccount;
  6. use App\Models\Agent\AgentWithdrawal;
  7. use App\Models\Agent\AgentRecharge;
  8. use App\Services\Agent\AgentExchangeRate;
  9. use App\Services\Agent\AgentPaymentService;
  10. use App\Services\Agent\AgentRechargeService;
  11. use App\Services\Agent\AgentWalletService;
  12. use App\Services\Agent\AgentWithdrawalService;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use App\Rules\DecimalNumber;
  16. class WalletController extends AgentApiController
  17. {
  18. public function logs(Request $request)
  19. {
  20. return $this->execute(function () use ($request) {
  21. $data = $request->validate([
  22. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  23. 'type' => ['nullable', 'string', 'max:32'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  24. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  25. ]);
  26. return $this->pageQuery(AgentBalanceLog::query()->where('agent_id', $this->currentAgent()->id), $data);
  27. });
  28. }
  29. /** 提现通道(总后台配置),格式同会员 GET /api/wallet/withdrawChannel。 */
  30. public function withdrawChannels(AgentWithdrawalService $service)
  31. {
  32. return $this->execute(fn () => $service->channels($this->currentAgent()->fresh()));
  33. }
  34. public function accounts(Request $request, AgentWithdrawalService $service)
  35. {
  36. return $this->execute(function () use ($request, $service) {
  37. $data = $request->validate(['type' => ['nullable', 'string', 'max:32']]);
  38. return $service->accounts($this->currentAgent(), $data['type'] ?? null);
  39. });
  40. }
  41. /** 新增/修改提现账户:type 为提现通道(默认 usdt),字段规则同会员绑卡。 */
  42. public function saveAccount(Request $request, AgentWithdrawalService $service)
  43. {
  44. return $this->execute(function () use ($request, $service) {
  45. $data = $request->validate([
  46. 'id' => ['nullable', 'integer'], 'type' => ['nullable', 'string', 'max:32'], 'name' => ['nullable', 'string', 'max:128'],
  47. 'network' => ['nullable', 'string', 'in:TRC20,ERC20,trc20,erc20'], 'account' => ['nullable', 'string', 'max:255'],
  48. 'card_no' => ['nullable', 'string', 'max:255'], 'bank_name' => ['nullable', 'string', 'max:128'],
  49. 'bank_branch' => ['nullable', 'string', 'max:255'], 'qr_image' => ['nullable', 'string', 'max:255'],
  50. 'payout_method' => ['nullable', 'string', 'in:bank,wechat,alipay'], 'real_name' => ['nullable', 'string', 'max:64'],
  51. 'status' => ['nullable', 'integer', 'in:0,1'],
  52. ]);
  53. return $service->saveAccount($this->currentAgent(), $data);
  54. });
  55. }
  56. public function deleteAccount(Request $request)
  57. {
  58. return $this->execute(function () use ($request) {
  59. $data = $request->validate(['id' => ['required', 'integer']]);
  60. DB::transaction(function () use ($data) {
  61. $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)
  62. ->lockForUpdate()->findOrFail($data['id']);
  63. if (AgentWithdrawal::query()->where('account_id', $account->id)->whereIn('status', AgentWithdrawalService::OPEN_STATUSES)->exists()) {
  64. throw new \RuntimeException('该账户有处理中的提现,不能删除');
  65. }
  66. $account->delete();
  67. });
  68. return [];
  69. });
  70. }
  71. public function withdrawals(Request $request)
  72. {
  73. return $this->execute(function () use ($request) {
  74. $data = $request->validate([
  75. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  76. 'status' => ['nullable', 'in:pending,approved,processing,rejected,paid,failed'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  77. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'], 'channel' => ['nullable', 'string', 'max:32'],
  78. ]);
  79. $query = AgentWithdrawal::query()->where('agent_id', $this->currentAgent()->id);
  80. if (!empty($data['channel'])) $query->where('account_type', $data['channel']);
  81. return $this->pageQuery($query, $data);
  82. });
  83. }
  84. public function requestWithdrawal(Request $request, AgentWithdrawalService $service)
  85. {
  86. return $this->execute(function () use ($request, $service) {
  87. $data = $request->validate([
  88. // USDT 账户传 usdt_amount(USDT 数量),其他通道传 amount(人民币,最多 2 位小数)
  89. 'account_id' => ['required', 'integer'],
  90. 'usdt_amount' => ['required_without:amount', 'nullable', 'numeric', 'min:0.0001', new DecimalNumber()],
  91. 'amount' => ['required_without:usdt_amount', 'nullable', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
  92. 'withdraw_password' => ['required', 'string'], 'remark' => ['nullable', 'string', 'max:500'],
  93. 'request_id' => ['required', 'string', 'max:64'],
  94. ]);
  95. $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)->findOrFail((int) $data['account_id']);
  96. $amount = $account->type === 'usdt' ? ($data['usdt_amount'] ?? null) : ($data['amount'] ?? null);
  97. if ($amount === null || $amount === '') {
  98. throw new \InvalidArgumentException($account->type === 'usdt' ? '请输入提现的 USDT 数量' : '请输入提现金额');
  99. }
  100. return $service->request($this->currentAgent(), (int) $data['account_id'], (string) $amount, $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? ''));
  101. });
  102. }
  103. /** 余额构成:总余额、保证金、盈亏、可提现。 */
  104. public function summary(AgentWalletService $wallets)
  105. {
  106. return $this->execute(fn () => $wallets->funds($agent = $this->currentAgent()->fresh()) + [
  107. 'recharge_locks' => $wallets->activeLocks($agent),
  108. 'withdraw_exchange_rate' => AgentExchangeRate::withdraw(),
  109. 'recharge_exchange_rate' => AgentExchangeRate::recharge(),
  110. ]);
  111. }
  112. public function rechargeAddress(Request $request, AgentRechargeService $service)
  113. {
  114. return $this->execute(function () use ($request, $service) {
  115. $data = $request->validate(['net' => ['nullable', 'in:trc20,erc20,TRC20,ERC20']]);
  116. return $service->address($this->currentAgent(), (string) ($data['net'] ?? 'trc20'));
  117. });
  118. }
  119. public function submitRecharge(Request $request, AgentRechargeService $service)
  120. {
  121. return $this->execute(function () use ($request, $service) {
  122. $data = $request->validate([
  123. 'net' => ['required', 'in:trc20,erc20,TRC20,ERC20'],
  124. 'usdt_amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber()],
  125. 'to_address' => ['required', 'string', 'max:64'],
  126. 'image' => ['required', 'url', 'max:1000'],
  127. ]);
  128. return $service->submitProof($this->currentAgent(), $data);
  129. });
  130. }
  131. public function recharges(Request $request)
  132. {
  133. return $this->execute(function () use ($request) {
  134. $data = $request->validate([
  135. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  136. 'status' => ['nullable', 'integer', 'in:0,1,2,3'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  137. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  138. ]);
  139. $query = AgentRecharge::query()->where('agent_id', $this->currentAgent()->id);
  140. if (isset($data['status'])) $query->where('status', (int) $data['status']);
  141. unset($data['status']);
  142. return $this->pageQuery($query, $data);
  143. });
  144. }
  145. /** 充值通道列表(总后台配置的全部通道,含 USDT、人工充值、808/NO/JD/三斤等),格式同会员 GET /api/wallet/channel。 */
  146. public function rechargeChannels(AgentPaymentService $service)
  147. {
  148. return $this->execute(fn () => ['list' => $service->channels()]);
  149. }
  150. /** 在线充值(三方代收)下单。 */
  151. public function createPayment(Request $request, AgentPaymentService $service)
  152. {
  153. return $this->execute(function () use ($request, $service) {
  154. $data = $request->validate([
  155. 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
  156. 'payment_type' => ['required', 'string', 'max:50'],
  157. ]);
  158. return $service->createOnline($this->currentAgent(), (string) $data['amount'], (string) $data['payment_type']);
  159. });
  160. }
  161. /** 人工充值:提交金额和付款方式。 */
  162. public function createManualPayment(Request $request, AgentPaymentService $service)
  163. {
  164. return $this->execute(function () use ($request, $service) {
  165. $data = $request->validate([
  166. 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
  167. 'payment_type' => ['required', 'integer'],
  168. ]);
  169. return $service->createManual($this->currentAgent(), (string) $data['amount'], (int) $data['payment_type']);
  170. });
  171. }
  172. /** 人工充值:收到收款信息后上传转账凭证。 */
  173. public function submitManualImage(Request $request, AgentPaymentService $service)
  174. {
  175. return $this->execute(function () use ($request, $service) {
  176. $data = $request->validate(['id' => ['required', 'integer'], 'image' => ['required', 'url', 'max:1000']]);
  177. return $service->submitImage($this->currentAgent(), (int) $data['id'], (string) $data['image']);
  178. });
  179. }
  180. /** 人工充值:撤回待处理 / 待提交凭证的订单。 */
  181. public function cancelManualPayment(Request $request, AgentPaymentService $service)
  182. {
  183. return $this->execute(function () use ($request, $service) {
  184. $data = $request->validate(['id' => ['required', 'integer']]);
  185. return $service->cancelManual($this->currentAgent(), (int) $data['id']);
  186. });
  187. }
  188. /** 人民币充值记录(在线 + 人工)。 */
  189. public function paymentOrders(Request $request, AgentPaymentService $service)
  190. {
  191. return $this->execute(function () use ($request, $service) {
  192. $data = $request->validate([
  193. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  194. 'type' => ['nullable', 'integer', 'in:1,3'], 'status' => ['nullable', 'integer', 'between:0,7'],
  195. 'order_no' => ['nullable', 'string', 'max:64'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  196. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  197. ]);
  198. return $service->paginate($data, $this->currentAgent());
  199. });
  200. }
  201. private function pageQuery($query, array $data): array
  202. {
  203. $page = max(1, (int) ($data['page'] ?? 1));
  204. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  205. if (!empty($data['type'])) $query->where('type', $data['type']);
  206. if (!empty($data['status'])) $query->where('status', $data['status']);
  207. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  208. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  209. $total = (clone $query)->count();
  210. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  211. return compact('total', 'page', 'limit', 'list');
  212. }
  213. }