Initial commit: Car Maintenance Management System
Author: Yang Lu School: Liaoning Institute of Science and Technology Major: Computer Science and Technology Class: BZ246 Tech Stack: - Backend: Spring Boot 2.7.18 + JPA + MySQL - Frontend: HTML5 + CSS3 + JavaScript Features: - User Management (Admin/Staff/Customer roles) - Vehicle Archive Management - Service Order Management - Parts Inventory Management - Online Appointment Service - Data Statistics and Analysis Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
194
frontend/js/api.js
Normal file
194
frontend/js/api.js
Normal file
@@ -0,0 +1,194 @@
|
||||
// API请求工具类
|
||||
class API {
|
||||
constructor() {
|
||||
this.baseURL = API_CONFIG.BASE_URL;
|
||||
this.timeout = API_CONFIG.TIMEOUT;
|
||||
}
|
||||
|
||||
// 获取请求头
|
||||
getHeaders() {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
// 通用请求方法
|
||||
async request(url, options = {}) {
|
||||
const config = {
|
||||
method: options.method || 'GET',
|
||||
headers: this.getHeaders(),
|
||||
...options
|
||||
};
|
||||
|
||||
if (options.body && typeof options.body === 'object') {
|
||||
config.body = JSON.stringify(options.body);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(this.baseURL + url, config);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.code === 401) {
|
||||
this.handleUnauthorized();
|
||||
throw new Error('未授权,请重新登录');
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('API请求错误:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// GET请求
|
||||
async get(url) {
|
||||
return this.request(url, { method: 'GET' });
|
||||
}
|
||||
|
||||
// POST请求
|
||||
async post(url, body) {
|
||||
return this.request(url, { method: 'POST', body });
|
||||
}
|
||||
|
||||
// PUT请求
|
||||
async put(url, body) {
|
||||
return this.request(url, { method: 'PUT', body });
|
||||
}
|
||||
|
||||
// DELETE请求
|
||||
async delete(url) {
|
||||
return this.request(url, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
// 处理未授权情况
|
||||
handleUnauthorized() {
|
||||
localStorage.removeItem(STORAGE_KEYS.TOKEN);
|
||||
localStorage.removeItem(STORAGE_KEYS.USER_INFO);
|
||||
window.location.href = 'login.html';
|
||||
}
|
||||
}
|
||||
|
||||
// 创建API实例
|
||||
const api = new API();
|
||||
|
||||
// 工具函数
|
||||
const utils = {
|
||||
// 显示提示消息
|
||||
showMessage(message, type = 'info') {
|
||||
alert(message);
|
||||
},
|
||||
|
||||
// 显示成功消息
|
||||
showSuccess(message) {
|
||||
this.showMessage(message, 'success');
|
||||
},
|
||||
|
||||
// 显示错误消息
|
||||
showError(message) {
|
||||
this.showMessage(message, 'error');
|
||||
},
|
||||
|
||||
// 确认对话框
|
||||
confirm(message) {
|
||||
return window.confirm(message);
|
||||
},
|
||||
|
||||
// 格式化日期
|
||||
formatDate(dateString) {
|
||||
if (!dateString) return '-';
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString('zh-CN');
|
||||
},
|
||||
|
||||
// 格式化日期时间
|
||||
formatDateTime(dateString) {
|
||||
if (!dateString) return '-';
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleString('zh-CN');
|
||||
},
|
||||
|
||||
// 获取当前用户信息
|
||||
getCurrentUser() {
|
||||
const userStr = localStorage.getItem(STORAGE_KEYS.USER_INFO);
|
||||
return userStr ? JSON.parse(userStr) : null;
|
||||
},
|
||||
|
||||
// 检查用户角色
|
||||
hasRole(role) {
|
||||
const user = this.getCurrentUser();
|
||||
return user && user.role === role;
|
||||
},
|
||||
|
||||
// 退出登录
|
||||
logout() {
|
||||
if (this.confirm('确定要退出登录吗?')) {
|
||||
localStorage.removeItem(STORAGE_KEYS.TOKEN);
|
||||
localStorage.removeItem(STORAGE_KEYS.USER_INFO);
|
||||
window.location.href = 'login.html';
|
||||
}
|
||||
},
|
||||
|
||||
// 检查登录状态
|
||||
checkAuth() {
|
||||
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
|
||||
if (!token) {
|
||||
window.location.href = 'login.html';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
// 获取状态标签HTML
|
||||
getStatusBadge(status, type) {
|
||||
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>'
|
||||
};
|
||||
|
||||
return badges[status] || `<span class="badge badge-secondary">${status}</span>`;
|
||||
},
|
||||
|
||||
// 获取服务类型文本
|
||||
getServiceTypeText(type) {
|
||||
const types = {
|
||||
maintenance: '保养维护',
|
||||
repair: '维修服务',
|
||||
beauty: '美容服务',
|
||||
insurance: '保险代理'
|
||||
};
|
||||
return types[type] || type;
|
||||
},
|
||||
|
||||
// 获取用户角色文本
|
||||
getRoleText(role) {
|
||||
const roles = {
|
||||
admin: '管理员',
|
||||
staff: '工作人员',
|
||||
customer: '客户'
|
||||
};
|
||||
return roles[role] || role;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user