Files
PerToolBoxFront/pages/json.php
2026-03-31 15:54:32 +08:00

82 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* PerToolBox Front - JSON 校验器页面
*
* Copyright (C) 2024 Sea Network Technology Studio
* Author: Canglan <admin@sea-studio.top>
* License: AGPL v3
*/
require_once '../config.php';
include_once '../header.php';
include_once '../sidebar.php';
?>
<div class="main-content">
<div class="card">
<h1 class="text-2xl font-bold mb-6">📋 JSON 校验器</h1>
<div class="mb-4">
<label class="form-label">输入 JSON</label>
<textarea id="jsonInput" rows="10" placeholder='{"key": "value"}' class="form-input font-mono text-sm"></textarea>
</div>
<button id="validateBtn" class="btn btn-primary mb-6">校验并格式化</button>
<div id="resultArea" class="hidden">
<div class="flex justify-between items-center mb-2">
<h3 class="font-semibold">结果:</h3>
<button id="copyBtn" class="text-sm text-blue-500 hover:text-blue-700">复制</button>
</div>
<pre id="jsonResult" class="bg-gray-50 p-4 rounded-lg overflow-x-auto font-mono text-sm"></pre>
</div>
<div id="errorArea" class="hidden">
<div class="bg-red-50 border border-red-200 rounded-lg p-4">
<div class="text-red-600 font-semibold mb-2">❌ JSON 无效</div>
<div id="errorMsg" class="text-red-500 text-sm"></div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('validateBtn').addEventListener('click', async () => {
const jsonString = document.getElementById('jsonInput').value;
if (!jsonString) {
showToast('请输入 JSON', 'error');
return;
}
try {
const data = await apiRequest('/json/validate', {
method: 'POST',
body: JSON.stringify({ json_string: jsonString })
});
if (data.valid) {
document.getElementById('resultArea').classList.remove('hidden');
document.getElementById('errorArea').classList.add('hidden');
document.getElementById('jsonResult').textContent = data.formatted;
} else {
document.getElementById('resultArea').classList.add('hidden');
document.getElementById('errorArea').classList.remove('hidden');
document.getElementById('errorMsg').textContent = `第 ${data.error.line} 行,第 ${data.error.column} 列:${data.error.message}`;
}
} catch (error) {
showToast(error.message, 'error');
}
});
document.getElementById('copyBtn').addEventListener('click', () => {
const result = document.getElementById('jsonResult').textContent;
if (result) {
navigator.clipboard.writeText(result);
showToast('已复制到剪贴板');
}
});
recordUsage('json');
</script>
<?php include_once '../footer.php'; ?>