184 lines
4.9 KiB
JavaScript
184 lines
4.9 KiB
JavaScript
// NAC资产一键上链系统 - 主JS脚本
|
||
|
||
// API基础URL
|
||
const API_BASE_URL = '/api';
|
||
|
||
// 工具函数:获取JWT Token
|
||
function getToken() {
|
||
return localStorage.getItem('token');
|
||
}
|
||
|
||
// 工具函数:设置JWT Token
|
||
function setToken(token) {
|
||
localStorage.setItem('token', token);
|
||
}
|
||
|
||
// 工具函数:清除JWT Token
|
||
function clearToken() {
|
||
localStorage.removeItem('token');
|
||
localStorage.removeItem('user');
|
||
}
|
||
|
||
// 工具函数:获取用户信息
|
||
function getUser() {
|
||
const userStr = localStorage.getItem('user');
|
||
return userStr ? JSON.parse(userStr) : null;
|
||
}
|
||
|
||
// 工具函数:设置用户信息
|
||
function setUser(user) {
|
||
localStorage.setItem('user', JSON.stringify(user));
|
||
}
|
||
|
||
// 工具函数:API请求
|
||
async function apiRequest(endpoint, options = {}) {
|
||
const token = getToken();
|
||
const headers = {
|
||
'Content-Type': 'application/json',
|
||
...options.headers,
|
||
};
|
||
|
||
if (token) {
|
||
headers['Authorization'] = `Bearer ${token}`;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
||
...options,
|
||
headers,
|
||
});
|
||
|
||
const data = await response.json();
|
||
|
||
if (!response.ok) {
|
||
throw new Error(data.message || '请求失败');
|
||
}
|
||
|
||
return data;
|
||
} catch (error) {
|
||
console.error('API请求错误:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 工具函数:显示提示消息
|
||
function showAlert(message, type = 'info') {
|
||
const alertDiv = document.createElement('div');
|
||
alertDiv.className = `alert alert-${type}`;
|
||
alertDiv.textContent = message;
|
||
alertDiv.style.position = 'fixed';
|
||
alertDiv.style.top = '20px';
|
||
alertDiv.style.right = '20px';
|
||
alertDiv.style.zIndex = '3000';
|
||
alertDiv.style.minWidth = '300px';
|
||
|
||
document.body.appendChild(alertDiv);
|
||
|
||
setTimeout(() => {
|
||
alertDiv.remove();
|
||
}, 3000);
|
||
}
|
||
|
||
// 工具函数:格式化日期
|
||
function formatDate(dateString) {
|
||
const date = new Date(dateString);
|
||
return date.toLocaleString('zh-CN');
|
||
}
|
||
|
||
// 工具函数:格式化状态
|
||
function formatState(state) {
|
||
const stateMap = {
|
||
'Pending': '待处理',
|
||
'ComplianceChecking': 'AI合规审批中',
|
||
'Valuating': 'AI估值中',
|
||
'GeneratingDNA': '生成DNA中',
|
||
'Custodying': '托管对接中',
|
||
'MintingXTZH': 'XTZH铸造中',
|
||
'IssuingToken': '代币发行中',
|
||
'Listing': '链上公示中',
|
||
'Listed': '已上链',
|
||
'Failed': '失败',
|
||
};
|
||
return stateMap[state] || state;
|
||
}
|
||
|
||
// 工具函数:获取状态样式类
|
||
function getStateClass(state) {
|
||
const classMap = {
|
||
'Pending': 'status-pending',
|
||
'ComplianceChecking': 'status-processing',
|
||
'Valuating': 'status-processing',
|
||
'GeneratingDNA': 'status-processing',
|
||
'Custodying': 'status-processing',
|
||
'MintingXTZH': 'status-processing',
|
||
'IssuingToken': 'status-processing',
|
||
'Listing': 'status-processing',
|
||
'Listed': 'status-success',
|
||
'Failed': 'status-failed',
|
||
};
|
||
return classMap[state] || 'status-pending';
|
||
}
|
||
|
||
// 初始化导航栏
|
||
function initNav() {
|
||
const token = getToken();
|
||
const user = getUser();
|
||
|
||
const loginLink = document.getElementById('login-link');
|
||
const registerLink = document.getElementById('register-link');
|
||
const dashboardLink = document.getElementById('dashboard-link');
|
||
const adminLink = document.getElementById('admin-link');
|
||
const logoutLink = document.getElementById('logout-link');
|
||
|
||
if (token && user) {
|
||
// 已登录
|
||
if (loginLink) loginLink.style.display = 'none';
|
||
if (registerLink) registerLink.style.display = 'none';
|
||
if (dashboardLink) dashboardLink.style.display = 'inline';
|
||
if (logoutLink) logoutLink.style.display = 'inline';
|
||
|
||
// 管理员显示管理链接
|
||
if (user.role === 'admin' && adminLink) {
|
||
adminLink.style.display = 'inline';
|
||
}
|
||
} else {
|
||
// 未登录
|
||
if (loginLink) loginLink.style.display = 'inline';
|
||
if (registerLink) registerLink.style.display = 'inline';
|
||
if (dashboardLink) dashboardLink.style.display = 'none';
|
||
if (adminLink) adminLink.style.display = 'none';
|
||
if (logoutLink) logoutLink.style.display = 'none';
|
||
}
|
||
|
||
// 退出登录
|
||
if (logoutLink) {
|
||
logoutLink.addEventListener('click', (e) => {
|
||
e.preventDefault();
|
||
clearToken();
|
||
showAlert('已退出登录', 'success');
|
||
setTimeout(() => {
|
||
window.location.href = '/';
|
||
}, 1000);
|
||
});
|
||
}
|
||
}
|
||
|
||
// 页面加载完成后初始化
|
||
document.addEventListener('DOMContentLoaded', () => {
|
||
initNav();
|
||
});
|
||
|
||
// 导出工具函数供其他页面使用
|
||
window.NAC = {
|
||
apiRequest,
|
||
getToken,
|
||
setToken,
|
||
clearToken,
|
||
getUser,
|
||
setUser,
|
||
showAlert,
|
||
formatDate,
|
||
formatState,
|
||
getStateClass,
|
||
};
|