Files
zydi-user/docs.js
T
2025-01-12 05:13:22 +00:00

96 lines
3.3 KiB
JavaScript

// 显示API文档
// 在文件开头添加这个函数
function i18n(key) {
return lang[selectedLang][key] || key;
}
async function showApiDocs() { // 添加 async 关键字
try {
const userInfo = await fetchUserInfo();
if (!userInfo) {
alert(i18n('pleaseLogin'));
window.location.href = 'login.html'; // 重定向到登录页面
return;
}
// 检查邮箱验证状态
const emailVerified = userInfo.email_verified === 'True' || userInfo.email_verified === true;
console.log('Email Verified:', emailVerified); // 打印邮箱验证状态
if (!emailVerified) {
alert(i18n('pleaseVerifyEmail'));
showUserCenterSection('profile'); // 跳转到个人资料页面
return;
}
const dashboardContent = document.getElementById('dashboardContent');
const usersContent = document.getElementById('usersContent');
const chatContent = document.getElementById('chatContent');
const featureContent = document.getElementById('featureContent');
const userCenterContent = document.getElementById('userCenterContent');
const apiDocsContent = document.getElementById('apiDocsContent'); // 新增这一行
const pageTitle = document.getElementById('pageTitle');
if (dashboardContent) dashboardContent.style.display = 'none';
if (usersContent) usersContent.style.display = 'none';
if (chatContent) chatContent.style.display = 'none';
if (featureContent) featureContent.style.display = 'none';
if (userCenterContent) userCenterContent.style.display = 'none';
if (apiDocsContent) apiDocsContent.style.display = 'block'; // 新增这一行
if (pageTitle) {
pageTitle.innerText = i18n('apiDocs');
} // 添加缺失的闭合大括号
} catch (error) {
console.error('Error in showApiDocs:', error); // 修改错误信息
alert(i18n('getUserInfoFailed') + error);
}
}
// 初始化API文档
function initApiDocs() {
const docLink = document.querySelector('a[href="#"][onclick="showApiDocs()"]');
if (docLink) {
docLink.onclick = (e) => {
e.preventDefault();
showApiDocs();
};
}
}
// 页面加载完成后初始化API文档
window.addEventListener('DOMContentLoaded', initApiDocs);
// 将函数添加到全局作用域
window.showApiDocs = showApiDocs;
// 获取用户信息的函数
async function fetchUserInfo() {
try {
const token = localStorage.getItem('access_token');
if (!token) {
console.error('No access token found');
return null;
}
const response = await fetch(`${BASE_URL}/me`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(i18n('failedToFetchUserInfo'));
}
const userInfo = await response.json();
return userInfo;
} catch (error) {
console.error('Error fetching user info:', error);
// 可以在这里处理错误,比如清除 token 并重定向到登录页面
localStorage.removeItem('access_token');
window.location.href = 'login.html'; // 重定向到登录页面
return null;
}
}