Withdraw.php 8.4 KB

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