WalletController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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($this->currentAgent()->fresh()) + [
  107. 'withdraw_exchange_rate' => AgentExchangeRate::withdraw(),
  108. 'recharge_exchange_rate' => AgentExchangeRate::recharge(),
  109. ]);
  110. }
  111. public function rechargeAddress(Request $request, AgentRechargeService $service)
  112. {
  113. return $this->execute(function () use ($request, $service) {
  114. $data = $request->validate(['net' => ['nullable', 'in:trc20,erc20,TRC20,ERC20']]);
  115. return $service->address($this->currentAgent(), (string) ($data['net'] ?? 'trc20'));
  116. });
  117. }
  118. public function submitRecharge(Request $request, AgentRechargeService $service)
  119. {
  120. return $this->execute(function () use ($request, $service) {
  121. $data = $request->validate([
  122. 'net' => ['required', 'in:trc20,erc20,TRC20,ERC20'],
  123. 'usdt_amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber()],
  124. 'to_address' => ['required', 'string', 'max:64'],
  125. 'image' => ['required', 'url', 'max:1000'],
  126. ]);
  127. return $service->submitProof($this->currentAgent(), $data);
  128. });
  129. }
  130. public function recharges(Request $request)
  131. {
  132. return $this->execute(function () use ($request) {
  133. $data = $request->validate([
  134. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  135. 'status' => ['nullable', 'integer', 'in:0,1,2,3'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  136. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  137. ]);
  138. $query = AgentRecharge::query()->where('agent_id', $this->currentAgent()->id);
  139. if (isset($data['status'])) $query->where('status', (int) $data['status']);
  140. unset($data['status']);
  141. return $this->pageQuery($query, $data);
  142. });
  143. }
  144. /** 充值通道列表(总后台配置的全部通道,含 USDT、人工充值、808/NO/JD/三斤等),格式同会员 GET /api/wallet/channel。 */
  145. public function rechargeChannels(AgentPaymentService $service)
  146. {
  147. return $this->execute(fn () => ['list' => $service->channels()]);
  148. }
  149. /** 在线充值(三方代收)下单。 */
  150. public function createPayment(Request $request, AgentPaymentService $service)
  151. {
  152. return $this->execute(function () use ($request, $service) {
  153. $data = $request->validate([
  154. 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
  155. 'payment_type' => ['required', 'string', 'max:50'],
  156. ]);
  157. return $service->createOnline($this->currentAgent(), (string) $data['amount'], (string) $data['payment_type']);
  158. });
  159. }
  160. /** 人工充值:提交金额和付款方式。 */
  161. public function createManualPayment(Request $request, AgentPaymentService $service)
  162. {
  163. return $this->execute(function () use ($request, $service) {
  164. $data = $request->validate([
  165. 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
  166. 'payment_type' => ['required', 'integer'],
  167. ]);
  168. return $service->createManual($this->currentAgent(), (string) $data['amount'], (int) $data['payment_type']);
  169. });
  170. }
  171. /** 人工充值:收到收款信息后上传转账凭证。 */
  172. public function submitManualImage(Request $request, AgentPaymentService $service)
  173. {
  174. return $this->execute(function () use ($request, $service) {
  175. $data = $request->validate(['id' => ['required', 'integer'], 'image' => ['required', 'url', 'max:1000']]);
  176. return $service->submitImage($this->currentAgent(), (int) $data['id'], (string) $data['image']);
  177. });
  178. }
  179. /** 人工充值:撤回待处理 / 待提交凭证的订单。 */
  180. public function cancelManualPayment(Request $request, AgentPaymentService $service)
  181. {
  182. return $this->execute(function () use ($request, $service) {
  183. $data = $request->validate(['id' => ['required', 'integer']]);
  184. return $service->cancelManual($this->currentAgent(), (int) $data['id']);
  185. });
  186. }
  187. /** 人民币充值记录(在线 + 人工)。 */
  188. public function paymentOrders(Request $request, AgentPaymentService $service)
  189. {
  190. return $this->execute(function () use ($request, $service) {
  191. $data = $request->validate([
  192. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  193. 'type' => ['nullable', 'integer', 'in:1,3'], 'status' => ['nullable', 'integer', 'between:0,7'],
  194. 'order_no' => ['nullable', 'string', 'max:64'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  195. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  196. ]);
  197. return $service->paginate($data, $this->currentAgent());
  198. });
  199. }
  200. private function pageQuery($query, array $data): array
  201. {
  202. $page = max(1, (int) ($data['page'] ?? 1));
  203. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  204. if (!empty($data['type'])) $query->where('type', $data['type']);
  205. if (!empty($data['status'])) $query->where('status', $data['status']);
  206. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  207. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  208. $total = (clone $query)->count();
  209. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  210. return compact('total', 'page', 'limit', 'list');
  211. }
  212. }