更新front的api

This commit is contained in:
扫地僧 2025-10-28 00:36:41 +08:00
parent 476c5e7658
commit d1ae365964
12 changed files with 389 additions and 235 deletions

57
front/src/api/api.ts Normal file
View File

@ -0,0 +1,57 @@
// API服务统一入口
// 重新导出所有API模块保持向后兼容性
// 基础API实例
export { default as api } from './index'
// 认证相关API
export { authAPI } from './auth'
export { default as authAPI } from './auth'
// 菜单相关API
export { menuAPI } from './menu'
export { default as menuAPI } from './menu'
// 用户相关API
export { userAPI } from './user'
export { default as userAPI } from './user'
// 文件相关API
export { fileAPI } from './file'
export { default as fileAPI } from './file'
// 程序分类相关API
export { programCategoryAPI } from './programCategory'
export { default as programCategoryAPI } from './programCategory'
// 程序信息相关API
export { programInfoAPI } from './programInfo'
export { default as programInfoAPI } from './programInfo'
// 为了保持向后兼容性也可以这样导入所有API
import authAPI from './auth'
import menuAPI from './menu'
import userAPI from './user'
import fileAPI from './file'
import programCategoryAPI from './programCategory'
import programInfoAPI from './programInfo'
// 导出所有API对象
export {
authAPI,
menuAPI,
userAPI,
fileAPI,
programCategoryAPI,
programInfoAPI
}
// 默认导出所有API可选
export default {
authAPI,
menuAPI,
userAPI,
fileAPI,
programCategoryAPI,
programInfoAPI
}

32
front/src/api/auth.ts Normal file
View File

@ -0,0 +1,32 @@
import api from './index'
// 认证相关API
export const authAPI = {
// 用户登录
login: (username: string, password: string) => {
return api.post('/api/login', {
username: username,
password: password
})
},
// 用户登出
logout: () => {
return api.post('/api/logout')
},
// 重置密码
resetPassword: (username: string, superPassword: string) => {
const formData = new URLSearchParams()
formData.append('username', username)
formData.append('superPassword', superPassword)
return api.post('/api/reset-password', formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
}
}
export default authAPI

45
front/src/api/file.ts Normal file
View File

@ -0,0 +1,45 @@
import api from './index'
// 文件相关API
export const fileAPI = {
// 获取所有文件
getAllFiles: () => {
return api.get('/api/files')
},
// 获取我的文件
getMyFiles: () => {
return api.get('/api/files/my')
},
// 根据ID获取文件
getFileById: (id: number) => {
return api.get(`/api/files/${id}`)
},
// 上传文件
uploadFile: (formData: FormData) => {
return api.post('/api/files', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
},
// 更新文件信息
updateFile: (id: number, fileData: any) => {
return api.put(`/api/files/${id}`, fileData)
},
// 删除文件
deleteFile: (id: number) => {
return api.delete(`/api/files/${id}`)
},
// 搜索文件
searchFiles: (keyword: string) => {
return api.get(`/api/files/search?keyword=${encodeURIComponent(keyword)}`)
}
}
export default fileAPI

47
front/src/api/index.ts Normal file
View File

@ -0,0 +1,47 @@
// API服务封装
import axios from 'axios'
// 创建axios实例
const api = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
})
// 请求拦截器
api.interceptors.request.use(
(config) => {
// 获取token
const token = localStorage.getItem('token')
if (token) {
// 设置Authorization头
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// 响应拦截器
api.interceptors.response.use(
(response) => {
return response.data;
},
(error) => {
if (error.response && error.response.status === 401) {
// token过期或无效清除本地登录状态并跳转到登录页
localStorage.removeItem('isAuthenticated');
localStorage.removeItem('user');
localStorage.removeItem('token');
window.location.href = '/login';
}
console.error('API请求错误:', error);
return Promise.reject(error);
}
);
export default api

55
front/src/api/menu.ts Normal file
View File

@ -0,0 +1,55 @@
import api from './index'
// 菜单相关API
export const menuAPI = {
// 获取所有菜单
getAllMenus: () => {
return api.get('/api/allmenu')
},
// 获取启用的菜单(用于路由生成)
getActiveMenus: () => {
return api.get('/api/menus/active')
},
//获取顶级菜单
getTopLevelMenus: () => {
return api.get('/api/menus/top-level')
},
// 根据ID获取菜单
getMenusByParentId: (id: number) => {
return api.get(`/api/menus/parent/${id}`)
},
// 创建菜单
createMenu: (menuData: any) => {
return api.request({
method: 'POST',
url: '/api/menu',
data: menuData,
headers: {
'Content-Type': 'application/json'
}
})
},
// 更新菜单
updateMenu: (id: number, menuData: any) => {
return api.request({
method: 'PUT',
url: `/api/menu/${id}`,
data: menuData,
headers: {
'Content-Type': 'application/json'
}
})
},
// 删除菜单
deleteMenu: (id: number) => {
return api.delete(`/api/menu/${id}`)
}
}
export default menuAPI

View File

@ -0,0 +1,36 @@
import api from './index'
// 程序分类相关API
export const programCategoryAPI = {
// 获取所有程序分类(需要认证)
getAllProgramCategories: () => {
return api.get('/api/program-categories')
},
// 获取所有程序分类(不需要认证的公共接口)
getAllProgramCategoriesPublic: () => {
return api.get('/api/program-categories/public')
},
// 根据ID获取程序分类
getProgramCategoryById: (id: number) => {
return api.get(`/api/program-categories/${id}`)
},
// 创建程序分类
createProgramCategory: (categoryData: any) => {
return api.post('/api/program-categories', categoryData)
},
// 更新程序分类
updateProgramCategory: (id: number, categoryData: any) => {
return api.put(`/api/program-categories/${id}`, categoryData)
},
// 删除程序分类
deleteProgramCategory: (id: number) => {
return api.delete(`/api/program-categories/${id}`)
}
}
export default programCategoryAPI

View File

@ -1,38 +1,4 @@
// 程序分类和程序信息API服务
import api from './api'
// 程序分类相关API
export const programCategoryAPI = {
// 获取所有程序分类(需要认证)
getAllProgramCategories: () => {
return api.get('/api/program-categories')
},
// 获取所有程序分类(不需要认证的公共接口)
getAllProgramCategoriesPublic: () => {
return api.get('/api/program-categories/public')
},
// 根据ID获取程序分类
getProgramCategoryById: (id: number) => {
return api.get(`/api/program-categories/${id}`)
},
// 创建程序分类
createProgramCategory: (categoryData: any) => {
return api.post('/api/program-categories', categoryData)
},
// 更新程序分类
updateProgramCategory: (id: number, categoryData: any) => {
return api.put(`/api/program-categories/${id}`, categoryData)
},
// 删除程序分类
deleteProgramCategory: (id: number) => {
return api.delete(`/api/program-categories/${id}`)
}
}
import api from './index'
// 程序信息相关API
export const programInfoAPI = {
@ -70,4 +36,6 @@ export const programInfoAPI = {
deleteProgramInfo: (id: number) => {
return api.delete(`/api/program-infos/${id}`)
}
}
}
export default programInfoAPI

60
front/src/api/test.ts Normal file
View File

@ -0,0 +1,60 @@
// API重构测试文件
import { authAPI, menuAPI, userAPI, fileAPI, programCategoryAPI, programInfoAPI } from '../services/api'
import api from '../api/index'
// 测试函数
export function testAPIImports() {
console.log('Testing API imports...')
// 测试基础API实例
console.log('API instance:', api)
// 测试各个API模块
console.log('Auth API:', authAPI)
console.log('Menu API:', menuAPI)
console.log('User API:', userAPI)
console.log('File API:', fileAPI)
console.log('Program Category API:', programCategoryAPI)
console.log('Program Info API:', programInfoAPI)
// 测试API方法是否存在
console.log('Auth API methods:', {
login: typeof authAPI.login,
logout: typeof authAPI.logout,
resetPassword: typeof authAPI.resetPassword
})
console.log('Menu API methods:', {
getAllMenus: typeof menuAPI.getAllMenus,
getActiveMenus: typeof menuAPI.getActiveMenus,
createMenu: typeof menuAPI.createMenu
})
console.log('User API methods:', {
getAllUsers: typeof userAPI.getAllUsers,
getUserById: typeof userAPI.getUserById,
createUser: typeof userAPI.createUser
})
console.log('File API methods:', {
getAllFiles: typeof fileAPI.getAllFiles,
uploadFile: typeof fileAPI.uploadFile,
searchFiles: typeof fileAPI.searchFiles
})
console.log('Program Category API methods:', {
getAllProgramCategories: typeof programCategoryAPI.getAllProgramCategories,
createProgramCategory: typeof programCategoryAPI.createProgramCategory
})
console.log('Program Info API methods:', {
getAllProgramInfos: typeof programInfoAPI.getAllProgramInfos,
createProgramInfo: typeof programInfoAPI.createProgramInfo
})
console.log('All API imports test completed successfully!')
return true
}
// 运行测试
export default testAPIImports

31
front/src/api/user.ts Normal file
View File

@ -0,0 +1,31 @@
import api from './index'
// 用户相关API
export const userAPI = {
// 获取所有用户
getAllUsers: () => {
return api.get('/api/users')
},
// 根据ID获取用户
getUserById: (id: number) => {
return api.get(`/api/users/${id}`)
},
// 创建用户
createUser: (userData: any) => {
return api.post('/api/users', userData)
},
// 更新用户
updateUser: (id: number, userData: any) => {
return api.put(`/api/users/${id}`, userData)
},
// 删除用户
deleteUser: (id: number) => {
return api.delete(`/api/users/${id}`)
}
}
export default userAPI

View File

@ -1,198 +1,23 @@
// API服务封装
import axios from 'axios'
// API服务统一入口
// 重新导出所有API模块保持向后兼容性
// 创建axios实例
const api = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080',
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
})
// 请求拦截器
api.interceptors.request.use(
(config) => {
// 获取token
const token = localStorage.getItem('token')
if (token) {
// 设置Authorization头
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// 响应拦截器
api.interceptors.response.use(
(response) => {
return response.data;
},
(error) => {
if (error.response && error.response.status === 401) {
// token过期或无效清除本地登录状态并跳转到登录页
localStorage.removeItem('isAuthenticated');
localStorage.removeItem('user');
localStorage.removeItem('token');
window.location.href = '/login';
}
console.error('API请求错误:', error);
return Promise.reject(error);
}
);
// 基础API实例
export { default as api } from '../api/index'
// 认证相关API
export const authAPI = {
// 用户登录
login: (username: string, password: string) => {
return api.post('/api/login', {
username: username,
password: password
})
},
// 用户登出
logout: () => {
return api.post('/api/logout')
},
// 重置密码
resetPassword: (username: string, superPassword: string) => {
const formData = new URLSearchParams()
formData.append('username', username)
formData.append('superPassword', superPassword)
return api.post('/api/reset-password', formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
}
}
export { default as authAPI } from '../api/auth'
// 菜单相关API
export const menuAPI = {
// 获取所有菜单
getAllMenus: () => {
return api.get('/api/allmenu')
},
// 获取启用的菜单(用于路由生成)
getActiveMenus: () => {
return api.get('/api/menus/active')
},
//获取顶级菜单
getTopLevelMenus: () => {
return api.get('/api/menus/top-level')
},
// 根据ID获取菜单
getMenusByParentId: (id: number) => {
return api.get(`/api/menus/parent/${id}`)
},
// 创建菜单
createMenu: (menuData: any) => {
return api.request({
method: 'POST',
url: '/api/menu',
data: menuData,
headers: {
'Content-Type': 'application/json'
}
})
},
// 更新菜单
updateMenu: (id: number, menuData: any) => {
return api.request({
method: 'PUT',
url: `/api/menu/${id}`,
data: menuData,
headers: {
'Content-Type': 'application/json'
}
})
},
// 删除菜单
deleteMenu: (id: number) => {
return api.delete(`/api/menu/${id}`)
}
}
export { default as menuAPI } from '../api/menu'
// 用户相关API
export const userAPI = {
// 获取所有用户
getAllUsers: () => {
return api.get('/api/users')
},
// 根据ID获取用户
getUserById: (id: number) => {
return api.get(`/api/users/${id}`)
},
// 创建用户
createUser: (userData: any) => {
return api.post('/api/users', userData)
},
// 更新用户
updateUser: (id: number, userData: any) => {
return api.put(`/api/users/${id}`, userData)
},
// 删除用户
deleteUser: (id: number) => {
return api.delete(`/api/users/${id}`)
}
}
export { default as userAPI } from '../api/user'
// 文件相关API
export const fileAPI = {
// 获取所有文件
getAllFiles: () => {
return api.get('/api/files')
},
// 获取我的文件
getMyFiles: () => {
return api.get('/api/files/my')
},
// 根据ID获取文件
getFileById: (id: number) => {
return api.get(`/api/files/${id}`)
},
// 上传文件
uploadFile: (formData: FormData) => {
return api.post('/api/files', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
},
// 更新文件信息
updateFile: (id: number, fileData: any) => {
return api.put(`/api/files/${id}`, fileData)
},
// 删除文件
deleteFile: (id: number) => {
return api.delete(`/api/files/${id}`)
},
// 搜索文件
searchFiles: (keyword: string) => {
return api.get(`/api/files/search?keyword=${encodeURIComponent(keyword)}`)
}
}
export { default as fileAPI } from '../api/file'
export default api
// 程序分类相关API
export { default as programCategoryAPI } from '../api/programCategory'
// 程序信息相关API
export { default as programInfoAPI } from '../api/programInfo'

View File

@ -6,7 +6,7 @@ runmode = dev
# MySQL - 远程连接配置
mysqluser = gotest
mysqlpass = 2nZhRdMPCNZrdzsd
mysqlurls = 212.64.112.158:3307
mysqlurls = 43.133.71.191:3308
mysqldb = gotest
# SQLite

View File

@ -48,8 +48,8 @@ CREATE TABLE yz_menus (
path VARCHAR(255) NOT NULL COMMENT '菜单路径',
parent_id INT DEFAULT 0 COMMENT '父菜单ID0表示顶级菜单',
icon VARCHAR(100) DEFAULT NULL COMMENT '菜单图标',
order INT DEFAULT 0 COMMENT '排序序号',
status TINYINT DEFAULT 1 COMMENT '状态0-禁用1-启用',
`order` INT DEFAULT 0 COMMENT '排序序号',
`status` TINYINT DEFAULT 1 COMMENT '状态0-禁用1-启用',
component_path VARCHAR(500) DEFAULT NULL COMMENT '组件路径',
is_external TINYINT DEFAULT 0 COMMENT '是否外部链接0-内部路由1-外部链接',
external_url VARCHAR(1000) DEFAULT NULL COMMENT '外部链接地址',
@ -63,11 +63,9 @@ CREATE TABLE yz_menus (
-- 索引
INDEX idx_parent_id (parent_id),
INDEX idx_level (level),
INDEX idx_status (status),
INDEX idx_order (order_num),
INDEX idx_menu_type (menu_type),
INDEX idx_full_path (full_path(255))
INDEX idx_status (`status`),
INDEX idx_order (`order`),
INDEX idx_menu_type (menu_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='菜单表(增强版)';
-- =============================================
@ -168,9 +166,9 @@ INSERT INTO yz_menus (name, path, parent_id, icon, `order`, status, component_pa
('角色管理', '/system/roles', 2, 'fa-solid fa-user-tag', 2, 1, '@/views/system/roles/index.vue', 1, '角色权限管理'),
('权限管理', '/system/permissions', 2, 'fa-solid fa-key', 2, 1, '@/views/system/permissions/index.vue', 1, '权限管理'),
('菜单管理', '/system/menus', 2, 'fa-solid fa-bars-progress', 2, 1, '@/views/system/menus/manager.vue', 1, '菜单权限管理'),
('程序管理', '/system/programs', 2, 'fa-solid fa-cpu', 3, 1, '@/views/system/programs/index.vue', 1, '程序功能管理'),
('知识库', '/apps/knowledge', 4, 'fa-solid fa-book', 1, 1, '@/views/apps/knowledge/index.vue', 1, '知识库管理');
('程序管理', '/system/programs', 2, 'fa-solid fa-grip', 3, 1, '@/views/system/programs/index.vue', 1, '程序功能管理'),
('知识库', '/apps/knowledge', 4, 'fa-solid fa-book', 1, 1, '@/views/apps/knowledge/index.vue', 1, '知识库管理'),
('详情', '/apps/knowledge/detail', 11, '', 1, 1, '@/views/apps/knowledge/components/detail.vue', 1, '知识库详情');
-- 插入默认程序分类
INSERT INTO yz_program_category (category_name, category_desc, parent_id, sort_order) VALUES
('办公软件', '办公相关程序', 0, 1),