Withdraw.php 6.8 KB

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