342 lines
12 KiB
HTML
342 lines
12 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>API接口测试</title>
|
||
<style>
|
||
body {
|
||
font-family: Arial, sans-serif;
|
||
max-width: 800px;
|
||
margin: 50px auto;
|
||
padding: 20px;
|
||
background: #f5f5f5;
|
||
}
|
||
h1 {
|
||
color: #333;
|
||
text-align: center;
|
||
}
|
||
.test-section {
|
||
background: white;
|
||
padding: 20px;
|
||
margin-bottom: 20px;
|
||
border-radius: 8px;
|
||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||
}
|
||
.test-section h2 {
|
||
color: #2196F3;
|
||
margin-top: 0;
|
||
}
|
||
button {
|
||
background: #2196F3;
|
||
color: white;
|
||
border: none;
|
||
padding: 10px 20px;
|
||
border-radius: 4px;
|
||
cursor: pointer;
|
||
margin-right: 10px;
|
||
margin-bottom: 10px;
|
||
}
|
||
button:hover {
|
||
background: #1976D2;
|
||
}
|
||
.result {
|
||
background: #f5f5f5;
|
||
padding: 15px;
|
||
border-radius: 4px;
|
||
margin-top: 10px;
|
||
max-height: 400px;
|
||
overflow-y: auto;
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 12px;
|
||
}
|
||
.success {
|
||
color: #4CAF50;
|
||
}
|
||
.error {
|
||
color: #f44336;
|
||
}
|
||
input {
|
||
padding: 8px;
|
||
margin: 5px;
|
||
border: 1px solid #ddd;
|
||
border-radius: 4px;
|
||
width: 200px;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<h1>🔌 API接口测试工具</h1>
|
||
|
||
<!-- 环境信息 -->
|
||
<div class="test-section" style="background: #e3f2fd;">
|
||
<h2>🌍 环境信息</h2>
|
||
<div id="envInfo"></div>
|
||
<div style="margin-top: 15px;">
|
||
<h3>手动设置后端地址(可选)</h3>
|
||
<input type="text" id="customBaseURL" placeholder="例:http://localhost:8080/api/v1" style="width: 400px;">
|
||
<button onclick="setCustomBaseURL()">设置</button>
|
||
<button onclick="resetBaseURL()">重置为自动检测</button>
|
||
<button onclick="testConnection()">测试连接</button>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 认证API测试 -->
|
||
<div class="test-section">
|
||
<h2>1. 用户认证API</h2>
|
||
<div>
|
||
<h3>登录</h3>
|
||
<input type="email" id="loginEmail" placeholder="邮箱" value="test@example.com">
|
||
<input type="password" id="loginPassword" placeholder="密码" value="123456">
|
||
<button onclick="testLogin()">测试登录</button>
|
||
</div>
|
||
<div>
|
||
<h3>注册</h3>
|
||
<input type="email" id="regEmail" placeholder="邮箱" value="newuser@test.com">
|
||
<input type="password" id="regPassword" placeholder="密码" value="123456">
|
||
<input type="text" id="regNickname" placeholder="昵称" value="测试用户">
|
||
<button onclick="testRegister()">测试注册</button>
|
||
</div>
|
||
<div class="result" id="authResult"></div>
|
||
</div>
|
||
|
||
<!-- 商品API测试 -->
|
||
<div class="test-section">
|
||
<h2>2. 商品API</h2>
|
||
<button onclick="testGetProducts()">获取商品列表</button>
|
||
<button onclick="testGetProductDetail()">获取商品详情(ID:1)</button>
|
||
<button onclick="testGetHotProducts()">获取热门商品</button>
|
||
<div class="result" id="productResult"></div>
|
||
</div>
|
||
|
||
<!-- Banner API测试 -->
|
||
<div class="test-section">
|
||
<h2>3. Banner轮播图API</h2>
|
||
<button onclick="testGetBanners()">获取Banner列表</button>
|
||
<div class="result" id="bannerResult"></div>
|
||
</div>
|
||
|
||
<!-- 用户信息API测试(需要登录) -->
|
||
<div class="test-section">
|
||
<h2>4. 用户信息API(需要先登录)</h2>
|
||
<button onclick="testGetProfile()">获取用户信息</button>
|
||
<button onclick="testGetAddresses()">获取地址列表</button>
|
||
<div class="result" id="userResult"></div>
|
||
</div>
|
||
|
||
<!-- 购物车API测试(需要登录) -->
|
||
<div class="test-section">
|
||
<h2>5. 购物车API(需要先登录)</h2>
|
||
<button onclick="testGetCart()">获取购物车</button>
|
||
<button onclick="testAddToCart()">添加商品到购物车</button>
|
||
<div class="result" id="cartResult"></div>
|
||
</div>
|
||
|
||
<!-- Scripts -->
|
||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||
<script src="assets/js/api.js"></script>
|
||
|
||
<script>
|
||
// 显示结果
|
||
function showResult(elementId, data, isError = false) {
|
||
const element = document.getElementById(elementId);
|
||
const className = isError ? 'error' : 'success';
|
||
const status = isError ? '❌ 失败' : '✅ 成功';
|
||
element.innerHTML = `<div class="${className}"><strong>${status}</strong></div><pre>${JSON.stringify(data, null, 2)}</pre>`;
|
||
}
|
||
|
||
// 测试登录
|
||
function testLogin() {
|
||
const email = document.getElementById('loginEmail').value;
|
||
const password = document.getElementById('loginPassword').value;
|
||
|
||
showResult('authResult', { status: '正在请求...' });
|
||
|
||
UserAPI.login(email, password)
|
||
.then(data => {
|
||
showResult('authResult', data);
|
||
console.log('登录成功,Token:', API.getToken());
|
||
})
|
||
.catch(error => {
|
||
showResult('authResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试注册
|
||
function testRegister() {
|
||
const email = document.getElementById('regEmail').value;
|
||
const password = document.getElementById('regPassword').value;
|
||
const nickname = document.getElementById('regNickname').value;
|
||
|
||
showResult('authResult', { status: '正在请求...' });
|
||
|
||
UserAPI.register(email, password, nickname)
|
||
.then(data => {
|
||
showResult('authResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('authResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试获取商品列表
|
||
function testGetProducts() {
|
||
showResult('productResult', { status: '正在请求...' });
|
||
|
||
ProductAPI.getList({ page: 1, page_size: 10 })
|
||
.then(data => {
|
||
showResult('productResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('productResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试获取商品详情
|
||
function testGetProductDetail() {
|
||
showResult('productResult', { status: '正在请求...' });
|
||
|
||
ProductAPI.getDetail(1)
|
||
.then(data => {
|
||
showResult('productResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('productResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试获取热门商品
|
||
function testGetHotProducts() {
|
||
showResult('productResult', { status: '正在请求...' });
|
||
|
||
ProductAPI.getHot()
|
||
.then(data => {
|
||
showResult('productResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('productResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试获取Banner
|
||
function testGetBanners() {
|
||
showResult('bannerResult', { status: '正在请求...' });
|
||
|
||
BannerAPI.getList()
|
||
.then(data => {
|
||
showResult('bannerResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('bannerResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试获取用户信息
|
||
function testGetProfile() {
|
||
showResult('userResult', { status: '正在请求...' });
|
||
|
||
UserAPI.getProfile()
|
||
.then(data => {
|
||
showResult('userResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('userResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试获取地址列表
|
||
function testGetAddresses() {
|
||
showResult('userResult', { status: '正在请求...' });
|
||
|
||
UserAPI.getAddresses()
|
||
.then(data => {
|
||
showResult('userResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('userResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试获取购物车
|
||
function testGetCart() {
|
||
showResult('cartResult', { status: '正在请求...' });
|
||
|
||
CartAPI.getCart()
|
||
.then(data => {
|
||
showResult('cartResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('cartResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 测试添加到购物车
|
||
function testAddToCart() {
|
||
showResult('cartResult', { status: '正在请求...' });
|
||
|
||
CartAPI.addToCart(1, 1, 1)
|
||
.then(data => {
|
||
showResult('cartResult', data);
|
||
})
|
||
.catch(error => {
|
||
showResult('cartResult', error, true);
|
||
});
|
||
}
|
||
|
||
// 页面加载完成
|
||
$(document).ready(function() {
|
||
console.log('API测试工具已加载');
|
||
|
||
// 显示环境信息
|
||
updateEnvInfo();
|
||
});
|
||
|
||
// 更新环境信息显示
|
||
function updateEnvInfo() {
|
||
const config = API_DEBUG.getConfig();
|
||
const html = `
|
||
<p><strong>当前环境:</strong> <span class="${config.environment === 'development' ? 'success' : 'error'}">${config.environment === 'development' ? '🟢 开发环境' : '🔴 生产环境'}</span></p>
|
||
<p><strong>后端地址:</strong> <code>${config.baseURL}</code></p>
|
||
<p><strong>配置模式:</strong> ${config.manualConfig === 'auto' ? '✅ 自动检测' : '🔧 手动配置'}</p>
|
||
<p><strong>当前URL:</strong> <code>${config.currentURL}</code></p>
|
||
`;
|
||
document.getElementById('envInfo').innerHTML = html;
|
||
}
|
||
|
||
// 设置自定义后端地址
|
||
function setCustomBaseURL() {
|
||
const url = document.getElementById('customBaseURL').value.trim();
|
||
if (!url) {
|
||
alert('请输入后端地址');
|
||
return;
|
||
}
|
||
|
||
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
||
alert('请输入完整的URL(包含http://或https://)');
|
||
return;
|
||
}
|
||
|
||
API_DEBUG.setBaseURL(url);
|
||
alert('后端地址已设置,即将刷新页面...');
|
||
setTimeout(() => {
|
||
window.location.reload();
|
||
}, 1000);
|
||
}
|
||
|
||
// 重置为自动检测
|
||
function resetBaseURL() {
|
||
API_DEBUG.resetBaseURL();
|
||
alert('已重置为自动检测模式,即将刷新页面...');
|
||
setTimeout(() => {
|
||
window.location.reload();
|
||
}, 1000);
|
||
}
|
||
|
||
// 测试连接
|
||
function testConnection() {
|
||
API_DEBUG.testConnection();
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|