diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..aa445a2 --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +VITE_API_BASE_URL=http://localhost:8080/api \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..7ee1eb1 --- /dev/null +++ b/.env.production @@ -0,0 +1 @@ +VITE_API_BASE_URL=https://api.example.com/api \ No newline at end of file diff --git a/package.json b/package.json index f101856..12e5db3 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "video.js": "^8.5.2", "vue": "^3.3.4", "vue-echarts": "^6.6.9", - "vue-router": "^4.2.5" + "vue-router": "^4.2.5", + "axios": "^1.6.7" }, "devDependencies": { "@rushstack/eslint-patch": "^1.3.3", diff --git a/src/api/index.ts b/src/api/index.ts new file mode 100644 index 0000000..cacbe90 --- /dev/null +++ b/src/api/index.ts @@ -0,0 +1,4 @@ +export * from './user' +export * from './activity' +export * from './community' +// ... 导出其他API模块 \ No newline at end of file diff --git a/src/api/types.ts b/src/api/types.ts new file mode 100644 index 0000000..d2f61f9 --- /dev/null +++ b/src/api/types.ts @@ -0,0 +1,21 @@ +// API 响应数据接口 +export interface ApiResponse { + code: number + message: string + data: T +} + +// 分页请求参数接口 +export interface PageParams { + page: number + pageSize: number + [key: string]: any +} + +// 分页响应数据接口 +export interface PageResult { + list: T[] + total: number + page: number + pageSize: number +} \ No newline at end of file diff --git a/src/api/user.ts b/src/api/user.ts new file mode 100644 index 0000000..2f4e0e4 --- /dev/null +++ b/src/api/user.ts @@ -0,0 +1,37 @@ +import request from '@/utils/request' +import type { ApiResponse } from './types' + +export interface LoginParams { + username: string + password: string +} + +export interface UserInfo { + id: number + username: string + nickname: string + avatar: string + roles: string[] +} + +export const userApi = { + // 登录 + login(data: LoginParams) { + return request.post>('/auth/login', data) + }, + + // 获取用户信息 + getUserInfo() { + return request.get>('/user/info') + }, + + // 修改密码 + changePassword(data: { oldPassword: string; newPassword: string }) { + return request.post('/user/change-password', data) + }, + + // 更新用户信息 + updateUserInfo(data: Partial) { + return request.put('/user/info', data) + } +} \ No newline at end of file diff --git a/src/main/java/com/elderly/config/SecurityConfig.java b/src/main/java/com/elderly/config/SecurityConfig.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/config/SecurityConfig.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/controller/UserController.java b/src/main/java/com/elderly/controller/UserController.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/controller/UserController.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/dto/user/UserDTO.java b/src/main/java/com/elderly/dto/user/UserDTO.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/dto/user/UserDTO.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/entity/User.java b/src/main/java/com/elderly/entity/User.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/entity/User.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/security/TokenAuthenticationFilter.java b/src/main/java/com/elderly/security/TokenAuthenticationFilter.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/security/TokenAuthenticationFilter.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/service/UserService.java b/src/main/java/com/elderly/service/UserService.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/service/UserService.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/service/impl/UserServiceImpl.java b/src/main/java/com/elderly/service/impl/UserServiceImpl.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/service/impl/UserServiceImpl.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/vo/user/LoginRequest.java b/src/main/java/com/elderly/vo/user/LoginRequest.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/vo/user/LoginRequest.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main/java/com/elderly/vo/user/LoginResponse.java b/src/main/java/com/elderly/vo/user/LoginResponse.java deleted file mode 100644 index 0519ecb..0000000 --- a/src/main/java/com/elderly/vo/user/LoginResponse.java +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/utils/request.ts b/src/utils/request.ts new file mode 100644 index 0000000..9176ec1 --- /dev/null +++ b/src/utils/request.ts @@ -0,0 +1,74 @@ +import axios from 'axios' +import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' +import { ElMessage } from 'element-plus' +import router from '@/router' + +// 创建 axios 实例 +const service: AxiosInstance = axios.create({ + baseURL: import.meta.env.VITE_API_BASE_URL || '/api', // 从环境变量获取API基础路径 + timeout: 15000, // 请求超时时间 + headers: { + 'Content-Type': 'application/json' + } +}) + +// 请求拦截器 +service.interceptors.request.use( + (config: AxiosRequestConfig) => { + // 从 localStorage 获取 token + const token = localStorage.getItem('token') + if (token) { + config.headers = { + ...config.headers, + Authorization: `Bearer ${token}` + } + } + return config + }, + (error) => { + console.error('请求错误:', error) + return Promise.reject(error) + } +) + +// 响应拦截器 +service.interceptors.response.use( + (response: AxiosResponse) => { + const { code, message, data } = response.data + + // 根据后端约定的状态码处理 + if (code === 200) { + return data + } else { + ElMessage.error(message || '请求失败') + return Promise.reject(new Error(message || '请求失败')) + } + }, + (error) => { + const { status } = error.response || {} + + // 处理常见错误 + switch (status) { + case 401: + ElMessage.error('登录已过期,请重新登录') + localStorage.removeItem('token') + router.push('/login') + break + case 403: + ElMessage.error('没有权限访问') + break + case 404: + ElMessage.error('请求的资源不存在') + break + case 500: + ElMessage.error('服务器错误') + break + default: + ElMessage.error('网络错误') + } + + return Promise.reject(error) + } +) + +export default service \ No newline at end of file