Withdraw.php 8.1 KB

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