58 lines
2.3 KiB
PHP
58 lines
2.3 KiB
PHP
<?php
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Core\AdminBaseController;
|
|
use Db\Database;
|
|
|
|
class VirtualAccountController extends AdminBaseController {
|
|
private $db;
|
|
public function __construct(Database $db) { $this->db = $db; }
|
|
|
|
public function index() {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$virtuals = $this->db->select('users', '*', ['is_virtual' => 1, 'ORDER' => ['id' => 'DESC']]);
|
|
$this->render('Admin/virtual_accounts.php', compact('virtuals'));
|
|
}
|
|
|
|
public function create() {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$username = trim($data['username'] ?? '');
|
|
if (empty($username)) { $this->json(['status'=>'error','message'=>'Username required']); return; }
|
|
if ($this->db->get('users', 'id', ['username' => $username])) {
|
|
$this->json(['status'=>'error','message'=>'Username exists']); return;
|
|
}
|
|
|
|
$this->db->insert('users', [
|
|
'username' => $username,
|
|
'password' => password_hash($data['password'] ?? '123456', PASSWORD_DEFAULT),
|
|
'email' => $username . '@virtual.local',
|
|
'role' => 'user', 'status' => 1, 'is_virtual' => 1,
|
|
'email_verified' => 1,
|
|
'balance' => floatval($data['balance'] ?? 100000),
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
]);
|
|
$this->json(['status' => 'success']);
|
|
}
|
|
|
|
public function adjustBalance() {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$id = (int)($data['id'] ?? 0);
|
|
$amount = floatval($data['amount'] ?? 0);
|
|
$user = $this->db->get('users', '*', ['id' => $id, 'is_virtual' => 1]);
|
|
if (!$user) { $this->json(['status'=>'error','message'=>'Not found']); return; }
|
|
$new = (float)$user['balance'] + $amount;
|
|
$this->db->update('users', ['balance' => $new], ['id' => $id]);
|
|
$this->json(['status' => 'success', 'new_balance' => $new]);
|
|
}
|
|
|
|
public function delete($id) {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$this->db->delete('users', ['id' => (int)$id, 'is_virtual' => 1]);
|
|
$this->json(['status' => 'success']);
|
|
}
|
|
|
|
private function json($data) { header('Content-Type: application/json'); echo json_encode($data); }
|
|
}
|