WalletFreeze.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\WalletFreezeRecord;
  6. use App\Services\ManualAuditService;
  7. use App\Services\WalletFreezeService;
  8. use Exception;
  9. use Illuminate\Validation\ValidationException;
  10. /** 会员列表 / 人工稽查的冻结、解冻,以及冻结记录列表。 */
  11. class WalletFreeze extends Controller
  12. {
  13. private const AMOUNT_PATTERN = '/^\d{1,20}(?:\.\d{1,10})?$/';
  14. public function index()
  15. {
  16. try {
  17. $params = request()->validate([
  18. 'page' => ['nullable', 'integer', 'min:1'],
  19. 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
  20. 'member_id' => ['nullable', 'string', 'max:64'],
  21. 'account' => ['nullable', 'string', 'max:64'],
  22. 'type' => ['nullable', 'string', 'in:' . implode(',', array_keys(WalletFreezeRecord::TYPE_LABELS))],
  23. 'status' => ['nullable', 'string', 'in:' . implode(',', array_keys(WalletFreezeRecord::STATUS_LABELS))],
  24. 'start_date' => ['nullable', 'date_format:Y-m-d', 'required_with:end_date'],
  25. 'end_date' => ['nullable', 'date_format:Y-m-d', 'required_with:start_date', 'after_or_equal:start_date'],
  26. ]);
  27. return $this->success(WalletFreezeService::list($params));
  28. } catch (ValidationException $e) {
  29. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  30. } catch (Exception $e) {
  31. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  32. }
  33. }
  34. public function freeze()
  35. {
  36. return $this->apply(true);
  37. }
  38. public function unfreeze()
  39. {
  40. return $this->apply(false);
  41. }
  42. private function apply(bool $isFreeze)
  43. {
  44. try {
  45. $params = request()->validate([
  46. 'request_id' => ['required', 'uuid'],
  47. 'member_id' => ['nullable', 'required_without:account', 'string', 'max:64'],
  48. 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
  49. 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
  50. 'wager_amount' => ['required', 'numeric', 'min:0', 'regex:' . self::AMOUNT_PATTERN],
  51. 'reason' => ['required', 'string', 'min:1', 'max:255'],
  52. ]);
  53. $memberId = ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null);
  54. $method = $isFreeze ? 'freeze' : 'unfreeze';
  55. return $this->success(WalletFreezeService::$method((string)$params['request_id'], (int)request()->user->id, $memberId,
  56. (string)$params['amount'], (string)$params['wager_amount'], trim((string)$params['reason'])));
  57. } catch (ValidationException $e) {
  58. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  59. } catch (Exception $e) {
  60. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  61. }
  62. }
  63. }