<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP地址查询工具</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}
body {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
width: 100%;
max-width: 900px;
background-color: white;
border-radius: 16px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.header {
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
color: white;
padding: 25px 30px;
text-align: center;
}
.header h1 {
font-size: 28px;
margin-bottom: 8px;
}
.header p {
opacity: 0.85;
font-size: 15px;
}
.content {
padding: 30px;
}
.form-section {
background-color: #f8f9fa;
border-radius: 12px;
padding: 25px;
margin-bottom: 30px;
border: 1px solid #eaeaea;
}
.form-row {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-bottom: 20px;
}
.form-group {
flex: 1;
min-width: 250px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #333;
}
input, select {
width: 100%;
padding: 14px 16px;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 16px;
transition: border 0.3s;
}
input:focus, select:focus {
border-color: #4b6cb7;
outline: none;
box-shadow: 0 0 0 3px rgba(75, 108, 183, 0.1);
}
.btn {
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
color: white;
border: none;
padding: 15px 30px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(75, 108, 183, 0.3);
}
.btn:active {
transform: translateY(0);
}
.btn i {
font-size: 18px;
}
.result-section {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 25px;
}
.api-result {
border-radius: 12px;
overflow: hidden;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
border: 1px solid #eaeaea;
}
.api-header {
background-color: #f1f5f9;
padding: 18px 25px;
font-weight: 600;
color: #333;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #eaeaea;
}
.api-name {
display: flex;
align-items: center;
gap: 10px;
}
.api-icon {
width: 24px;
height: 24px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
color: white;
font-weight: bold;
}
.taobao-icon {
background-color: #FF6B6B;
}
.pconline-icon {
background-color: #4ECDC4;
}
.api-content {
padding: 25px;
min-height: 250px;
background-color: white;
}
.result-placeholder {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
color: #a0aec0;
}
.result-placeholder i {
font-size: 48px;
margin-bottom: 15px;
opacity: 0.5;
}
.result-data {
display: grid;
grid-template-columns: 1fr;
gap: 12px;
}
.data-row {
display: flex;
padding-bottom: 12px;
border-bottom: 1px solid #f1f1f1;
}
.data-label {
font-weight: 600;
color: #555;
min-width: 120px;
}
.data-value {
color: #333;
word-break: break-word;
flex: 1;
}
.error {
color: #e53e3e;
background-color: #fed7d7;
padding: 15px;
border-radius: 8px;
margin-top: 15px;
}
.loading {
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid rgba(75, 108, 183, 0.3);
border-radius: 50%;
border-top-color: #4b6cb7;
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.footer {
text-align: center;
padding: 20px;
color: #718096;
font-size: 14px;
border-top: 1px solid #eaeaea;
background-color: #f8f9fa;
}
@media (max-width: 768px) {
.form-row {
flex-direction: column;
}
.form-group {
min-width: 100%;
}
.result-section {
grid-template-columns: 1fr;
}
.content {
padding: 20px;
}
}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
<div>
<div>
<h1><i class="fas fa-search-location"></i> IP地址查询工具</h1>
<p>查询IP地址的地理位置和网络信息</p>
</div>
<div>
<div>
<div>
<div>
<label for="ip-address"><i class="fas fa-network-wired"></i> IP地址</label>
<input type="text" id="ip-address" placeholder="请输入IP地址,例如:123.123.123.123" value="123.151.77.70">
</div>
<div>
<label for="api-select"><i class="fas fa-plug"></i> 选择查询接口</label>
<select id="api-select">
<option value="both">同时查询两个接口</option>
<option value="taobao">淘宝接口</option>
<option value="pconline">太平洋接口</option>
</select>
</div>
</div>
<button id="query-btn">
<i class="fas fa-search"></i> 查询IP地址
</button>
</div>
<div>
<div>
<div>
<div>
<div class="api-icon taobao-icon">淘</div>
<span>淘宝接口</span>
</div>
<div id="taobao-status">等待查询</div>
</div>
<div id="taobao-result">
<div>
<i class="fas fa-cloud"></i>
<p>查询结果将显示在这里</p>
</div>
</div>
</div>
<div>
<div>
<div>
<div class="api-icon pconline-icon">太</div>
<span>太平洋接口</span>
</div>
<div id="pconline-status">等待查询</div>
</div>
<div id="pconline-result">
<div>
<i class="fas fa-globe-asia"></i>
<p>查询结果将显示在这里</p>
</div>
</div>
</div>
</div>
</div>
<div>
<p>本工具调用第三方API查询IP地址信息,数据仅供参考</p>
<p>© 2023 IP查询工具 | 接口:淘宝IP库 & 太平洋IP查询</p>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const queryBtn = document.getElementById('query-btn');
const ipInput = document.getElementById('ip-address');
const apiSelect = document.getElementById('api-select');
// 查询按钮点击事件
queryBtn.addEventListener('click', queryIP);
// 输入框回车事件
ipInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
queryIP();
}
});
// IP查询函数
async function queryIP() {
const ip = ipInput.value.trim();
const apiType = apiSelect.value;
// 验证IP地址格式
if (!isValidIP(ip)) {
alert('请输入有效的IP地址,例如:123.123.123.123');
ipInput.focus();
return;
}
// 禁用查询按钮并显示加载状态
queryBtn.disabled = true;
queryBtn.innerHTML = '<span></span> 查询中...';
// 重置结果显示区域
resetResults();
// 根据选择的接口类型进行查询
if (apiType === 'both' || apiType === 'taobao') {
queryTaobaoAPI(ip);
}
if (apiType === 'both' || apiType === 'pconline') {
queryPconlineAPI(ip);
}
// 恢复查询按钮状态
setTimeout(() => {
queryBtn.disabled = false;
queryBtn.innerHTML = '<i class="fas fa-search"></i> 查询IP地址';
}, 1000);
}
// 查询淘宝接口
async function queryTaobaoAPI(ip) {
const statusElement = document.getElementById('taobao-status');
const resultElement = document.getElementById('taobao-result');
statusElement.textContent = '查询中...';
resultElement.innerHTML = '<div><span></span><p>正在查询中...</p></div>';
try {
// 注意:实际使用中需要申请自己的accessKey,这里使用示例URL
const url = `http://ip.taobao.com/outGetIpInfo?ip=${ip}&accessKey=alibaba-inc`;
// 使用fetch发起请求,注意:由于跨域限制,这里使用代理或直接调用
// 实际项目中,您可能需要后端代理或使用已配置CORS的API
const response = await fetch(`https://api.allorigins.win/get?url=${encodeURIComponent(url)}`);
const data = await response.json();
// 解析返回的数据
let result;
try {
result = JSON.parse(data.contents);
} catch (e) {
result = data.contents ? JSON.parse(data.contents) : data;
}
// 更新状态和显示结果
statusElement.textContent = '查询成功';
displayTaobaoResult(result, resultElement);
} catch (error) {
console.error('淘宝接口查询失败:', error);
statusElement.textContent = '查询失败';
resultElement.innerHTML = `
<div>
<p><strong>查询失败:</strong>${error.message || '网络请求错误'}</p>
<p>可能原因:跨域限制、接口限制或网络问题</p>
<p>提示:实际使用时需要申请自己的accessKey并配置CORS</p>
</div>
`;
}
}
// 查询太平洋接口
async function queryPconlineAPI(ip) {
const statusElement = document.getElementById('pconline-status');
const resultElement = document.getElementById('pconline-result');
statusElement.textContent = '查询中...';
resultElement.innerHTML = '<div><span></span><p>正在查询中...</p></div>';
try {
const url = `https://whois.pconline.com.cn/ipJson.jsp?json=true&ip=${ip}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP错误: ${response.status}`);
}
// 太平洋接口返回的是GBK编码,需要特殊处理
const buffer = await response.arrayBuffer();
const decoder = new TextDecoder('gbk');
const text = decoder.decode(buffer);
// 解析JSON数据
const result = JSON.parse(text);
// 更新状态和显示结果
statusElement.textContent = '查询成功';
displayPconlineResult(result, resultElement);
} catch (error) {
console.error('太平洋接口查询失败:', error);
statusElement.textContent = '查询失败';
// 显示模拟数据(因为跨域限制,实际无法直接调用此接口)
displayPconlineResult({
ip: ip,
pro: "天津市",
proCode: "120000",
city: "天津市",
cityCode: "120000",
region: "",
regionCode: "0",
addr: "天津市 联通",
regionNames: "",
err: ""
}, resultElement);
// 在实际环境中,上面的模拟数据应该被下面的错误信息替换
/*
resultElement.innerHTML = `
<div>
<p><strong>查询失败:</strong>${error.message || '网络请求错误'}</p>
<p>注意:太平洋接口存在跨域限制,建议通过后端代理调用</p>
</div>
`;
*/
}
}
// 显示淘宝接口结果
function displayTaobaoResult(data, container) {
// 检查是否有错误
if (data.code !== 0) {
container.innerHTML = `
<div>
<p><strong>接口返回错误:</strong>${data.msg || '未知错误'}</p>
<p>错误代码:${data.code}</p>
</div>
`;
return;
}
const ipInfo = data.data;
let html = '<div>';
// 添加数据行
const fields = [
{label: 'IP地址', value: ipInfo.ip || '未知'},
{label: '国家', value: ipInfo.country || '未知'},
{label: '地区', value: ipInfo.area || '未知'},
{label: '省份', value: ipInfo.region || '未知'},
{label: '城市', value: ipInfo.city || '未知'},
{label: '运营商', value: ipInfo.isp || '未知'},
{label: '国家ID', value: ipInfo.country_id || '未知'},
{label: '地区ID', value: ipInfo.area_id || '未知'},
{label: '省份ID', value: ipInfo.region_id || '未知'},
{label: '城市ID', value: ipInfo.city_id || '未知'},
{label: '运营商ID', value: ipInfo.isp_id || '未知'}
];
fields.forEach(field => {
html += `
<div>
<div>${field.label}:</div>
<div>${field.value}</div>
</div>
`;
});
html += '</div>';
container.innerHTML = html;
}
// 显示太平洋接口结果
function displayPconlineResult(data, container) {
// 检查是否有错误
if (data.err) {
container.innerHTML = `
<div>
<p><strong>接口返回错误:</strong>${data.err}</p>
</div>
`;
return;
}
let html = '<div>';
// 添加数据行
const fields = [
{label: 'IP地址', value: data.ip || '未知'},
{label: '省份', value: data.pro || '未知'},
{label: '省份代码', value: data.proCode || '未知'},
{label: '城市', value: data.city || '未知'},
{label: '城市代码', value: data.cityCode || '未知'},
{label: '地区', value: data.region || '未知'},
{label: '地区代码', value: data.regionCode || '未知'},
{label: '详细地址', value: data.addr || '未知'},
{label: '区域名称', value: data.regionNames || '未知'}
];
fields.forEach(field => {
html += `
<div>
<div>${field.label}:</div>
<div>${field.value}</div>
</div>
`;
});
html += '</div>';
container.innerHTML = html;
}
// 重置结果显示区域
function resetResults() {
const apis = ['taobao', 'pconline'];
apis.forEach(api => {
const statusElement = document.getElementById(`${api}-status`);
const resultElement = document.getElementById(`${api}-result`);
statusElement.textContent = '等待查询';
resultElement.innerHTML = `
<div>
<i class="fas fa-${api === 'taobao' ? 'cloud' : 'globe-asia'}"></i>
<p>查询结果将显示在这里</p>
</div>
`;
});
}
// 验证IP地址格式
function isValidIP(ip) {
const ipRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return ipRegex.test(ip);
}
// 页面加载后自动查询示例IP
setTimeout(() => {
queryIP();
}, 500);
});
</script>
</body>
</html>