add
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// API请求工具类
|
||||
class API {
|
||||
// HTTP请求类
|
||||
class HttpClient {
|
||||
constructor() {
|
||||
this.baseURL = API_CONFIG.BASE_URL;
|
||||
this.timeout = API_CONFIG.TIMEOUT;
|
||||
@@ -11,7 +11,7 @@ class API {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
|
||||
const token = localStorage.getItem(STORAGE.TOKEN);
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
@@ -48,51 +48,93 @@ class API {
|
||||
}
|
||||
|
||||
// GET请求
|
||||
async get(url) {
|
||||
get(url) {
|
||||
return this.request(url, { method: 'GET' });
|
||||
}
|
||||
|
||||
// POST请求
|
||||
async post(url, body) {
|
||||
post(url, body) {
|
||||
return this.request(url, { method: 'POST', body });
|
||||
}
|
||||
|
||||
// PUT请求
|
||||
async put(url, body) {
|
||||
put(url, body) {
|
||||
return this.request(url, { method: 'PUT', body });
|
||||
}
|
||||
|
||||
// DELETE请求
|
||||
async delete(url) {
|
||||
delete(url) {
|
||||
return this.request(url, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
// 处理未授权情况
|
||||
// 处理未授权
|
||||
handleUnauthorized() {
|
||||
localStorage.removeItem(STORAGE_KEYS.TOKEN);
|
||||
localStorage.removeItem(STORAGE_KEYS.USER_INFO);
|
||||
localStorage.removeItem(STORAGE.TOKEN);
|
||||
localStorage.removeItem(STORAGE.USER);
|
||||
window.location.href = 'login.html';
|
||||
}
|
||||
}
|
||||
|
||||
// 创建API实例
|
||||
const api = new API();
|
||||
// 创建HTTP客户端实例
|
||||
const http = new HttpClient();
|
||||
|
||||
// 工具函数
|
||||
const utils = {
|
||||
// 显示提示消息
|
||||
showMessage(message, type = 'info') {
|
||||
alert(message);
|
||||
// 工具函数类
|
||||
const Utils = {
|
||||
// 显示Toast消息
|
||||
showToast(message, type = 'info') {
|
||||
const toastEl = document.getElementById('liveToast');
|
||||
const toast = new bootstrap.Toast(toastEl);
|
||||
|
||||
const toastIcon = document.getElementById('toastIcon');
|
||||
const toastTitle = document.getElementById('toastTitle');
|
||||
const toastMessage = document.getElementById('toastMessage');
|
||||
|
||||
// 设置图标和样式
|
||||
toastEl.className = 'toast';
|
||||
toastIcon.className = 'bi me-2';
|
||||
|
||||
switch(type) {
|
||||
case 'success':
|
||||
toastEl.classList.add('success');
|
||||
toastIcon.classList.add('bi-check-circle-fill');
|
||||
toastIcon.style.color = '#198754';
|
||||
toastTitle.textContent = '成功';
|
||||
break;
|
||||
case 'error':
|
||||
toastEl.classList.add('error');
|
||||
toastIcon.classList.add('bi-x-circle-fill');
|
||||
toastIcon.style.color = '#dc3545';
|
||||
toastTitle.textContent = '错误';
|
||||
break;
|
||||
case 'warning':
|
||||
toastEl.classList.add('warning');
|
||||
toastIcon.classList.add('bi-exclamation-triangle-fill');
|
||||
toastIcon.style.color = '#ffc107';
|
||||
toastTitle.textContent = '警告';
|
||||
break;
|
||||
default:
|
||||
toastEl.classList.add('info');
|
||||
toastIcon.classList.add('bi-info-circle-fill');
|
||||
toastIcon.style.color = '#0dcaf0';
|
||||
toastTitle.textContent = '提示';
|
||||
}
|
||||
|
||||
toastMessage.textContent = message;
|
||||
toast.show();
|
||||
},
|
||||
|
||||
// 显示成功消息
|
||||
showSuccess(message) {
|
||||
this.showMessage(message, 'success');
|
||||
},
|
||||
|
||||
// 显示错误消息
|
||||
showError(message) {
|
||||
this.showMessage(message, 'error');
|
||||
// 显示/隐藏Loading
|
||||
loading(show = true) {
|
||||
const overlay = document.getElementById('loadingOverlay');
|
||||
if (overlay) {
|
||||
if (show) {
|
||||
overlay.classList.remove('d-none');
|
||||
overlay.classList.add('loading');
|
||||
} else {
|
||||
overlay.classList.add('d-none');
|
||||
overlay.classList.remove('loading');
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 确认对话框
|
||||
@@ -111,12 +153,24 @@ const utils = {
|
||||
formatDateTime(dateString) {
|
||||
if (!dateString) return '-';
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleString('zh-CN');
|
||||
return date.toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
},
|
||||
|
||||
// 获取当前用户信息
|
||||
// 格式化金额
|
||||
formatMoney(amount) {
|
||||
if (amount === null || amount === undefined) return '-';
|
||||
return '¥' + parseFloat(amount).toFixed(2);
|
||||
},
|
||||
|
||||
// 获取当前用户
|
||||
getCurrentUser() {
|
||||
const userStr = localStorage.getItem(STORAGE_KEYS.USER_INFO);
|
||||
const userStr = localStorage.getItem(STORAGE.USER);
|
||||
return userStr ? JSON.parse(userStr) : null;
|
||||
},
|
||||
|
||||
@@ -129,15 +183,15 @@ const utils = {
|
||||
// 退出登录
|
||||
logout() {
|
||||
if (this.confirm('确定要退出登录吗?')) {
|
||||
localStorage.removeItem(STORAGE_KEYS.TOKEN);
|
||||
localStorage.removeItem(STORAGE_KEYS.USER_INFO);
|
||||
localStorage.removeItem(STORAGE.TOKEN);
|
||||
localStorage.removeItem(STORAGE.USER);
|
||||
window.location.href = 'login.html';
|
||||
}
|
||||
},
|
||||
|
||||
// 检查登录状态
|
||||
checkAuth() {
|
||||
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
|
||||
const token = localStorage.getItem(STORAGE.TOKEN);
|
||||
if (!token) {
|
||||
window.location.href = 'login.html';
|
||||
return false;
|
||||
@@ -145,50 +199,64 @@ const utils = {
|
||||
return true;
|
||||
},
|
||||
|
||||
// 获取状态标签HTML
|
||||
getStatusBadge(status, type) {
|
||||
// 获取状态徽章HTML
|
||||
getStatusBadge(status, type = 'order') {
|
||||
const badges = {
|
||||
// 工单状态
|
||||
pending: '<span class="badge badge-info">待处理</span>',
|
||||
appointed: '<span class="badge badge-info">已预约</span>',
|
||||
in_progress: '<span class="badge badge-warning">进行中</span>',
|
||||
completed: '<span class="badge badge-success">已完成</span>',
|
||||
cancelled: '<span class="badge badge-secondary">已取消</span>',
|
||||
|
||||
// 支付状态
|
||||
unpaid: '<span class="badge badge-danger">未支付</span>',
|
||||
paid: '<span class="badge badge-success">已支付</span>',
|
||||
refunded: '<span class="badge badge-secondary">已退款</span>',
|
||||
|
||||
// 预约状态
|
||||
confirmed: '<span class="badge badge-success">已确认</span>',
|
||||
|
||||
// 车辆状态
|
||||
normal: '<span class="badge badge-success">正常</span>',
|
||||
in_service: '<span class="badge badge-warning">维修中</span>'
|
||||
order: {
|
||||
pending: '<span class="badge badge-status pending">待处理</span>',
|
||||
appointed: '<span class="badge badge-status appointed">已预约</span>',
|
||||
in_progress: '<span class="badge badge-status in_progress">进行中</span>',
|
||||
completed: '<span class="badge badge-status completed">已完成</span>',
|
||||
cancelled: '<span class="badge badge-status cancelled">已取消</span>'
|
||||
},
|
||||
payment: {
|
||||
unpaid: '<span class="badge badge-status unpaid">未支付</span>',
|
||||
paid: '<span class="badge badge-status paid">已支付</span>',
|
||||
refunded: '<span class="badge badge-status cancelled">已退款</span>'
|
||||
},
|
||||
appointment: {
|
||||
pending: '<span class="badge badge-status pending">待确认</span>',
|
||||
confirmed: '<span class="badge badge-status completed">已确认</span>',
|
||||
completed: '<span class="badge badge-status completed">已完成</span>',
|
||||
cancelled: '<span class="badge badge-status cancelled">已取消</span>'
|
||||
}
|
||||
};
|
||||
|
||||
return badges[status] || `<span class="badge badge-secondary">${status}</span>`;
|
||||
return badges[type]?.[status] || `<span class="badge bg-secondary">${status}</span>`;
|
||||
},
|
||||
|
||||
// 获取服务类型文本
|
||||
getServiceTypeText(type) {
|
||||
const types = {
|
||||
maintenance: '保养维护',
|
||||
repair: '维修服务',
|
||||
beauty: '美容服务',
|
||||
insurance: '保险代理'
|
||||
maintenance: '<span class="badge bg-primary">保养维护</span>',
|
||||
repair: '<span class="badge bg-warning">维修服务</span>',
|
||||
beauty: '<span class="badge bg-info">美容服务</span>',
|
||||
insurance: '<span class="badge bg-success">保险代理</span>'
|
||||
};
|
||||
return types[type] || type;
|
||||
},
|
||||
|
||||
// 获取用户角色文本
|
||||
// 获取角色文本
|
||||
getRoleText(role) {
|
||||
const roles = {
|
||||
admin: '管理员',
|
||||
staff: '工作人员',
|
||||
customer: '客户'
|
||||
admin: '<span class="badge bg-danger">管理员</span>',
|
||||
staff: '<span class="badge bg-success">工作人员</span>',
|
||||
customer: '<span class="badge bg-info">客户</span>'
|
||||
};
|
||||
return roles[role] || role;
|
||||
},
|
||||
|
||||
// 根据角色跳转
|
||||
redirectToDashboard(role) {
|
||||
const roleMap = {
|
||||
'admin': 'admin/dashboard.html',
|
||||
'staff': 'staff/dashboard.html',
|
||||
'customer': 'customer/dashboard.html'
|
||||
};
|
||||
const url = roleMap[role];
|
||||
if (url) {
|
||||
window.location.href = url;
|
||||
} else {
|
||||
this.showToast('未知的用户角色', 'error');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user