Skip to content

Commit ab85840

Browse files
committed
fixed(api, auth): 调整api的使用,调整auth对token client_id等的使用方法
1 parent aca60c6 commit ab85840

18 files changed

Lines changed: 252 additions & 171 deletions

File tree

src/api/browser-events.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { clearAuth } from '@/utils/auth'
2+
13
export default {
24
onBeforeUnmount() {
3-
localStorage.removeItem('token')
4-
localStorage.removeItem('client_id')
5-
localStorage.removeItem('role')
5+
clearAuth()
66
console.log('Token and role have been removed from localStorage')
77
},
88
}

src/api/http-client/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios'
2+
import { getAuthToken, getClientIdFromStorage } from '@/utils/auth'
23

34
// 创建一个 axios 实例
45
const instance = axios.create({
@@ -8,14 +9,12 @@ const instance = axios.create({
89
// 请求拦截器:在请求发送之前附加 Authorization 头
910
instance.interceptors.request.use(
1011
(config) => {
11-
// 直接从localStorage获取client_id
12-
const clientId = localStorage.getItem('client_id')
13-
const token = localStorage.getItem('token')
12+
const clientId = getClientIdFromStorage()
13+
const token = getAuthToken()
1414

1515
if (token) {
1616
config.headers['Authorization'] = `Bearer ${token}`
1717
config.headers['Content-Type'] = 'application/json'
18-
// 如果存在client_id,则使用它
1918
if (clientId) {
2019
config.headers['clientid'] = clientId
2120
}

src/api/request.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import axios from 'axios'
2-
import { authConfig } from '@/config/request.config'
3-
const authToken = localStorage.getItem(authConfig.tokenKey)
4-
const clientId = localStorage.getItem(authConfig.clientIdKey)
2+
import { requestConfig } from '@/config/request.config'
3+
import { getAuthHeaders } from '@/utils/auth'
4+
55
const request = axios.create({
6-
baseURL: 'http://59.110.62.188:8080/',
6+
baseURL: requestConfig.baseURL,
77
timeout: 60000,
8-
headers: {
9-
Authorization: `Bearer ${authToken}`,
10-
clientId: `${clientId}`,
11-
},
8+
})
9+
10+
request.interceptors.request.use((config) => {
11+
const authHeaders = getAuthHeaders()
12+
config.headers = config.headers || {}
13+
14+
Object.entries(authHeaders).forEach(([key, value]) => {
15+
; (config.headers as Record<string, string>)[key] = value
16+
})
17+
18+
return config
1219
})
1320

1421
export default request

src/api/student-files.api.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import request from '@/api/request'
2+
3+
export interface StudentListParams {
4+
pageNum: number
5+
pageSize: number
6+
[key: string]: any
7+
}
8+
9+
export interface StudentDetailData {
10+
fundUserInfoVo: Record<string, any>
11+
fundProjectVo: any[]
12+
fundScholarshipVo: any[]
13+
fundPunishVo: any[]
14+
}
15+
16+
export const studentFilesApi = {
17+
getStudentList: (params: StudentListParams) => request.get('/grow/userInfo/listAll', { params }),
18+
19+
getStudentDetail: (userId: string) => request.get('/grow/userInfo/detail', { params: { userId } }),
20+
21+
exportStudentInfo: (userId: string) =>
22+
request.get('/grow/userOwnInfo/exportAll', {
23+
params: { userId },
24+
responseType: 'blob',
25+
}),
26+
27+
exportSelectedStudentInfo: (userIds: string[]) =>
28+
request.post(
29+
'/grow/userOwnInfo/exportOne',
30+
{ userId: userIds },
31+
{
32+
responseType: 'blob',
33+
},
34+
),
35+
}

src/api/user.api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ export interface AddProjectPayload {
133133
experience: string
134134
}
135135

136+
export interface UpdatePasswordPayload {
137+
oldPassword: string
138+
newPassword: string
139+
}
140+
136141
// 用户相关API
137142
export const userApi = {
138143
// 检查登录状态
@@ -158,6 +163,9 @@ export const userApi = {
158163
addSocialExperience: (data: AddProjectPayload): Promise<ApiResponse<void>> =>
159164
post('/grow/project/addProject', data),
160165

166+
updatePassword: (data: UpdatePasswordPayload): Promise<ApiResponse<void>> =>
167+
put('/system/user/profile/updatePwd', data),
168+
161169
uploadAvatar: (file: File): Promise<ApiResponse<{ imgUrl: string }>> => {
162170
const formData = new FormData()
163171
formData.append('avatarfile', file)

src/components/logout-action/index.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { WarningFilled } from '@element-plus/icons-vue'
4242
import { useRouter } from 'vue-router'
4343
import { userApi } from '@/api/user.api'
4444
import { useUserStore } from '@/stores/user-store'
45+
import { clearClientId } from '@/utils/auth'
4546
4647
withDefaults(
4748
defineProps<{
@@ -83,7 +84,7 @@ const handleLogout = async () => {
8384
ElMessage.success('退出成功!')
8485
outerVisible.value = false
8586
userStore.logout()
86-
localStorage.removeItem('client_id')
87+
clearClientId()
8788
setTimeout(() => {
8889
router.push('/')
8990
}, 120)

src/components/nav-bar/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import LogoutAction from '@/components/logout-action/index.vue'
6363
import { useUserStore } from '@/stores/user-store'
6464
import Coins from '@/components/coins/index.vue'
6565
import { CoinType } from '@/types/goods-info'
66-
import { authConfig } from '@/config/request.config'
66+
import { getAuthToken } from '@/utils/auth'
6767
6868
const router = useRouter()
6969
const route = useRoute()
@@ -161,7 +161,7 @@ const navigateTo = (path: string) => {
161161
}
162162
163163
onMounted(async () => {
164-
if (!localStorage.getItem(authConfig.tokenKey)) {
164+
if (!getAuthToken()) {
165165
return
166166
}
167167

src/config/request.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface RequestConfig {
99
export interface AuthConfig {
1010
tokenKey: string
1111
clientIdKey: string
12+
roleKey: string
13+
roleGroupKey: string
1214
clientId: string
1315
}
1416

@@ -23,5 +25,7 @@ export const requestConfig: RequestConfig = {
2325
export const authConfig: AuthConfig = {
2426
tokenKey: 'auth_token',
2527
clientIdKey: 'client_id',
28+
roleKey: 'role',
29+
roleGroupKey: 'roleGroup',
2630
clientId: getClientId(),
2731
}

src/core/request.core.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import axios, {
44
type AxiosResponse,
55
type InternalAxiosRequestConfig,
66
} from 'axios'
7-
import { authConfig } from '@/config/request.config'
7+
import { clearAuth, getAuthToken, getClientIdFromStorage } from '@/utils/auth'
88
import type { ApiResponse, RequestOptions, ErrorResponse } from '@/types/request.types'
99

1010
export class RequestCore {
@@ -17,18 +17,6 @@ export class RequestCore {
1717
this.setupInterceptors()
1818
}
1919

20-
/**
21-
* 获取认证令牌
22-
*/
23-
private getAuthToken(): string | null {
24-
try {
25-
return localStorage.getItem(authConfig.tokenKey)
26-
} catch (error) {
27-
console.warn('Failed to get auth token from localStorage:', error)
28-
return null
29-
}
30-
}
31-
3220
/**
3321
* 设置请求拦截器
3422
*/
@@ -37,13 +25,16 @@ export class RequestCore {
3725
this.requestInterceptor = this.instance.interceptors.request.use(
3826
(config: InternalAxiosRequestConfig) => {
3927
// 获取认证令牌
40-
const token = this.getAuthToken()
28+
const token = getAuthToken()
29+
const clientId = getClientIdFromStorage()
4130

4231
// 添加认证头
4332
if (token) {
4433
config.headers = config.headers || {}
4534
config.headers.Authorization = `Bearer ${token}`
46-
config.headers.clientId = authConfig.clientId
35+
if (clientId) {
36+
config.headers.clientid = clientId
37+
}
4738
}
4839

4940
// 这里可以添加其他全局请求处理逻辑
@@ -116,8 +107,7 @@ export class RequestCore {
116107
* 处理认证失败
117108
*/
118109
private handleUnauthorized(): void {
119-
// 清除本地认证信息
120-
localStorage.removeItem(authConfig.tokenKey)
110+
clearAuth()
121111

122112
// 这里可以添加跳转到登录页的逻辑
123113
console.warn('Authentication failed, redirecting to login...')

src/mixins/clear-local-storage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { clearAuth } from '@/utils/auth'
2+
13
export default {
24
onBeforeUnmount() {
3-
localStorage.removeItem('token')
4-
localStorage.removeItem('client_id')
5-
localStorage.removeItem('role')
5+
clearAuth()
66
console.log('Token and role have been removed from localStorage')
77
},
88
}

0 commit comments

Comments
 (0)