域名
为了提升前端和后台的界面美观度,我们可以使用Bootstrap框架来实现。同时,通过API调用汇率来自动设置不同货币的价格,并在后台提供可视化编辑功能。
项目目录结构(更新后)
domain-sales/
├── index.php
├── buy.php
├── admin/
│ ├── index.php
│ ├── login.php
│ ├── logout.php
│ ├── config.php
│ ├── add_domain.php
│ ├── update_domain.php
│ ├── settings.php
├── data/
│ ├── domains.json
│ ├── settings.json
├── css/
│ └── styles.css
数据存储格式
domains.json
[
{
"domain": "example.com",
"account": "account@example.com",
"password": "password",
"price": 100, // 价格以USDT为基础
"currency": "USDT"
}
]
settings.json
{
"eth_address": "0xYourEthAddress",
"btc_address": "YourBtcAddress",
"usdc_address": "YourUsdcAddress",
"usdt_address": "YourUsdtAddress",
"bch_address": "YourBchAddress",
"doge_address": "YourDogeAddress",
"admin_password": "hashed_admin_password",
"api_key": "your_exchange_rate_api_key" // 汇率API密钥
}
样式文件
css/styles.css
body {
padding-top: 50px;
}
.navbar {
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
.container {
max-width: 800px;
}
h1, h2 {
margin-bottom: 20px;
}
前端显示域名列表和购买功能
index.php
<?php
$domains = json_decode(file_get_contents('data/domains.json'), true);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>域名售卖</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">域名售卖</a>
</nav>
<div class="container">
<h1>域名列表</h1>
<div class="list-group">
<?php foreach ($domains as $domain): ?>
<a href="buy.php?domain=<?php echo urlencode($domain['domain']); ?>" class="list-group-item list-group-item-action">
<?php echo htmlspecialchars($domain['domain']); ?>
- 价格: <?php echo htmlspecialchars($domain['price']); ?> USDT
</a>
<?php endforeach; ?>
</div>
</div>
</body>
</html>
buy.php
<?php
$domain_name = $_GET['domain'];
$domains = json_decode(file_get_contents('data/domains.json'), true);
$settings = json_decode(file_get_contents('data/settings.json'), true);
$domain = null;
foreach ($domains as $d) {
if ($d['domain'] === $domain_name) {
$domain = $d;
break;
}
}
if ($domain === null) {
die('域名不存在');
}
// 获取不同货币的支付地址
$currency_address = [
'USDT' => $settings['usdt_address'],
'ETH' => $settings['eth_address'],
'BTC' => $settings['btc_address'],
'USDC' => $settings['usdc_address'],
'BCH' => $settings['bch_address'],
'DOGE' => $settings['doge_address'],
];
$exchange_rates = json_decode(file_get_contents('https://api.exchangerate-api.com/v4/latest/USDT'), true);
$price_in_usdt = $domain['price'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购买 <?php echo htmlspecialchars($domain_name); ?></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">域名售卖</a>
</nav>
<div class="container">
<h1>购买 <?php echo htmlspecialchars($domain_name); ?></h1>
<p>价格: <?php echo htmlspecialchars($price_in_usdt); ?> USDT</p>
<form method="POST">
<div class="form-group">
<label for="account">请输入您的账号:</label>
<input type="text" class="form-control" id="account" name="account" required>
</div>
<div class="form-group">
<label for="currency">选择支付货币:</label>
<select class="form-control" id="currency" name="currency">
<?php foreach ($currency_address as $currency => $address): ?>
<option value="<?php echo htmlspecialchars($currency); ?>"><?php echo htmlspecialchars($currency); ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$account = $_POST['account'];
$currency = $_POST['currency'];
$address = $currency_address[$currency];
// 调用第三方API检测转账情况,并处理购买逻辑
// 示例:假设成功付款
echo "<p>支付地址: " . htmlspecialchars($address) . "</p>";
echo "<p>购买成功,您的账号是: " . htmlspecialchars($account) . "</p>";
}
?>
</div>
</body>
</html>
后台管理功能
admin/login.php
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$password = $_POST['password'];
$settings = json_decode(file_get_contents('../data/settings.json'), true);
if (password_verify($password, $settings['admin_password'])) {
$_SESSION['admin_logged_in'] = true;
header('Location: index.php');
exit;
} else {
$error = "密码错误";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理员登录</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<div class="container">
<h1>管理员登录</h1>
<?php if (isset($error)) echo "<div class='alert alert-danger'>$error</div>"; ?>
<form method="POST">
<div class="form-group">
<label for="password">密码:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">登录</button>
</form>
</div>
</body>
</html>
admin/index.php
<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
header('Location: login.php');
exit;
}
$domains = json_decode(file_get_contents('../data/domains.json'), true);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理员后台</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">后台管理</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="settings.php">设置</a></li>
<li class="nav-item"><a class="nav-link" href="logout.php">登出</a></li>
</ul>
</div>
</nav>
<div class="container">
<h1>域名管理</h1>
<a href="add_domain.php" class="btn btn-primary mb-3">添加域名</a>
<div class="list-group">
<?php foreach ($domains as $domain): ?>
<a href="update_domain.php?domain=<?php echo urlencode($domain['domain']); ?>" class="list-group-item list-group-item-action">
<?php echo htmlspecialchars($domain['domain']); ?>
</a>
<?php endforeach; ?>
</div>
</div>
</body>
</html>
admin/add_domain.php
<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
header('Location: login.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$new_domain = [
'domain' => $_POST['domain'],
'account' => $_POST['account'],
'password' => $_POST['password'],
'price' => (float) $_POST['price'],
'currency' => 'USDT'
];
$domains = json_decode(file_get_contents('../data/domains.json'), true);
$domains[] = $new_domain;
file_put_contents('../data/domains.json', json_encode($domains));
header('Location: index.php');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加域名</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<div class="container">
<h1>添加域名</h1>
<form method="POST">
<div class="form-group">
<label for="domain">域名:</label>
<input type="text" class="form-control" id="domain" name="domain" required>
</div>
<div class="form-group">
<label for="account">所属账号:</label>
<input type="text" class="form-control" id="account" name="account" required>
</div>
<div class="form-group">
<label for="password">账号密码:</label>
<input type="text" class="form-control" id="password" name="password" required>
</div>
<div class="form-group">
<label for="price">价格 (USDT):</label>
<input type="number" class="form-control" id="price" name="price" step="0.01" required>
</div>
<button type="submit" class="btn btn-primary">添加</button>
</form>
<a href="index.php" class="btn btn-secondary mt-3">返回</a>
</div>
</body>
</html>
admin/settings.php
<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
header('Location: login.php');
exit;
}
$settings = json_decode(file_get_contents('../data/settings.json'), true);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$settings = [
'eth_address' => $_POST['eth_address'],
'btc_address' => $_POST['btc_address'],
'usdc_address' => $_POST['usdc_address'],
'usdt_address' => $_POST['usdt_address'],
'bch_address' => $_POST['bch_address'],
'doge_address' => $_POST['doge_address'],
'admin_password' => $settings['admin_password'],
'api_key' => $settings['api_key']
];
if (!empty($_POST['admin_password'])) {
$settings['admin_password'] = password_hash($_POST['admin_password'], PASSWORD_DEFAULT);
}
file_put_contents('../data/settings.json', json_encode($settings));
$message = "设置已更新";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>设置</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<div class="container">
<h1>设置</h1>
<?php if (isset($message)) echo "<div class='alert alert-success'>$message</div>"; ?>
<form method="POST">
<div class="form-group">
<label for="eth_address">ETH 地址:</label>
<input type="text" class="form-control" id="eth_address" name="eth_address" value="<?php echo htmlspecialchars($settings['eth_address']); ?>" required>
</div>
<div class="form-group">
<label for="btc_address">BTC 地址:</label>
<input type="text" class="form-control" id="btc_address" name="btc_address" value="<?php echo htmlspecialchars($settings['btc_address']); ?>" required>
</div>
<div class="form-group">
<label for="usdc_address">USDC 地址:</label>
<input type="text" class="form-control" id="usdc_address" name="usdc_address" value="<?php echo htmlspecialchars($settings['usdc_address']); ?>" required>
</div>
<div class="form-group">
<label for="usdt_address">USDT 地址:</label>
<input type="text" class="form-control" id="usdt_address" name="usdt_address" value="<?php echo htmlspecialchars($settings['usdt_address']); ?>" required>
</div>
<div class="form-group">
<label for="bch_address">BCH 地址:</label>
<input type="text" class="form-control" id="bch_address" name="bch_address" value="<?php echo htmlspecialchars($settings['bch_address']); ?>" required>
</div>
<div class="form-group">
<label for="doge_address">DOGE 地址:</label>
<input type="text" class="form-control" id="doge_address" name="doge_address" value="<?php echo htmlspecialchars($settings['doge_address']); ?>" required>
</div>
<div class="form-group">
<label for="admin_password">新管理员密码:</label>
<input type="password" class="form-control" id="admin_password" name="admin_password">
</div>
<button type="submit" class="btn btn-primary">更新设置</button>
</form>
<a href="index.php" class="btn btn-secondary mt-3">返回</a>
</div>
</body>
</html>
admin/logout.php
<?php
session_start();
session_destroy();
header('Location: login.php');
exit;
?>
使用汇率API更新价格
为了自动更新价格,可以在管理员登录时或在后台页面自动调用汇率API,并更新 domains.json 中的价格。
admin/config.php
<?php
function updatePrices() {
$settings = json_decode(file_get_contents('../data/settings.json'), true);
$api_key = $settings['api_key'];
// 使用示例API URL,实际使用时请替换为你所使用的API
$api_url = 'https://api.exchangerate-api.com/v4/latest/USDT';
$exchange_rates = json_decode(file_get_contents($api_url), true);
$domains = json_decode(file_get_contents('../data/domains.json'), true);
foreach ($domains as &$domain) {
$price_in_usdt = $domain['price'];
$currency = $domain['currency'];
if (isset($exchange_rates['rates'][$currency])) {
$domain['price'] = $price_in_usdt / $exchange_rates['rates'][$currency];
}
}
file_put_contents('../data/domains.json', json_encode($domains));
}
?>
更新 admin/index.php 中的代码
在 admin/index.php 中,调用 updatePrices() 函数来更新价格。
<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
header('Location: login.php');
exit;
}
include('config.php');
updatePrices();
$domains = json_decode(file_get_contents('../data/domains.json'), true);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理员后台</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">后台管理</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="settings.php">设置</a></li>
<li class="nav-item"><a class="nav-link" href="logout.php">登出</a></li>
</ul>
</div>
</nav>
<div class="container">
<h1>域名管理</h1>
<a href="add_domain.php" class="btn btn-primary mb-3">添加域名</a>
<div class="list-group">
<?php foreach ($domains as $domain): ?>
<a href="update_domain.php?domain=<?php echo urlencode($domain['domain']); ?>" class="list-group-item list-group-item-action">
<?php echo htmlspecialchars($domain['domain']); ?>
</a>
<?php endforeach; ?>
</div>
</div>
</body>
</html>
通过上述代码,网站前端和后台管理界面将更加美观,并且能够自动更新不同加密货币的价格。管理员可以
通过可视化界面管理域名和支付地址,提升用户体验。
