From d1ae36596477c1eed69994e93751dedc9d1e720b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=AB=E5=9C=B0=E5=83=A7?= <357099073@qq.com> Date: Tue, 28 Oct 2025 00:36:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0front=E7=9A=84api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/api/api.ts | 57 +++++ front/src/api/auth.ts | 32 +++ front/src/api/file.ts | 45 ++++ front/src/api/index.ts | 47 ++++ front/src/api/menu.ts | 55 +++++ front/src/api/programCategory.ts | 36 ++++ .../programAPI.ts => api/programInfo.ts} | 40 +--- front/src/api/test.ts | 60 ++++++ front/src/api/user.ts | 31 +++ front/src/services/api.ts | 201 ++---------------- server/conf/app.conf | 2 +- server/database/init_database.sql | 18 +- 12 files changed, 389 insertions(+), 235 deletions(-) create mode 100644 front/src/api/api.ts create mode 100644 front/src/api/auth.ts create mode 100644 front/src/api/file.ts create mode 100644 front/src/api/index.ts create mode 100644 front/src/api/menu.ts create mode 100644 front/src/api/programCategory.ts rename front/src/{services/programAPI.ts => api/programInfo.ts} (51%) create mode 100644 front/src/api/test.ts create mode 100644 front/src/api/user.ts diff --git a/front/src/api/api.ts b/front/src/api/api.ts new file mode 100644 index 0000000..2486265 --- /dev/null +++ b/front/src/api/api.ts @@ -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 +} \ No newline at end of file diff --git a/front/src/api/auth.ts b/front/src/api/auth.ts new file mode 100644 index 0000000..6327f86 --- /dev/null +++ b/front/src/api/auth.ts @@ -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 \ No newline at end of file diff --git a/front/src/api/file.ts b/front/src/api/file.ts new file mode 100644 index 0000000..576aa72 --- /dev/null +++ b/front/src/api/file.ts @@ -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 \ No newline at end of file diff --git a/front/src/api/index.ts b/front/src/api/index.ts new file mode 100644 index 0000000..2232567 --- /dev/null +++ b/front/src/api/index.ts @@ -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 \ No newline at end of file diff --git a/front/src/api/menu.ts b/front/src/api/menu.ts new file mode 100644 index 0000000..9a9bd40 --- /dev/null +++ b/front/src/api/menu.ts @@ -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 \ No newline at end of file diff --git a/front/src/api/programCategory.ts b/front/src/api/programCategory.ts new file mode 100644 index 0000000..0fd9584 --- /dev/null +++ b/front/src/api/programCategory.ts @@ -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 \ No newline at end of file diff --git a/front/src/services/programAPI.ts b/front/src/api/programInfo.ts similarity index 51% rename from front/src/services/programAPI.ts rename to front/src/api/programInfo.ts index 885e015..4428eec 100644 --- a/front/src/services/programAPI.ts +++ b/front/src/api/programInfo.ts @@ -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}`) } -} \ No newline at end of file +} + +export default programInfoAPI \ No newline at end of file diff --git a/front/src/api/test.ts b/front/src/api/test.ts new file mode 100644 index 0000000..cb5c11b --- /dev/null +++ b/front/src/api/test.ts @@ -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 \ No newline at end of file diff --git a/front/src/api/user.ts b/front/src/api/user.ts new file mode 100644 index 0000000..04962e8 --- /dev/null +++ b/front/src/api/user.ts @@ -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 \ No newline at end of file diff --git a/front/src/services/api.ts b/front/src/services/api.ts index 6424381..f8dcafb 100644 --- a/front/src/services/api.ts +++ b/front/src/services/api.ts @@ -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' diff --git a/server/conf/app.conf b/server/conf/app.conf index e14735d..2777c36 100644 --- a/server/conf/app.conf +++ b/server/conf/app.conf @@ -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 diff --git a/server/database/init_database.sql b/server/database/init_database.sql index f0106c2..fe46ccb 100644 --- a/server/database/init_database.sql +++ b/server/database/init_database.sql @@ -48,8 +48,8 @@ CREATE TABLE yz_menus ( path VARCHAR(255) NOT NULL COMMENT '菜单路径', parent_id INT DEFAULT 0 COMMENT '父菜单ID,0表示顶级菜单', 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),