WalletController.php 11 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. public function accounts(Request $request)
  30. {
  31. return $this->execute(function () use ($request) {
  32. return AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)
  33. ->where('type', 'usdt')->orderByDesc('id')->get();
  34. });
  35. }
  36. public function saveAccount(Request $request)
  37. {
  38. return $this->execute(function () use ($request) {
  39. // 代理提现仅支持 USDT,地址按公链校验格式。
  40. $data = $request->validate([
  41. 'id' => ['nullable', 'integer'], 'name' => ['nullable', 'string', 'max:128'],
  42. 'network' => ['required', 'in:TRC20,ERC20'],
  43. 'account' => ['required', 'string', 'max:255', function ($attribute, $value, $fail) use ($request) {
  44. $pattern = $request->input('network') === 'ERC20' ? '/^0x[0-9a-fA-F]{40}$/D' : '/^T[1-9A-HJ-NP-Za-km-z]{33}$/D';
  45. if (!preg_match($pattern, (string) $value)) $fail('USDT 地址格式错误');
  46. }],
  47. 'status' => ['nullable', 'integer', 'in:0,1'],
  48. ]);
  49. $data['type'] = 'usdt';
  50. $data['bank_name'] = '';
  51. $isNew = empty($data['id']);
  52. foreach (['name', 'network'] as $field) {
  53. if ($isNew || array_key_exists($field, $data)) {
  54. $data[$field] = trim((string) ($data[$field] ?? ''));
  55. }
  56. }
  57. if ($isNew || array_key_exists('status', $data)) {
  58. $data['status'] = $data['status'] ?? 1;
  59. }
  60. $agent = $this->currentAgent();
  61. $account = $isNew ? new AgentWithdrawAccount() : AgentWithdrawAccount::query()->where('agent_id', $agent->id)->findOrFail($data['id']);
  62. $account->fill($data);
  63. $account->agent_id = $agent->id;
  64. $account->save();
  65. return $account;
  66. });
  67. }
  68. public function deleteAccount(Request $request)
  69. {
  70. return $this->execute(function () use ($request) {
  71. $data = $request->validate(['id' => ['required', 'integer']]);
  72. DB::transaction(function () use ($data) {
  73. $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)
  74. ->lockForUpdate()->findOrFail($data['id']);
  75. if (AgentWithdrawal::query()->where('account_id', $account->id)->whereIn('status', ['pending', 'approved'])->exists()) {
  76. throw new \RuntimeException('该账户有处理中的提现,不能删除');
  77. }
  78. $account->delete();
  79. });
  80. return [];
  81. });
  82. }
  83. public function withdrawals(Request $request)
  84. {
  85. return $this->execute(function () use ($request) {
  86. $data = $request->validate([
  87. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  88. 'status' => ['nullable', 'in:pending,approved,rejected,paid'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  89. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  90. ]);
  91. return $this->pageQuery(AgentWithdrawal::query()->where('agent_id', $this->currentAgent()->id), $data);
  92. });
  93. }
  94. public function requestWithdrawal(Request $request, AgentWithdrawalService $service)
  95. {
  96. return $this->execute(function () use ($request, $service) {
  97. $data = $request->validate([
  98. 'account_id' => ['required', 'integer'], 'usdt_amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()],
  99. 'withdraw_password' => ['required', 'string'], 'remark' => ['nullable', 'string', 'max:500'],
  100. 'request_id' => ['required', 'string', 'max:64'],
  101. ]);
  102. return $service->request($this->currentAgent(), (int) $data['account_id'], (string) $data['usdt_amount'], $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? ''));
  103. });
  104. }
  105. /** 余额构成:总余额、保证金、盈亏、可提现。 */
  106. public function summary(AgentWalletService $wallets)
  107. {
  108. return $this->execute(fn () => $wallets->funds($this->currentAgent()->fresh()) + [
  109. 'withdraw_exchange_rate' => AgentExchangeRate::withdraw(),
  110. 'recharge_exchange_rate' => AgentExchangeRate::recharge(),
  111. ]);
  112. }
  113. public function rechargeAddress(Request $request, AgentRechargeService $service)
  114. {
  115. return $this->execute(function () use ($request, $service) {
  116. $data = $request->validate(['net' => ['nullable', 'in:trc20,erc20,TRC20,ERC20']]);
  117. return $service->address($this->currentAgent(), (string) ($data['net'] ?? 'trc20'));
  118. });
  119. }
  120. public function submitRecharge(Request $request, AgentRechargeService $service)
  121. {
  122. return $this->execute(function () use ($request, $service) {
  123. $data = $request->validate([
  124. 'net' => ['required', 'in:trc20,erc20,TRC20,ERC20'],
  125. 'usdt_amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber()],
  126. 'to_address' => ['required', 'string', 'max:64'],
  127. 'image' => ['required', 'url', 'max:1000'],
  128. ]);
  129. return $service->submitProof($this->currentAgent(), $data);
  130. });
  131. }
  132. public function recharges(Request $request)
  133. {
  134. return $this->execute(function () use ($request) {
  135. $data = $request->validate([
  136. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  137. 'status' => ['nullable', 'integer', 'in:0,1,2,3'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  138. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  139. ]);
  140. $query = AgentRecharge::query()->where('agent_id', $this->currentAgent()->id);
  141. if (isset($data['status'])) $query->where('status', (int) $data['status']);
  142. unset($data['status']);
  143. return $this->pageQuery($query, $data);
  144. });
  145. }
  146. /** 充值通道列表(总后台配置的全部通道,含 USDT、人工充值、808/NO/JD/三斤等),格式同会员 GET /api/wallet/channel。 */
  147. public function rechargeChannels(AgentPaymentService $service)
  148. {
  149. return $this->execute(fn () => ['list' => $service->channels()]);
  150. }
  151. /** 在线充值(三方代收)下单。 */
  152. public function createPayment(Request $request, AgentPaymentService $service)
  153. {
  154. return $this->execute(function () use ($request, $service) {
  155. $data = $request->validate([
  156. 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
  157. 'payment_type' => ['required', 'string', 'max:50'],
  158. ]);
  159. return $service->createOnline($this->currentAgent(), (string) $data['amount'], (string) $data['payment_type']);
  160. });
  161. }
  162. /** 人工充值:提交金额和付款方式。 */
  163. public function createManualPayment(Request $request, AgentPaymentService $service)
  164. {
  165. return $this->execute(function () use ($request, $service) {
  166. $data = $request->validate([
  167. 'amount' => ['required', 'numeric', 'min:0.01', new DecimalNumber(14, 2)],
  168. 'payment_type' => ['required', 'integer'],
  169. ]);
  170. return $service->createManual($this->currentAgent(), (string) $data['amount'], (int) $data['payment_type']);
  171. });
  172. }
  173. /** 人工充值:收到收款信息后上传转账凭证。 */
  174. public function submitManualImage(Request $request, AgentPaymentService $service)
  175. {
  176. return $this->execute(function () use ($request, $service) {
  177. $data = $request->validate(['id' => ['required', 'integer'], 'image' => ['required', 'url', 'max:1000']]);
  178. return $service->submitImage($this->currentAgent(), (int) $data['id'], (string) $data['image']);
  179. });
  180. }
  181. /** 人工充值:撤回待处理 / 待提交凭证的订单。 */
  182. public function cancelManualPayment(Request $request, AgentPaymentService $service)
  183. {
  184. return $this->execute(function () use ($request, $service) {
  185. $data = $request->validate(['id' => ['required', 'integer']]);
  186. return $service->cancelManual($this->currentAgent(), (int) $data['id']);
  187. });
  188. }
  189. /** 人民币充值记录(在线 + 人工)。 */
  190. public function paymentOrders(Request $request, AgentPaymentService $service)
  191. {
  192. return $this->execute(function () use ($request, $service) {
  193. $data = $request->validate([
  194. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  195. 'type' => ['nullable', 'integer', 'in:1,3'], 'status' => ['nullable', 'integer', 'between:0,7'],
  196. 'order_no' => ['nullable', 'string', 'max:64'], 'start_date' => ['nullable', 'date_format:Y-m-d'],
  197. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  198. ]);
  199. return $service->paginate($data, $this->currentAgent());
  200. });
  201. }
  202. private function pageQuery($query, array $data): array
  203. {
  204. $page = max(1, (int) ($data['page'] ?? 1));
  205. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  206. if (!empty($data['type'])) $query->where('type', $data['type']);
  207. if (!empty($data['status'])) $query->where('status', $data['status']);
  208. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  209. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  210. $total = (clone $query)->count();
  211. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  212. return compact('total', 'page', 'limit', 'list');
  213. }
  214. }