Withdraw.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\BalanceLogService;
  6. use App\Services\PaymentOrderService;
  7. use App\Services\TopUpService;
  8. use App\Services\WalletService;
  9. use App\Services\WithdrawService;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Validation\ValidationException;
  12. use Exception;
  13. use App\Models\Withdraw as WithdrawModel;
  14. use App\Models\PaymentOrder;
  15. use App\Models\Config;
  16. class Withdraw extends Controller
  17. {
  18. function setRmbNote()
  19. {
  20. try {
  21. $params = request()->validate([
  22. 'id' => ['required', 'integer', 'min:1'],
  23. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  24. ]);
  25. $po = PaymentOrder::where('id', $params['id'])
  26. ->where('type', 2)->first();
  27. if (!$po) throw new Exception("记录不存在", HttpStatus::CUSTOM_ERROR);
  28. $po->admin_note = $params['admin_note'];
  29. $po->save();
  30. } catch (ValidationException $e) {
  31. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  32. } catch (Exception $e) {
  33. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  34. }
  35. return $this->success();
  36. }
  37. public function rmb()
  38. {
  39. try {
  40. $params = request()->validate([
  41. 'page' => ['nullable', 'integer', 'min:1'],
  42. 'limit' => ['nullable', 'integer', 'min:1'],
  43. 'member_id' => ['nullable', 'string', 'min:1'],
  44. 'status' => ['nullable', 'integer', 'min:0', 'max:3']
  45. ]);
  46. $page = request()->input('page', 1);
  47. $limit = request()->input('limit', 10);
  48. $params['type'] = 2;
  49. $query = PaymentOrder::query();
  50. $where = PaymentOrderService::getWhere($params);
  51. $count = $query->where($where)->count();
  52. $list = $query->where($where)
  53. ->with(['userInfo'])
  54. ->orderBy('created_at', 'desc')
  55. ->forpage($page, $limit)->get();
  56. $result = ['total' => $count, 'data' => $list];
  57. } catch (ValidationException $e) {
  58. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  59. } catch (Exception $e) {
  60. return $this->error(intval($e->getCode()));
  61. }
  62. return $this->success($result);
  63. }
  64. public function setNote()
  65. {
  66. try {
  67. $params = request()->validate([
  68. 'id' => ['required', 'integer', 'min:1'],
  69. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  70. ]);
  71. $w = WithdrawModel::where('id', $params['id'])->first();
  72. if (!$w) throw new Exception("记录不存在", HttpStatus::CUSTOM_ERROR);
  73. $w->admin_note = $params['admin_note'];
  74. $w->save();
  75. } catch (ValidationException $e) {
  76. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  77. } catch (Exception $e) {
  78. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  79. }
  80. return $this->success();
  81. }
  82. public function index()
  83. {
  84. try {
  85. $params = request()->validate([
  86. 'page' => ['nullable', 'integer', 'min:1'],
  87. 'limit' => ['nullable', 'integer', 'min:1'],
  88. 'member_id' => ['nullable', 'string', 'min:1'],
  89. 'status' => ['nullable', 'integer', 'min:0', 'max:2']
  90. ]);
  91. $page = request()->input('page', 1);
  92. $limit = request()->input('limit', 10);
  93. $query = WithdrawModel::query();
  94. $where = WithdrawService::getWhere($params);
  95. $count = $query->where($where)->count();
  96. $list = $query->where($where)
  97. ->with(['member'])
  98. ->orderBy('created_at', 'desc')
  99. ->forpage($page, $limit)->get();
  100. foreach ($list as &$item) {
  101. $item['exchange_rate'] = floatval($item['exchange_rate']);
  102. $item['after_balance'] = floatval($item['after_balance']);
  103. $item['service_charge'] = floatval($item['service_charge']);
  104. $item['amount'] = floatval($item['amount']);
  105. $item['to_account'] = floatval($item['to_account']);
  106. $item['exchange_rate'] = $item['exchange_rate'] <= 0 ? 7.3 : $item['exchange_rate'];
  107. $item['to_account_usdt'] = floatval($item['to_account']);
  108. }
  109. $result = ['total' => $count, 'data' => $list];
  110. } catch (ValidationException $e) {
  111. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  112. } catch (Exception $e) {
  113. return $this->error(intval($e->getCode()));
  114. }
  115. return $this->success($result);
  116. }
  117. /**
  118. * @api {post} /admin/withdraw/setStatus 通过|拒绝
  119. * @apiGroup 提现管理
  120. *
  121. * @apiUse result
  122. * @apiUse header
  123. * @apiVersion 1.0.0
  124. *
  125. * @apiParam {string} id 提现表ID
  126. * @apiParam {int} status 状态
  127. * - 1 通过
  128. * - 2 拒绝
  129. */
  130. public function setStatus()
  131. {
  132. DB::beginTransaction();
  133. try {
  134. request()->validate([
  135. 'id' => ['required', 'string', 'min:1'],
  136. 'status' => ['required', 'integer', 'min:1', 'max:2']
  137. ]);
  138. $id = request()->input('id');
  139. $status = request()->input('status');
  140. $w = WithdrawService::findOne(['id' => $id, 'status' => 0]);
  141. if (!$w) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  142. // 汇率
  143. $rate = $w->exchange_rate ?? 1;
  144. if ($status == 1) {
  145. $w->status = 1;
  146. $w->save();
  147. } else if ($status == 2) {
  148. $rate_rmb_amount = bcmul($w->amount, $rate, 2); // 提现金额 折合RMB
  149. $w->status = 2;
  150. $w->save();
  151. $wallet = WalletService::findOne(['member_id' => $w->member_id]);
  152. $afterBalance = bcadd($wallet->available_balance, $rate_rmb_amount, 10);
  153. BalanceLogService::addLog($w->member_id, $w->rate_rmb_amount, $wallet->available_balance, $afterBalance, '提现', $w->id, '');
  154. $wallet->available_balance = $afterBalance;
  155. $wallet->save();
  156. }
  157. $arr = ['⏳️申请中', '✅️成功', '❌️失败'];
  158. $text = "📢 提现结果通知\n\n";
  159. $temp = floatval($w->service_charge);
  160. $text .= "手续费:{$temp} USDT\n";
  161. $temp = floatval($w->amount);
  162. $text .= "提现金额:{$temp} USDT\n";
  163. $temp = floatval($w->to_account);
  164. $text .= "到账金额:{$temp} USDT\n";
  165. $text .= "提现地址:{$w->address}\n\n";
  166. $text .= "状态:{$arr[$w->status]}\n";
  167. if ($w->remark) $text .= "说明:{$w->remark}";
  168. $res = WithdrawService::notify([
  169. 'chat_id' => $w->member_id,
  170. 'text' => $text,
  171. ]);
  172. DB::commit();
  173. } catch (ValidationException $e) {
  174. DB::rollBack();
  175. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  176. } catch (Exception $e) {
  177. DB::rollBack();
  178. return $this->error(intval($e->getCode()), $e->getMessage());
  179. }
  180. return $this->success($res);
  181. }
  182. }