checkLogin(); } /** * 充提审核列表页 */ public function index() { $db = new Database(); try { // 查询充提申请列表,JOIN users 获取用户名 $requests = $db->select('fund_requests', [ '[>]users' => ['user_id' => 'id'] ], [ 'fund_requests.id', 'fund_requests.user_id', 'users.username', 'users.usdt_address', 'fund_requests.type', 'fund_requests.amount', 'fund_requests.status', 'fund_requests.remark', 'fund_requests.admin_remark', 'fund_requests.operator_id', 'fund_requests.created_at', 'fund_requests.processed_at' ], [ 'ORDER' => ['fund_requests.id' => 'DESC'], 'LIMIT' => 200 ]); if (!is_array($requests)) { $requests = []; } // 统计各状态数量 $pendingCount = $db->count('fund_requests', ['status' => 'pending']); $todayDeposit = $db->count('fund_requests', [ 'type' => 'deposit', 'created_at[>=]' => date('Y-m-d 00:00:00') ]); $todayWithdraw = $db->count('fund_requests', [ 'type' => 'withdraw', 'created_at[>=]' => date('Y-m-d 00:00:00') ]); } catch (\Throwable $e) { $requests = []; $pendingCount = 0; $todayDeposit = 0; $todayWithdraw = 0; } $this->render('Admin/fund_requests.php', [ 'requests' => $requests, 'pendingCount' => $pendingCount, 'todayDeposit' => $todayDeposit, 'todayWithdraw' => $todayWithdraw, 'title' => '充提审核' ]); } /** * 审批通过 */ public function approve() { header('Content-Type: application/json'); $rawData = file_get_contents('php://input'); $data = json_decode($rawData, true); if (json_last_error() !== JSON_ERROR_NONE) { echo json_encode(['success' => false, 'message' => '数据格式错误']); return; } $id = isset($data['id']) ? (int)$data['id'] : 0; $adminRemark = trim($data['admin_remark'] ?? ''); if ($id <= 0) { echo json_encode(['success' => false, 'message' => '无效的申请ID']); return; } $db = new Database(); try { // 查询申请记录 $request = $db->get('fund_requests', '*', ['id' => $id]); if (!$request) { echo json_encode(['success' => false, 'message' => '申请记录不存在']); return; } if ($request['status'] !== 'pending') { echo json_encode(['success' => false, 'message' => '该申请已处理,状态:' . $request['status']]); return; } $userId = (int)$request['user_id']; $amount = floatval($request['amount']); $type = $request['type']; // 获取用户信息 $user = $db->get('users', ['id', 'balance'], ['id' => $userId]); if (!$user) { echo json_encode(['success' => false, 'message' => '用户不存在']); return; } $balanceBefore = floatval($user['balance']); $now = date('Y-m-d H:i:s'); $operatorId = $_SESSION['user_id'] ?? 0; // 开启事务 $db->medoo->pdo->beginTransaction(); if ($type === 'deposit') { // 充值:给用户加款 $balanceAfter = $balanceBefore + $amount; $db->update('users', [ 'balance' => $balanceAfter, 'updated_at' => $now ], ['id' => $userId]); $db->insert('transactions', [ 'user_id' => $userId, 'type' => 'deposit', 'amount' => $amount, 'balance_before' => $balanceBefore, 'balance_after' => $balanceAfter, 'description' => '充值申请审核通过 #' . $id, 'created_at' => $now ]); } else { // 提现:余额已在申请时预扣,确认放款 $balanceAfter = $balanceBefore; // 余额不变,已预扣 $db->insert('transactions', [ 'user_id' => $userId, 'type' => 'withdraw', 'amount' => -$amount, 'balance_before' => $balanceBefore, 'balance_after' => $balanceAfter, 'description' => '提现申请审核通过 #' . $id, 'created_at' => $now ]); } // 更新申请状态 $db->update('fund_requests', [ 'status' => 'approved', 'admin_remark' => $adminRemark, 'operator_id' => $operatorId, 'processed_at' => $now ], ['id' => $id]); $db->medoo->pdo->commit(); echo json_encode(['success' => true, 'message' => '审批通过']); } catch (\Exception $e) { if ($db->medoo->pdo->inTransaction()) { $db->medoo->pdo->rollBack(); } echo json_encode(['success' => false, 'message' => '操作失败:' . $e->getMessage()]); } } /** * 拒绝申请 */ public function reject() { header('Content-Type: application/json'); $rawData = file_get_contents('php://input'); $data = json_decode($rawData, true); if (json_last_error() !== JSON_ERROR_NONE) { echo json_encode(['success' => false, 'message' => '数据格式错误']); return; } $id = isset($data['id']) ? (int)$data['id'] : 0; $adminRemark = trim($data['admin_remark'] ?? ''); if ($id <= 0) { echo json_encode(['success' => false, 'message' => '无效的申请ID']); return; } $db = new Database(); try { // 查询申请记录 $request = $db->get('fund_requests', '*', ['id' => $id]); if (!$request) { echo json_encode(['success' => false, 'message' => '申请记录不存在']); return; } if ($request['status'] !== 'pending') { echo json_encode(['success' => false, 'message' => '该申请已处理,状态:' . $request['status']]); return; } $userId = (int)$request['user_id']; $amount = floatval($request['amount']); $type = $request['type']; $now = date('Y-m-d H:i:s'); $operatorId = $_SESSION['user_id'] ?? 0; // 开启事务 $db->medoo->pdo->beginTransaction(); if ($type === 'withdraw') { // 提现拒绝:退还预扣余额 $user = $db->get('users', ['id', 'balance'], ['id' => $userId]); if ($user) { $balanceBefore = floatval($user['balance']); $balanceAfter = $balanceBefore + $amount; $db->update('users', [ 'balance' => $balanceAfter, 'updated_at' => $now ], ['id' => $userId]); $db->insert('transactions', [ 'user_id' => $userId, 'type' => 'refund', 'amount' => $amount, 'balance_before' => $balanceBefore, 'balance_after' => $balanceAfter, 'description' => '提现申请被拒绝,退还预扣金额 #' . $id, 'created_at' => $now ]); } } // 充值拒绝:无需退款 // 更新申请状态 $db->update('fund_requests', [ 'status' => 'rejected', 'admin_remark' => $adminRemark, 'operator_id' => $operatorId, 'processed_at' => $now ], ['id' => $id]); $db->medoo->pdo->commit(); echo json_encode(['success' => true, 'message' => '已拒绝']); } catch (\Exception $e) { if ($db->medoo->pdo->inTransaction()) { $db->medoo->pdo->rollBack(); } echo json_encode(['success' => false, 'message' => '操作失败:' . $e->getMessage()]); } } /** * 获取待处理数量(轮询接口) */ public function pendingCount() { header('Content-Type: application/json'); $db = new Database(); try { $count = $db->count('fund_requests', ['status' => 'pending']); echo json_encode(['count' => (int)$count]); } catch (\Throwable $e) { echo json_encode(['count' => 0]); } } }