Skip to content

Commit

Permalink
feat: 修复无感token刷新鉴权失败问题
Browse files Browse the repository at this point in the history
  • Loading branch information
nonhana committed Aug 25, 2024
1 parent dd59eb7 commit 1900535
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/common/user-item/user-work-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const UserWorkItem: FC<UserWorkItemProps> = ({ itemInfo, like }) => {
<Link
to={`/work-detail/${itemInfo.id}`}
className='w-full cursor-pointer font-size-m color-shallowblack font-bold whitespace-nowrap overflow-hidden text-ellipsis inline-block'>
<span>{itemInfo.name}</span>
<span>{itemInfo.name ? itemInfo.name : '无题'}</span>
</Link>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/work-favorite-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const WorkFavoriteItem: FC<WorkFavoriteItemProps> = ({
<Link
to={`/work-detail/${itemInfo.id}`}
className='w-full cursor-pointer font-size-m color-shallowblack font-bold whitespace-nowrap overflow-hidden text-ellipsis inline-block'>
<span>{itemInfo.name}</span>
<span>{itemInfo.name ? itemInfo.name : '无题'}</span>
</Link>
<div className='w-full flex justify-between '>
<div className='flex items-center gap-10px font-size-m color-deepgrey'>
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/work-history-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const WorkHistoryItem: FC<WorkHistoryItemProps> = ({ itemInfo, ...props }) => {
<Link
to={`/work-detail/${itemInfo.id}`}
className='w-full cursor-pointer font-size-m color-shallowblack font-bold whitespace-nowrap overflow-hidden text-ellipsis inline-block'>
<span>{itemInfo.name}</span>
<span>{itemInfo.name ? itemInfo.name : '无题'}</span>
</Link>
<div className='w-full flex justify-between '>
<div className='flex items-center gap-10px font-size-m color-deepgrey'>
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/work-rank-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const WorkRankItem: FC<WorkRankItemProps> = ({ itemInfo, like }) => {
<Link
to={`/work-detail/${itemInfo.id}`}
className='w-full cursor-pointer font-size-m color-shallowblack font-bold whitespace-nowrap overflow-hidden text-ellipsis inline-block'>
<span>{itemInfo.name}</span>
<span>{itemInfo.name ? itemInfo.name : '无题'}</span>
</Link>

<div className='flex items-center gap-10px font-size-m color-deepgrey'>
Expand Down
17 changes: 14 additions & 3 deletions src/service/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { refreshTokenAPI } from '@/apis/user'
import { notification } from 'antd'
import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
Expand Down Expand Up @@ -53,7 +52,15 @@ class Request {
try {
refreshing = true
const refreshToken = localStorage.getItem('refreshToken')
const { data } = await refreshTokenAPI({ refreshToken: refreshToken! })

const response = await axios({
url: '/api/user/refresh-token',
method: 'GET',
params: { refreshToken: refreshToken },
})

const data = response.data.data

localStorage.setItem('accessToken', data.access_token)
localStorage.setItem('refreshToken', data.refresh_token)

Expand All @@ -66,12 +73,16 @@ class Request {
return this.instance.request(config)
} catch (error) {
notification.error({
message: 'token已失效,请重新进行登录~',
message: 'token已失效',
description: '请重新进行登录~即将跳转到首页哦',
})
pendingTasks.forEach((task) => {
task.reject(error)
})
pendingTasks.length = 0 // 清空队列
setTimeout(() => {
window.location.href = '/login'
}, 2000)
} finally {
refreshing = false
}
Expand Down

0 comments on commit 1900535

Please sign in to comment.